Typescript Basics

Static typing

let name: string; 
let id: number; 
let now: date;

Interface

interface IStudent {
    name: string, 
    id: number
}

// The following will give error coz it's missing id property
let john:IStudent = {
    name: 'John'
}

// To make the above statement to true, we add optional question 
// mark to id
interface IStudent {
    name: string, 
    id?: number
}

Class

class Student {
    name: string;
    id: number;
    constructor (name) {
        this.name = name;
    }
}

// Equivalent form
class Student {
    constructor (name, id)
}



// By default, all properties and members are public if not 
// specified. If private members intended, we need to add the 
// keyword
class Student {
    private name: string; 
    private id: number;
    constructor (name, id) {
        this.name = name;
        this.id = id;
    }
}

// Equivalent form
class Student {
    constructor (private name, private id)
}

let john = new Student ('John', 123)