The base type

let isDone: boolean = false; let decLiteral: number = 6; let name: string = "bob"; let list: number[] = [1, 2, 3]; let list: Array<number> = [1, 2, 3]; let x: [string, number]; x = ['hello', 10]; // OK enumeration enum Color {Red, Green, Blue} let c: Color = color.green; let notSure: any = 4; let u: undefined = undefined; let n: null = null;Copy the code

Types of assertions

Type assertions come in two forms. Let someValue: any = "this is a string"; let strLength: number = (<string>someValue).length; Let someValue: any = "this is a string"; let strLength: number = (someValue as string).length;Copy the code

interface

interface LabelledValue {
  label: string;
}

function printLabel(labelledObj: LabelledValue) {
  console.log(labelledObj.label);
}

let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

Copy the code

Optional attribute

interface SquareConfig { color? : string; width? : number; } If SquareConfig has the color and width attributes of the type defined above, and any number of other attributes, we can define it like this: interface SquareConfig {color? : string; width? : number; [propName: string]: any; }Copy the code

Read-only property

interface Point {
    readonly x: number;
    readonly y: number;
}
Copy the code

Function type interface

interface SearchFunc {
  (source: string, subString: string): boolean;
}
Copy the code

Interface inheritance

interface Shape {
    color: string;
}

interface Square extends Shape {
    sideLength: number;
}

let square = <Square>{};
square.color = "blue";
square.sideLength = 10;
Copy the code