简体   繁体   English

如何理解打字稿中类型定义中的`[0, 1, 2, ...0[]]`?

[英]How to understand `[0, 1, 2, ...0[]]` in type definition in typescript?

I see some type definition like this in Typescript: deep keyof of a nested object :我在Typescript: deep keyof of a nested object 中看到了一些这样的类型定义:

type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
    11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...0[]]

I can't understand what the last part ...0[] means.我不明白最后一部分...0[]是什么意思。

Is there a document for this?有这方面的文件吗? Thanks谢谢

You need two pieces of information to understand this type:您需要两条信息才能理解这种类型:

The last element of a tuple type can be a rest element of the form ...X , where X is an array type.元组类型的最后一个元素可以是...X形式的剩余元素,其中X是数组类型。 A rest element indicates that the tuple type is open-ended and may have zero or more additional elements of the array element type.其余元素表示元组类型是开放式的,并且可能具有零个或多个数组元素类型的附加元素。 For example, [number, ...string[]] means tuples with a number element followed by any number of string elements.例如, [number, ...string[]]表示具有number元素后跟任意数量的string元素的元组。

  • you can restrict a type to one constant value:您可以将类型限制为一个常量值:
const x: 0 = 0;
const y: 0 = 1; // ERROR: Type '1' is not assignable to type '0'.

Thus, the trailing ...0[] in tuple type means: any number of zeroes因此,元组类型中的尾随...0[]表示:任意数量的零

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

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