Typescript basic syntax example

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.

TypeScript Coding Syntax

We learn how to declare a variable in typescript, assign value in variable, different type of access modifiers, typescript datatypes etc.

  1. TypeScript basic variables, modifiers

    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;  
    
  2. We can define namespace in typescript like C#
    namespace WebTrainingRoom.TypeScript
    {
        class DataTypeExample
        {
        }
    }
    
  3. Module in TypeScript
    Here conceptually modules are like some piece of code that can be used from different place, just think if we need some common functionality to be written, we can write that in a separate file and then use those functionality from wherever we want.

    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();
    
  4. How to call a typescript method and pass complex object as parameter
  5. Define Class in TypeScript, constructor, property, method
  6. How to write if-else statement and switch case in TypeScript
  7. How to call Web API services
 
TypeScript variable, syntax, structure
Learn typescript programming with real-time examples.
Popular Libraries
TypeScript Tutorial
TypeScript and JavaScript Examples