rimo030 / type-challenges

Collection of TypeScript type challenges with online judge
https://tsch.js.org/
MIT License
3 stars 0 forks source link

no - 7544 Construct Tuple #133

Open rimo030 opened 4 months ago

rimo030 commented 4 months ago
type ConstructTuple<L extends number, R extends any[] = []> = R['length'] extends L
  ? R
  : ConstructTuple<L, [...R, unknown]>
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<ConstructTuple<0>, []>>,
  Expect<Equal<ConstructTuple<2>, [unknown, unknown]>>,
  Expect<Equal<ConstructTuple<999>['length'], 999>>,
  // @ts-expect-error
  Expect<Equal<ConstructTuple<1000>['length'], 1000>>,
]