基础类型
1布尔值
let isDone: boolean = false;
2 数字
let decLiteral: number = 6;
let hexLiteral: number = 0xf00d;
let binaryLiteral: number = 0b1010;
let octalLiteral: number = 0o7;
3 字符串
let name: string = "bob";
name = "smith";
4 数组
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3]; //数组泛型,Array<元素类型>
5 元组 Tuple
let x: [string, number]
x = ['hello', 10];
6 枚举
enum Color {Red, Green, Blue}
let c: Color = Color.Green
7 任意值
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean
8 空值
function warnUser(): void {
alert("This is my warning message");
}
let unusable: void = undefined;
9 Never
// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
throw new Error(message);
}
// 推断的返回值类型为never
function fail() {
return error("Something failed");
}
// 返回never的函数必须存在无法达到的终点
function infiniteLoop(): never {
while (true) {
}
}