fat-cat

basic types(基本类型)

最简单的数据单元:数字,字符串,结构体,布尔值,枚举类型

布尔值

 let isDone: boolean = false

数字

 let decLiteral: number = 6

字符串

 let name: string = 'smith'

数组

 let list: number[] = [1, 2, 3]

 let list: Array<number> = [1, 2, 3]

元组(Tuple)

 // Declare a tuple type
 let x: [string, number]

 // Initialize it
 x = ['hello', 10] // OK

 // Initialize it incorrectly
 x = [10, 'hello'] // Error

枚举

enum 类型是对 JavaScript 标准数据类型的一个补充。 像 C#等其它语言一样,使用枚举类型可以为一组数值赋予友好的名字

 enum Color {Red = 1, Green, Blue}
 let colorName: string = Color[2];

 console.log(colorName);  // 显示'Green'因为上面代码里它的值是2

Any

为那些在编程阶段还不清楚类型的变量指定一个类型

 let notSure: any = 4;
 notSure = "maybe a string instead";
 notSure = false; // okay, definitely a boolean

Void

表示没有任何类型

 function warnUser(): void {
    console.log("This is my warning message");
 }

Null 和 undefined

Never

never 类型表示的是那些永不存在的值的类型

// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
    throw new Error(message);
}

// 推断的返回值类型为never
function fail() {
    return error("Something failed");
}

// 返回never的函数必须存在无法达到的终点
function infiniteLoop(): never {
    while (true) {
    }
}

Object

object表示非原始类型

declare function create(o: object | null): void

类型断言

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