如何在打字稿元组中使用扩展运算符?

How to use the spread operator in a typescript tuple?

我想在不知道数组将有多少个值的情况下指定数组其余部分的外观。我怎样才能实现这样的目标? (游乐场版本 here):

type numStrArr = [number, ...string];

let arr: numStrArr = [1, 'hi', 'other string'];

但是,上面的代码在扩展运算符处抛出错误 Type expected.

我知道有一个 proposal 可以解决这个问题,但是我现在怎样才能实现类似的行为呢?

我知道这个问题已有 3 年历史,但现在支持该功能。您需要做的就是将类型更改为可以传播的类型。阅读更多相关信息 here:


// no more error...
type numStrArr = [number, ...string[]];