一、函数类型
function add(x: number, y:number): number{ return x + y; }let add = function (x:number, y:number):number{return x + y} //完整函数类型let add:(x:number,y:number) => number = function(x:number,y:number): number{return x + y}//可选参数 ?function add(x:number, y?:number){}//默认值function add(x:number, y=1){}//剩余参数 ...function add(x:number, ...y:string[]){}
二、this
//this参数 提供一个显式的 this参数interface Person { name: string; showName(this: Person): string; } let p: Person = { name: "小王", showName (this: Person){ return this.name; } }
三、重载
为同一个函数提供多个函数类型定义来进行函数重载
function pickCard(x: {suit: string; card: number; }[]): number; function pickCard(x: number): {suit: string; card: number; }; function pickCard(x): any {}
作者:大富_大贵
链接:https://www.jianshu.com/p/388cc441f4c3