当前位置:网站首页>【TS】函数类型
【TS】函数类型
2022-06-24 06:43:00 【一颗不甘坠落的流星】
一、函数类型
- 指定函数参数类型
// 函数声明(Function Declaration)
function sum(x:number, y:number) {
return x + y;
}
sum(1,2) // 正确
sum('1',2) // 报错,x、y类型必须都是设置的number类型
// 函数表达式(Function Expression)
let mySum = function (x:number, y:number) {
return x + y;
};
mySum(1,2) // 正确
mySum('1',2) // 报错,x、y类型必须都是设置的number类型
- 指定函数返回值类型
// 函数声明(Function Declaration)
function sum(x, y):number {
return x + y;
}
sum(1,2) // 正确
sum('1',2) // 报错,'1',2 进行的是字符串拼接,所以返回值是string,不是设置的number类型
// 函数表达式(Function Expression)
let mySum = function (x, y):number {
return x + y;
};
mySum(1,2) // 正确
mySum('1',2) // 报错,'1',2 进行的是字符串拼接,所以返回值是string,不是设置的number类型
二、参数设置
2.1、可选参数
- 输入多余的(或者少于要求的)参数,是不允许的。那么如何定义可选的参数呢?
- 我们用
?表示可选的参数
function buildName(firstName: string, lastName?: string) {
if (lastName) {
return firstName + ' ' + lastName;
} else {
return firstName;
}
}
let tomcat = buildName('Tom', 'Cat'); // 打印结果:'Tom Cat'
let tom = buildName('Tom'); // 打印结果:'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.
// 翻译:错误TS1016:一个必需的参数不能跟随一个可选参数
2.2、默认参数
- 在
ES6中,我们允许给函数的参数添加默认值。 - 而
TypeScript会将添加了默认值的参数识别为可选参数:
function buildName(firstName: string, lastName: string = 'Cat') {
return firstName + ' ' + lastName;
}
let tomcat = buildName('Tom', 'Caa'); // 打印结果:'Tom Caa'
let tom = buildName('Tom'); // 打印结果:'Tom Cat'
- 但是此时就不受「可选参数必须接在必需参数后面」的限制了:
function buildName(firstName: string = 'Tom', lastName: string) {
return firstName + ' ' + lastName;
}
let tomcat = buildName('Tom', 'Cat'); // 打印结果:'Tom Cat'
let cat = buildName(undefined, 'Cat'); // 打印结果:'Tom Cat'
2.3、剩余参数
ES6中,可以使用...rest的方式获取函数中的剩余参数(rest参数):
function push(array, ...items) {
items.forEach(function(item) {
array.push(item);
});
}
let a = [];
push(a, 1, 2, 3);
console.log(a) // [1, 2, 3]
- 事实上,items 是一个数组。所以我们可以用数组的类型来定义它:
function push(array: any[], ...items: any[]) {
items.forEach(function(item) {
array.push(item);
});
}
let a = [];
push(a, 1, 2, 3);
- 注意:
rest剩余参数只能是最后一个参数
边栏推荐
- [cloud based co creation] overview of the IOT of Huawei cloud HCIA IOT v2.5 training series
- JVM debugging tool -jstack
- Huawei cloud database advanced learning
- 【Proteus】Arduino UNO + DS1307+LCD1602时间显示
- 0 foundation a literature club low code development member management applet (5)
- Bay area enterprises quick look! The focus of the data regulations of Shenzhen Special Economic Zone just released is coming!
- Go operation SQLite code error
- Spark参数调优实践
- GPU frequency of zhanrui chip
- JVM调试工具-jmap
猜你喜欢

取模软件 模拟显示验证取模数据正确性 逆向 把点阵数组bin文件转显示

Unexpected token u in JSON at position 0

电脑如何打开软键盘,教大家Win10如何打开软键盘的方法

机器人迷雾之算力与智能

The data synchronization tool dataX has officially supported reading and writing tdengine
![[cloud based co creation] overview of the IOT of Huawei cloud HCIA IOT v2.5 training series](/img/80/1be6a87639ac8da41bc881b3341646.png)
[cloud based co creation] overview of the IOT of Huawei cloud HCIA IOT v2.5 training series

Decryption of the original divine square stone mechanism

Typora charges? Build vs Code markdown writing environment

原神方石机关解密

【Proteus】Arduino UNO + DS1307+LCD1602时间显示
随机推荐
You have a chance, here is a stage
Huawei cloud database advanced learning
How do I reinstall the system? How to install win10 system with USB flash disk?
JVM debugging tool -arthas
The cloud monitoring system hertzbeat V1.1.0 is released, and a command starts the monitoring journey!
FreeRTOS MPU使系统更健壮!
OMX的初始化流程
Graduation season advance technology
Laravel document reading notes -laravel str slug() function example
Spark project Packaging Optimization Practice
Leetcode概率题面试突击系列11~15
Another double win! Tencent's three security achievements were selected into the 2021 wechat independent innovation achievements recommendation manual
MFC使用控制台时 项目路径中不能有空格和中文,否则会报错误 LNK1342 未能保存要编辑的二进制文件的备份副本等
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures
年中了,准备了少量的自动化面试题,欢迎来自测
Spark accumulators and broadcast variables
App management platform app host
[Yugong series] June 2022 asp Basic introduction and use of cellreport reporting tool under net core
System design: partition or data partition
Spark累加器和廣播變量