这篇文章我们来看一下TS里面的函数 函数声明 在 JavaScript 中,有两种常见的定义函数的方式——函数声明(Function Declaration)和函数表达式(Function Expression): // 函数声明(Function Declaration) function sum(x, y) { return x y; } // 函数表达式(Function Expression) let mySum = function (x, y) { return x y; }; 一个函数有输入和输出,要在 TypeScript 中对其进行约束,需要把输入和输出都考虑到,其中函数声明的类型定义较简单: function sum(x:number,y:number):number{ return x y } 注意,输入多余的或者少于要求的参数,是不被允许的 sum(1, 2, 3); // index.ts(4,1): error TS2346: Supplied parameters do not match any signature of call target. sum(1); // index.ts(4,1): error TS2346: Supplied parameters do not match any signature of call target. 函数表达式 如果要我们现在写一个对函数表达式(Function Expression)的定义,可能会写成这样 let mySun = function(x:number,y:number):number{ return x y } 这是可以通过编译的,不过事实上,上面的代码只对等号右侧的匿名函数进行了类型定义,而等号左边的 let mySum(x:number,y:number)=>number = function(x:number,y:number):number{ return x y} 注意不要混淆了 TypeScript 中的 在 TypeScript 的类型定义中, 在 ES6 中, 用接口定义函数的形状我们也可以使用接口的方式来定义一个函数需要符合的形状: interface SearchFunc{ (source:string,subString:string):boolean } let mySearch:SearchFunc; mySearch = function(source: string,subString:string){ return source.search(subString) !== -1 } 可选参数 前面提到,输入多余的(或者少于要求的)参数,是不允许的。那么如何定义可选的参数呢? 与接口中的可选属性类似,我们用 function buildName(firstName: string, lastName?: string) { if (lastName) { return firstName ' ' lastName; } else { return firstName; } } let tomcat = buildName('Tom', 'Cat'); let tom = buildName('Tom'); 注意:可选参数必须接在必需参数后面。换句话说,可选参数后面不允许再出现必须参数了 function buildName(firstName?: string, lastName: string) { if (firstName) { return firstName ' ' lastName; } else { return lastName; } } let tomcat = buildName('Tom', 'Cat'); let tom = buildName(undefined, 'Tom'); // index.ts(1,40): error TS1016: A required parameter cannot follow an optional parameter. 参数默认值在 ES6 中,我们允许给函数的参数添加默认值,TypeScript 会将添加了默认值的参数识别为可选参数: function buildName(firstName:string,lastName:string='Cat'){ return firstName ' ' lastName; } let tomcat = buildName('Tom', 'Cat'); let tom = buildName('Tom'); 此时就不受「可选参数必须接在必需参数后面」的限制了: function buildName(firstName: string = 'Tom', lastName: string) { return firstName ' ' lastName; } let tomcat = buildName('Tom', 'Cat'); let cat = buildName(undefined, 'Cat'); 剩余参数 ES6 中,可以使用 function push(array,...items){ items.forEach(function(item){ array.push(item) }) } let a = []; push(a,1,2,3) 事实上, function push(array:any[],...items:any[]){ items.forEach(function(item){ array.push(item); }) } let a = [] push(a,1,2,3) 注意:rest 参数只能是最后一个参数 重载:重载允许一个函数接受不同数量或类型的参数时,作出不同的处理。 比如,我们需要实现一个函数 利用联合类型,我们可以这么实现: function reverse(x: number | string): number | string { if (typeof x === 'number') { return Number(x.toString().split('').reverse().join('')); } else if (typeof x === 'string') { return x.split('').reverse().join(''); } } 然而这样有一个缺点,就是不能够精确的表达,输入为数字的时候,输出也应该为数字,输入为字符串的时候,输出也应该为字符串。 这时,我们可以使用重载定义多个 function reverse(x: number): number; function reverse(x: string): string; function reverse(x: number | string): number | string { if (typeof x === 'number') { return Number(x.toString().split('').reverse().join('')); } else if (typeof x === 'string') { return x.split('').reverse().join(''); } } 上例中,我们重复定义了多次函数 注意:TypeScript 会优先从最前面的函数定义开始匹配,所以多个函数定义如果有包含关系,需要优先把精确的定义写在前面。 原文链接:https://github.com/xcatliu/typescript-tutorial/blob/master/basics/type-of-function.md 来源:http://www./content-4-153451.html |
|