Add integer range `IntRange` function
brandonmcconnell opened this issue · comments
It could be useful to match a number between a set of number, which a range function could be useful for.
Definition
type Enumerate<N extends number, Acc extends number[] = []> = Acc['length'] extends N
? Acc[number]
: Enumerate<N, [...Acc, Acc['length']]>;
type IntRange<F extends number, T extends number> = Exclude<Enumerate<Simplify<T>>, Enumerate<F>> | T;
Usage
type CustomRange = IntRange<20, 300>; // -> 20 | 21 | ... many more ... | 300
Disclaimer & credit
This type is inspired (99% copied) from this StackOverflow answer by AlexG, with a slight modification to include the last number in the series (| T
). Without this change, IntRange<20, 300>
would match 20
–299
instead or 20
–300
.
Duplicates #9