简体   繁体   English

在TypeScript中,`const func:(num:number)=> string = String;`是什么意思?

[英]What is the meaning of `const func: (num: number) => string = String;` in TypeScript?

I am reading 2ality blog post on TypeScript and I came across the following code - 我正在阅读关于TypeScript的2ality博客文章 ,我遇到了以下代码 -

const func: (num: number) => string = String;

I don't understand the meaning of this syntax. 我不明白这种语法的含义。

In TypeScript Playground , it compiles to - TypeScript Playground中 ,它编译为 -

var func = String;

From what I understand, func is a function which takes a parameter num and the function itself returns a string and the func variable is assigned to String so the whole function thing becomes useless which means the above thing could've been written as simple as - 根据我的理解, func是一个函数,它接受一个参数num ,函数本身返回一个stringfunc变量被赋值给String所以整个函数变得无用,这意味着上面的东西可以写成简单的 -

const func = String;

Am I correct? 我对么? Or am I understanding Typescript wrong? 还是我理解Typescript错了?

const func: (num: number) => string = String; const func:(num:number)=> string = String;

This means there is a variable func whose type is (num: number) => string with value as String class. 这意味着有一个变量func其类型为(num: number) => string ,其值为String类。

Now string constructor accepts a value and returns a string value. 现在,字符串构造函数接受一个值并返回一个字符串值。


For people that are confused between string and String 对于在stringString之间混淆的人

  • string : refers to string value. string :指字符串值。 So () => string means a function returning a string. So () => string表示返回字符串的函数。 In var a = 'abc' , a is a string var a = 'abc' ,a是一个字符串
  • String : Note capital S . String :注意资本S That refers to string constructor, which is a function. 这是指字符串构造函数,它是一个函数。

Following is the definition of String : 以下是String的定义: 在此输入图像描述

It's a function which converts a passed Number to a String . 它是一个将传递的Number转换为String的函数。

You can see that if you enter the following TypeScript: 您可以看到,如果输入以下TypeScript:

const func: (num: number) => string = String;
console.log(func(123));
console.log(typeof func(123));
console.log(typeof 123);

It compiles to: 它编译为:

var func = String;
console.log(func(123));
console.log(typeof func(123));
console.log(typeof 123);

And executing this code shows 123 , string and number : 执行此代码显示123stringnumber

 var func = String; console.log(func(123)); console.log(typeof func(123)); console.log(typeof 123); 

So, func in the above example is returning new String(num) , which is why 123 is a number, but func(123) is a string. 因此,上例中的func返回new String(num) ,这就是为什么123是数字,但func(123)是一个字符串。

It is basically saying func is a function (denoted by x => y ) which takes variable named num of type number as input, and returns a string . 它基本上是说func是一个函数(用x => y表示),它将变量名为num的类型number作为输入,并返回一个string In this case the value of function is assigned to the function String , which is a constructor function. 在这种情况下,函数的值被赋给函数String ,这是一个构造函数。

In the end, func is a new name given to String constructor such that it only accepts numeric input. 最后, func是一个赋予String构造func的新名称,因此它只接受数字输入。 So func(5) would be equivalent to String(5) and it would return "5". 因此func(5)将等效于String(5) ,它将返回“5”。

Notice that the string (num: number) => string denotes the schema of function being declared. 请注意,string (num: number) => string表示正在声明的函数的模式。

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

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