简体   繁体   中英

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 :

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.

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. 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.

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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