mirror of
https://github.com/sindresorhus/type-fest.git
synced 2026-02-01 15:59:43 +00:00
Add IntClosedRange type (#992)
This commit is contained in:
parent
8df4fa75b7
commit
d7b692bb04
1
index.d.ts
vendored
1
index.d.ts
vendored
@ -106,6 +106,7 @@ export type {IsFloat} from './source/is-float';
|
||||
export type {TupleToUnion} from './source/tuple-to-union';
|
||||
export type {UnionToTuple} from './source/union-to-tuple';
|
||||
export type {IntRange} from './source/int-range';
|
||||
export type {IntClosedRange} from './source/int-closed-range';
|
||||
export type {IsEqual} from './source/is-equal';
|
||||
export type {
|
||||
IsLiteral,
|
||||
|
||||
@ -192,6 +192,8 @@ Click the type names for complete docs.
|
||||
- [`Spread`](source/spread.d.ts) - Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
|
||||
- [`IsEqual`](source/is-equal.d.ts) - Returns a boolean for whether the two given types are equal.
|
||||
- [`TaggedUnion`](source/tagged-union.d.ts) - Create a union of types that share a common discriminant property.
|
||||
- [`IntRange`](source/int-range.d.ts) - Generate a union of numbers (includes the start and excludes the end).
|
||||
- [`IntClosedRange`](source/int-closed-range.d.ts) - Generate a union of numbers (includes the start and the end).
|
||||
- [`IntRange`](source/int-range.d.ts) - Generate a union of numbers.
|
||||
- [`ArrayIndices`](source/array-indices.d.ts) - Provides valid indices for a constant array or tuple.
|
||||
- [`ArrayValues`](source/array-values.d.ts) - Provides all values for a constant array or tuple.
|
||||
|
||||
35
source/int-closed-range.d.ts
vendored
Normal file
35
source/int-closed-range.d.ts
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
import type {IntRange} from './int-range';
|
||||
import type {Sum} from './sum';
|
||||
|
||||
/**
|
||||
Generate a union of numbers.
|
||||
|
||||
The numbers are created from the given `Start` (inclusive) parameter to the given `End` (inclusive) parameter.
|
||||
|
||||
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`.
|
||||
|
||||
Note: `Start` or `End` must be non-negative and smaller than `999`.
|
||||
|
||||
Use-cases:
|
||||
1. This can be used to define a set of valid input/output values. for example:
|
||||
```
|
||||
type Age = IntClosedRange<0, 120>; //=> 0 | 1 | 2 | ... | 119 | 120
|
||||
type FontSize = IntClosedRange<10, 20>; //=> 10 | 11 | ... | 19 | 20
|
||||
type EvenNumber = IntClosedRange<0, 10, 2>; //=> 0 | 2 | 4 | 6 | 8 | 10
|
||||
```
|
||||
2. This can be used to define random numbers in a range. For example, `type RandomNumber = IntClosedRange<0, 100>;`
|
||||
|
||||
@example
|
||||
```
|
||||
import type {IntClosedRange} from 'type-fest';
|
||||
|
||||
// Create union type `0 | 1 | ... | 9`
|
||||
type ZeroToNine = IntClosedRange<0, 9>;
|
||||
|
||||
// Create union type `100 | 200 | 300 | ... | 900`
|
||||
type Hundreds = IntClosedRange<100, 900, 100>;
|
||||
```
|
||||
|
||||
@see IntRange
|
||||
*/
|
||||
export type IntClosedRange<Start extends number, End extends number, Skip extends number = 1> = IntRange<Start, Sum<End, 1>, Skip>;
|
||||
2
source/int-range.d.ts
vendored
2
source/int-range.d.ts
vendored
@ -29,6 +29,8 @@ type ZeroToNine = IntRange<0, 10>;
|
||||
// Create union type `100 | 200 | 300 | ... | 900`
|
||||
type Hundreds = IntRange<100, 901, 100>;
|
||||
```
|
||||
|
||||
@see IntClosedRange
|
||||
*/
|
||||
export type IntRange<Start extends number, End extends number, Step extends number = 1> = PrivateIntRange<Start, End, Step>;
|
||||
|
||||
|
||||
20
test-d/int-closed-range.ts
Normal file
20
test-d/int-closed-range.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import {expectType, expectAssignable} from 'tsd';
|
||||
import type {IntClosedRange} from '../source/int-closed-range';
|
||||
|
||||
declare const test: IntClosedRange<0, 5>;
|
||||
expectType<0 | 1 | 2 | 3 | 4 | 5>(test);
|
||||
|
||||
declare const startTest: IntClosedRange<5, 10>;
|
||||
expectType<5 | 6 | 7 | 8 | 9 | 10>(startTest);
|
||||
|
||||
declare const stepTest1: IntClosedRange<10, 20, 2>;
|
||||
expectType<10 | 12 | 14 | 16 | 18 | 20>(stepTest1);
|
||||
|
||||
// Test for step > end - start
|
||||
declare const stepTest2: IntClosedRange<10, 20, 100>;
|
||||
expectType<10>(stepTest2);
|
||||
|
||||
type Int0_998 = IntClosedRange<0, 998>;
|
||||
declare const maxNumberTest: Int0_998;
|
||||
expectAssignable<number>(maxNumberTest);
|
||||
expectAssignable<Int0_998>(998);
|
||||
Loading…
x
Reference in New Issue
Block a user