DEV Community

Discussion on: Announcing TypeScript 4.1

Collapse
 
danielrosenwasser profile image
Daniel Rosenwasser

It's not so hard to do this with conditional types now in TypeScript 4.1!

type TupleOf<T, Length extends number> =
  number extends Length ? T[] : TupleOfHelper<[], T, Length>

type TupleOfHelper<Arr extends unknown[], T, Length extends number> =
  Arr["length"] extends Length ? Arr : TupleOfHelper<[T, ...Arr], T, Length>;

type Yadda = TupleOf<string, 2>
Enter fullscreen mode Exit fullscreen mode