This tutorial will help you to get started with TypeScript coding syntax, you may find many similarities with your other programming languages and JavaScript, you can consider TypeScript as a mediator between JavaScript and any other server side programming language, or in other words TypeScript helps creating object oriented JavaScript more easily.
let speed: number = 10; if (speed >= 50) { console.log("you are at right speed") }
Learn how to work with typescript class, methods, variable etc, Here we start with basic TypeScript syntax with some examples.
We learn how to declare a variable in typescript, assign value in variable, different type of access modifiers, typescript datatypes etc.
Here are some example of declaring local variable with initial values
[variable-Name: data-type]
or
[Variable-Name: data-type = initial-value]
studentId: number=0; title: string = "Create"; errorMessage: any; fullname: string=""; isActive: boolean = true;
In variable we also can specify access modifier like private, public, protected (like we use in c#) example: [access-modifier Variable-Name: data-type = initial-value]
private studentId: number = 0; public title: string = "Create"; protected errorMessage: any;
namespace WebTrainingRoom.TypeScript { class DataTypeExample { } }
To define module in TypeScript, we create file containing a top-level export or import.
Here in example we create a student class with one function and access that from school class.
For each class we will have two separate files called <classname>.ts
Student.ts file export class Student { studentCode: number; studentName: string; constructor(name: string, code: number) { this.studentCode = code; this.studentName = name; } showStudent() { console.log("Student [" + this.studentCode + "] : " + this.studentName); } }
Now we access the above module in school module
School.ts file import { Student } from "./Student"; let stuObj = new Student("Steve Jobs", 1); stuObj.showStudent();