Home > Back-end >  How to Create a type that is an array of number generated dynamically
How to Create a type that is an array of number generated dynamically

Time:01-08

I need to create a type that is an array from 0 to 255 without putting each value manually, something like

type Range = [0, 1, 2, ..., 255]

There is any solution or type function to achieve this ?

CodePudding user response:

If you need a type utils that will start from 0, you can use this:

type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N
  ? Acc[number]
  : Enumerate<N, [...Acc, Acc['length']]>


type RangeUntil255 = Enumerate<256>

source

  • Related