function sum(x: number, y: number): number {
return x + y;
}
//输入多余的(或者少于要求的)参数,是不被允许的:
//sum(1, 2, 3); 报错
//sum(1); 报错
//在 TypeScript 的类型定义中,=>
//用来表示函数的定义,左边是输入类型,需要用括号括起来,右边是输出类型。
//可选参数 lastName?: string 可以传或者不传
//可选参数必须接在必需参数后面
function buildName(firstName: string, lastName?: string) {
if (lastName) {
return firstName + ' ' + lastName;
} else {
return firstName;
}
}
let tomcat = buildName('Tom', 'Cat');
let tom3 = buildName('Tom');
ts 函数的类型
猜你喜欢
转载自blog.csdn.net/weixin_38404899/article/details/103718472
今日推荐
周排行