一、基础类型
数组 :
1 2 3 > const list:number [] = [1 ,2 ,3 ] > const list:Array <number > = [1 ,2 ,3 ] >
元祖 tuple
1 2 3 4 5 6 > let x: [string , number ] > x = ['hello' ,2 ] > x = [2 , 'hello' ] > x[3 ] = 'world' > x[4 ] = true >
枚举 枚举类型是对javascript标准数据结构的一个补充 为一组数值赋予优雅的名字
1 2 3 4 5 6 7 8 9 10 > enum Color {Red, Green, Blue} > let x: Color = Color.Green > > > enum Color {Red:1 , Green:4 , Blue:6 } > let x: Color = Color.Green > > > let colorName: string = Color[4 ] >
void 空值 函数返回 只能被赋予undefined null
1 2 3 4 > function warnUser ( ): void { > alert("This is a warning message!" ) > } >
never 永远不存在
1 2 3 4 5 6 7 8 9 > > function error (message: string ): never { > throw new Error (message) > } > > function infiniteLoop ( ): never { > while (true ){} > } >
类型断言 只在编译阶段起作用,假设已经进行了必须的检查
1 2 3 4 5 6 7 8 > > 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 >
二、接口 TS核心原则之一 “鸭式辩型法”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 > function printLabel (labelObj:{label: string } ) { > console .log('label:' , labelObj.label) > } > > let myObj = {size: 10 , label: 'Size 10 Object' } > printLabel(myObj) > > > interface labelValue { > label: string ; > name?: string ; > readonly count: number ; > } > > > let ro:ReadonlyArray<number > = [1 ,3 ,4 ,5 ] > > function printLabel (labelObj: labelValue ) { > console .log('label:' ,labelObj.label) > } >
额外的属性检查: ts中,对象字面量会经过额外属性检查 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 > interface SquareConfig { > color?: string ; > width?: number ; > } > > function createSquare (config: SquareConfig ): {color: string ; area:number }{ > > } > > let mySquare = createSquare({colour:'red' , width: 100 }) > > > let mySquare = createSquare({colour:'red' ,width:100 } as SquareConfig) > > > interface SquareConfig { > color?: string ; > number ?: number ; > [propName:string ]: any ; > } >
函数类型 1 2 3 4 5 6 7 8 9 > interface SearchFunc { > (source:string , subString: string ):boolean > } > > let mySearch: SearchFunc > mySearch = function (source:string , subString:string ) { > return true > } >
可索引的类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 > > > interface StringArray { > [index: number ]: string ; > } > > let myArray: StringArray > myArray = ['Bob' , 'Fred' ] > > let myStr:string = myArray[0 ] > > > class Animal { > name: string ; > } > > class Dog extends Animal { > breed: string ; > } > > interface NotOkay { > [x: number ]: Animal, > [x: string ]: Dog > } >
类类型 类具有两个类型: 静态部分的类型和实例的类型
1 2 3 4 5 6 7 8 9 10 11 12 > > interface ClockInterface { > currentTime: Date ; > } > > class Clock implements ClockInterface { > currentTime: Date ; > constructor (h:number , m:number ){} > } > > >
继承接口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 > interface Shape { > color: string ; > } > > interface PenStoke { > penWidth: number ; > } > > interface Square extends Shape, PenStoke { > sideLength: number ; > } > > let square = <Square>{} > square.color = 'red' ; > square.penWidth = 10 ; >
混合类型 一个对象可以同时作为函数和对象使用,并带有额外属性 1 2 3 4 5 6 7 8 9 10 11 12 13 > interface Counter { > (start: number ): string ; > interval: number ; > reset(): void ; > } > > function getCounter ( ): Counter { > let counter = <Counter>function (start: number ) {}; > counter.interval = 123 ; > counter.reset = function ( ) {}; > return counter; > } >
接口继承类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 > class Control { > private state: any ; > } > > interface SelectableControl extends Control { > select(): void ; > } > > class Button extends Control implements SelectableControl { > select() {} > } > > class TextBox extends Control { > select() {} > } > > > class Image implements SelectableControl { > select(){} > } >
三、类