声明数组的三种方式
let array1: string[] = ['name', 'age'];
let array2: Array<string> = ['name', 'age'];
let array3: [string] = ['name', 'age']; (推荐)
let array4: [string, number] = ['name', 123];
声明枚举
enum Color {
Red,
Green,
Blue = 5
};
let color1: Color = Color.Green; // print 1
let color2: Color = Color.Blue; // print 5
let colorName: string = Color[0]; // print Red
声明联合类型
let foo: string | number = 123;
foo = 'foo'; // 合法
foo = true; // 不合法
类型断言
let someValue: any = "this is a string";
let strLength: number = (someValue).length;
或者:(推荐)
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;