1 | import type {IntRange} from './int-range';
|
2 | import type {Sum} from './sum';
|
3 |
|
4 | /**
|
5 | Generate a union of numbers.
|
6 |
|
7 | The numbers are created from the given `Start` (inclusive) parameter to the given `End` (inclusive) parameter.
|
8 |
|
9 | You skip over numbers using the `Step` parameter (defaults to `1`). For example, `IntClosedRange<0, 10, 2>` will create a union of `0 | 2 | 4 | 6 | 8 | 10`.
|
10 |
|
11 | Note: `Start` or `End` must be non-negative and smaller than `999`.
|
12 |
|
13 | Use-cases:
|
14 | 1. This can be used to define a set of valid input/output values. for example:
|
15 | ```
|
16 | type Age = IntClosedRange<0, 120>; //=> 0 | 1 | 2 | ... | 119 | 120
|
17 | type FontSize = IntClosedRange<10, 20>; //=> 10 | 11 | ... | 19 | 20
|
18 | type EvenNumber = IntClosedRange<0, 10, 2>; //=> 0 | 2 | 4 | 6 | 8 | 10
|
19 | ```
|
20 | 2. This can be used to define random numbers in a range. For example, `type RandomNumber = IntClosedRange<0, 100>;`
|
21 |
|
22 | @example
|
23 | ```
|
24 | import type {IntClosedRange} from 'type-fest';
|
25 |
|
26 | // Create union type `0 | 1 | ... | 9`
|
27 | type ZeroToNine = IntClosedRange<0, 9>;
|
28 |
|
29 | // Create union type `100 | 200 | 300 | ... | 900`
|
30 | type Hundreds = IntClosedRange<100, 900, 100>;
|
31 | ```
|
32 |
|
33 | @see IntRange
|
34 | */
|
35 | export type IntClosedRange<Start extends number, End extends number, Skip extends number = 1> = IntRange<Start, Sum<End, 1>, Skip>;
|