3

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

crashmstr
  • 28,043
  • 9
  • 61
  • 79
Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

6

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

Lesiak
  • 22,088
  • 2
  • 41
  • 65