基础类型
let isDone: boolean = false; let decLiteral: number = 6; let name: string = "bob";
- 数组
let list: number[] = [1, 2, 3]; let list: Array<number> = [1, 2, 3];
函数类型
type in line
let add=function(x:number,y?:number):string{ return 'asdfasdf' } let add1=(x:string,y?:string):number=>{ return 21 }
interface
interface Foo { (a: string | number, b: string | number): string | number } let foo: Foo = (a, b) => { return a + b }
type
type Foo = (a: string, b: string) => string let foo: Foo = (a, b) => { return a + b } // or type Foo = { (a: strin, b: string): string } let foo: Foo = (a, b) => { return a + b }
函数重载
interface Foo { (a: string, b: string): string (a: number, b: number): number } let foo1: Foo = (a, b) => { return a + b }
function foo(a: string | number, b: string | number): string | number { return a + b } let a = foo('a', 'b') // 'ab': string let b = foo(0, 1) // 1: number let c = foo('a', 1) // error: No overload matches this call.
type interface 区别
- type 可以声明基本类型别名,联合类型,元组等类型
- interface 能够声明自动合并