当前位置:网站首页>[TS] function type
[TS] function type
2022-06-24 07:19:00 【A meteor unwilling to fall】
List of articles
One 、 Function type
- Specify the function parameter type
// Function declaration (Function Declaration)
function sum(x:number, y:number) {
return x + y;
}
sum(1,2) // correct
sum('1',2) // Report errors ,x、y Type must be set number type
// Function expression (Function Expression)
let mySum = function (x:number, y:number) {
return x + y;
};
mySum(1,2) // correct
mySum('1',2) // Report errors ,x、y Type must be set number type
- Specifies the return value type of the function
// Function declaration (Function Declaration)
function sum(x, y):number {
return x + y;
}
sum(1,2) // correct
sum('1',2) // Report errors ,'1',2 String splicing , So the return value is string, It's not set up number type
// Function expression (Function Expression)
let mySum = function (x, y):number {
return x + y;
};
mySum(1,2) // correct
mySum('1',2) // Report errors ,'1',2 String splicing , So the return value is string, It's not set up number type
Two 、 Parameter setting
2.1、 Optional parameters
- Type in the extra ( Or less than required ) Parameters , It's not allowed . So how to define the optional parameters ?
- We use it
?Represents an optional parameter
function buildName(firstName: string, lastName?: string) {
if (lastName) {
return firstName + ' ' + lastName;
} else {
return firstName;
}
}
let tomcat = buildName('Tom', 'Cat'); // Print the results :'Tom Cat'
let tom = buildName('Tom'); // Print the results :'Tom '
- It should be noted that , Optional parameters must be followed by required parameters
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.
// translate : error TS1016: A required parameter cannot follow an optional parameter
2.2、 Default parameters
- stay
ES6in , We allow you to add default values to the parameters of a function . - and
TypeScriptParameters with added default values are recognized as optional parameters :
function buildName(firstName: string, lastName: string = 'Cat') {
return firstName + ' ' + lastName;
}
let tomcat = buildName('Tom', 'Caa'); // Print the results :'Tom Caa'
let tom = buildName('Tom'); // Print the results :'Tom Cat'
- But at this point 「 Optional parameters must be followed by required parameters 」 The limit of :
function buildName(firstName: string = 'Tom', lastName: string) {
return firstName + ' ' + lastName;
}
let tomcat = buildName('Tom', 'Cat'); // Print the results :'Tom Cat'
let cat = buildName(undefined, 'Cat'); // Print the results :'Tom Cat'
2.3、 The remaining parameters
ES6in , have access to...restGet the remaining parameters in the function (restParameters ):
function push(array, ...items) {
items.forEach(function(item) {
array.push(item);
});
}
let a = [];
push(a, 1, 2, 3);
console.log(a) // [1, 2, 3]
- in fact ,items Is an array . So we can define an array by its type :
function push(array: any[], ...items: any[]) {
items.forEach(function(item) {
array.push(item);
});
}
let a = [];
push(a, 1, 2, 3);
- Be careful :
restThe remaining parameter can only be the last parameter
边栏推荐
- Unexpected token u in JSON at position 0
- JVM debugging tool -jmap
- 现货黄金有哪些值得借鉴的心态
- 1. go deep into tidb: see tidb for the first time
- An example of MySQL accidental deletion recovery - using Myflash
- High energy ahead: Figure 18 shows you how to use the waterfall chart to visually reflect data changes
- Canal安装配置
- 【图像融合】基于NSST结合PCNN实现图像融合附matlab代码
- Computing power and intelligence of robot fog
- You have a chance, here is a stage
猜你喜欢
随机推荐
NVIDIA control panel does not open what is NVIDIA control panel
JVM debugging tool -jvisualvm
Huawei Cloud Database Advanced Learning
Summary of 2022 blue team HW elementary interview questions
超宽带脉冲定位方案,UWB精准定位技术,无线室内定位应用
【图像融合】基于NSST结合PCNN实现图像融合附matlab代码
Can the small fire Chunfeng tea make its debut by "keeping fit"?
Graduation season advance technology
基因检测,如何帮助患者对抗疾病?
【pointNet】基于pointNet的三维点云目标分类识别matlab仿真
Unexpected token u in JSON at position 0
What is the OSI seven layer model? What is the role of each layer?
学会使用楼宇控制系统BACnet网关没那么难
Laravel document reading notes -laravel str slug() function example
How do I reinstall the system? How to install win10 system with USB flash disk?
EasyDSS_ The dash version solves the problem that the RTSP source address cannot play the video stream
MFC使用控制台时 项目路径中不能有空格和中文,否则会报错误 LNK1342 未能保存要编辑的二进制文件的备份副本等
Arduino raised $32million to enter the enterprise market
Unexpected token u in JSON at position 0
面渣逆袭:MySQL六十六问,两万字+五十图详解








