简体   繁体   English

TypeScript函数类型语法说明

[英]TypeScript function type syntax explanation

I'm reading the TypeScript Handbook and in the Functions section under the Function Types heading there is this example (which I understand): 我正在阅读《 TypeScript手册》,在“ 函数类型”标题下的“ 函数”部分中有以下示例(据我了解):

let myAdd = function(x: number, y: number): number { return x+y; };

This is followed by 接下来是

let's write the full type of the function out by looking at the each piece of the function type. 让我们通过查看函数类型的每一部分来写出函数的完整类型。

and this syntax: 和这个语法:

let myAdd: (x: number, y: number) => number =
    function(x: number, y: number): number { return x+y; };

Could someone break this down and explain it as I have never seen this before and can't find an explanation in the handbook? 有人可以将其分解并解释吗,就像我以前从未见过的一样,并且在手册中找不到解释?

This line: 这行:

let myAdd: (x: number, y: number) => number =
    function(x: number, y: number): number { return x+y; };

Consists of 3 parts: 包括3个部分:

(1) The variable declaration, this part is the let myAdd . (1)变量声明,这部分是let myAdd I assume that there's nothing to add here, it's just like with js. 我假设这里没有要添加的东西,就像js一样。

(2) The type of the variable: (x: number, y: number) => number . (2)变量的类型: (x: number, y: number) => number
Here we're defining a type of a function that expects two parameters, both of type number , named x and y . 在这里,我们定义了一个函数类型,该函数需要两个参数,两个参数的类型均为number ,分别名为xy
The function needs to return a number . 该函数需要返回一个number

(3) The assignment of the value to the variable: = function(x: number, y: number): number { return x+y; } (3)将值赋给变量: = function(x: number, y: number): number { return x+y; } = function(x: number, y: number): number { return x+y; } . = function(x: number, y: number): number { return x+y; }
This is just like javascript as well, except for the added types for the params and return value. 除了参数和返回值的添加类型外,这也与javascript一样。
If you look at it you'll see that the actual implementation matches the declared type perfectly. 如果您看一下它,就会发现实际的实现与声明的类型完全匹配。

You can also write it like this: 您也可以这样写:

let myAdd: (x: number, y: number) => number = function(x, y) { return x+y; };

Or: 要么:

let myAdd: (x: number, y: number) => number = (x, y) => { return x+y; };

The first line: 第一行:

let myAdd: (x: number, y: number) => number

Is declaring the type of the variable "myAdd". 声明变量“ myAdd”的类型。 Typescript can automatically infer this in the first example, alternatively (which the second example shows) you can implicitly tell Typescript what it should expect. Typescript可以在第一个示例中自动推断出来,或者(第二个示例显示),您可以隐式告诉Typescript它应该期望什么。

The second line: 第二行:

function(x: number, y: number): number { return x+y; };

Refers to the type of the function itself which you have assigned to the variable "myAdd". 指代已分配给变量“ myAdd”的函数本身的类型。

For a simpler example illustrating the same thing: 对于说明同一件事的更简单示例:

let myString: (input: string) => string = (input: string) => input;

Alternatively a different example of implicitly declaring the variables type: 或者,另一个示例隐式声明变量类型:

let myNumber: number = 10;

Both of the above tell Typescript what the variable should be. 以上两个都告诉Typescript变量应该是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM