1.类型注解

function a(person: string) {}

调用a 传非 string 或 空编译报错,但依然编译完成。

类型:

boolean, string, number, number[], Array<number>,元组Tuple x: [nubmer, string], enum, any, void, null, undefined, never

enum Color {Red = 1, Green, Blue};
var c: Color = Color.Green; var d: string = Color[1];

类型断言:

“尖括号”语法:

let someValue: any = “this is a string”;

let strLength: number = (<string>someValue).length;

as语法: (jsx中只允许as语法)

let someValue: any = “this is a string”;

let strLength: number = (someValue as string).length;

2.接口 interface

interface SquareConfig {
color?: string;   // ?为可选属性
readonly width: number;   // reandonly 为只读属性
height: number;
}

函数类型

interface SearchFunc {
(source: string, subString: string): boolean; //定义参数 和 返回值
}

可索引的类型

interface StringArray {
[index: number]: string; // 字符串的数组
}

类类型

扩展接口

混合类型

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;
}

let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;

接口继承类

3.类

其实跟其他语言都差不多

待续。。。

Tagged with: