【发布时间】:2019-09-28 04:00:14
【问题描述】:
我有以下注释,在 typescript 3.5.3 版本中定义元组时收到错误消息。 可能是什么问题?
type Test = [string, number];
const xxx: Test = () => ['hello', 10];
我得到错误:
Type '() => (string | number)[]' is not assignable to type '[string, number]'.
【问题讨论】:
-
你里面为什么有
() =>?那是 lambda 语法。您正在尝试将函数分配给元组类型的变量。 -
我想要一个将一些数据作为元组返回的函数....有可能吗?
-
但是你说
Test是一个元组,而不是一个返回元组的函数,并且你有xxx作为类型Test。你想让xxx成为返回元组还是元组的函数? -
是的,请以什么方式
-
再一次,您希望 Test 是一个返回元组的函数类型,还是返回元组?哪一个?
标签: typescript