[英]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
,函数本身返回一个string
, func
变量被赋值给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
对于在
string
和String
之间混淆的人
string
: refers to string value. string
:指字符串值。 So () => string
means a function returning a string. () => 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. 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
: 执行此代码显示
123
, string
和number
:
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.