[英]How to assign arrow function return value to string[] in TypeScript
I have the following cast issue with the typescript code below: 我的以下打字稿代码存在以下强制转换问题:
Error: 错误:
Type '() => string[]' is missing the following properties from type 'string[]': pop, push, concat, join, and 24 more. 类型'()=> string []'缺少类型'string []'的以下属性:pop,push,concat,join和另外24个。 function stringArray(): string[]
函数stringArray():string []
function stringArray(): string[] {
return ['a', 'b', 'c']
}
let a: string[]
a = (): string[] => stringArray()
I simplified the code so nobody has to understand the actual problem I'm trying to solve. 我简化了代码,因此没有人需要理解我要解决的实际问题。 The main question is how can I define the return value of the arrow function to be accepted for the assignment of
a
. 主要的问题是我如何定义箭头函数的返回值进行的分配可以接受
a
。
This is happening because you are assigning an arrow function reference in the variable a
. 发生这种情况是因为您要在变量
a
中分配一个箭头函数引用。
function stringArray(): string[] {
return ['a', 'b', 'c'];
}
let a: Function;
a = (): string[] => stringArray();
Instead you should set the type as Function
相反,您应该将类型设置为
Function
Or you can invoke the arrow function to return the array ['a', 'b', 'c']
: 或者,您可以调用 arrow函数以返回数组
['a', 'b', 'c']
:
function stringArray(): string[] {
return ['a', 'b', 'c'];
}
let a: string[];
a = ((): string[] => stringArray())();
To make it simple: 为了简单起见:
function stringArray(): string[] {
return ['a', 'b', 'c'];
}
let a: string[];
a = stringArray();
You are assigning another arrow function to a. 您正在将另一个箭头功能分配给a。 To assign the return value to a, invoke the function in assignment.
要将返回值分配给a,请调用赋值函数。
let a: string[]
a = stringArray()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.