Open PorkSashimi opened 2 years ago
type NumberRange<T extends number, U extends number, RZ extends any[] = [], IsInRange extends boolean = false> = RZ['length'] extends U
? RZ[number]
: RZ['length'] extends MinusOne<T>
? NumberRange<T, U, [...RZ, U], true>
: IsInRange extends true
? NumberRange<T, U, [...RZ, RZ['length']], IsInRange>
: NumberRange<T, U, [...RZ, never], IsInRange>
type NewArray<T, U extends any[] = []> = U['length'] extends T ? U : NewArray<T, [...U, U['length']]>;
type NumberRange<L, H> = NewArray<H> extends [...NewArray<L>, ...infer R] ? [...R, H][number] : never;
type ConstructTuple<
L extends number,
A extends any[] = []
> = A["length"] extends L ? A : ConstructTuple<L, [unknown, ...A]>;
// type foo = ConstructTuple<3> -> // [unknown,unknown,unknown]
type NumberRange<
L extends number,
H,
U extends number = L,
F extends any[] = ConstructTuple<L>
> = F["length"] extends H
? U | H
: NumberRange<L, H, U | F["length"], [unknown, ...F]>;
type NumberRange<L, H, R extends unknown[] = []> = H extends R[0] ? R[number] : L extends R['length'] ? NumberRange<[any, ...R]['length'], H, [R['length'], ...R]> : NumberRange<L, H, [never, ...R]>