UNPKG

5.12 kBTypeScriptView Raw
1import type {IsFloat} from './is-float';
2import type {IsInteger} from './is-integer';
3
4export type Numeric = number | bigint;
5
6export type Zero = 0 | 0n;
7
8/**
9Matches the hidden `Infinity` type.
10
11Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
12
13@see NegativeInfinity
14
15@category Numeric
16*/
17// See https://github.com/microsoft/TypeScript/issues/31752
18// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
19export type PositiveInfinity = 1e999;
20
21/**
22Matches the hidden `-Infinity` type.
23
24Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
25
26@see PositiveInfinity
27
28@category Numeric
29*/
30// See https://github.com/microsoft/TypeScript/issues/31752
31// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
32export type NegativeInfinity = -1e999;
33
34/**
35A finite `number`.
36You can't pass a `bigint` as they are already guaranteed to be finite.
37
38Use-case: Validating and documenting parameters.
39
40Note: This can't detect `NaN`, please upvote [this issue](https://github.com/microsoft/TypeScript/issues/28682) if you want to have this type as a built-in in TypeScript.
41
42@example
43```
44import type {Finite} from 'type-fest';
45
46declare function setScore<T extends number>(length: Finite<T>): void;
47```
48
49@category Numeric
50*/
51export type Finite<T extends number> = T extends PositiveInfinity | NegativeInfinity ? never : T;
52
53/**
54A `number` that is an integer.
55
56Use-case: Validating and documenting parameters.
57
58@example
59```
60type Integer = Integer<1>;
61//=> 1
62
63type IntegerWithDecimal = Integer<1.0>;
64//=> 1
65
66type NegativeInteger = Integer<-1>;
67//=> -1
68
69type Float = Integer<1.5>;
70//=> never
71
72// Supports non-decimal numbers
73
74type OctalInteger: Integer<0o10>;
75//=> 0o10
76
77type BinaryInteger: Integer<0b10>;
78//=> 0b10
79
80type HexadecimalInteger: Integer<0x10>;
81//=> 0x10
82```
83
84@example
85```
86import type {Integer} from 'type-fest';
87
88declare function setYear<T extends number>(length: Integer<T>): void;
89```
90
91@see NegativeInteger
92@see NonNegativeInteger
93
94@category Numeric
95*/
96// `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1)
97// Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points
98export type Integer<T> =
99 T extends unknown // To distributive type
100 ? IsInteger<T> extends true ? T : never
101 : never; // Never happens
102
103/**
104A `number` that is not an integer.
105
106Use-case: Validating and documenting parameters.
107
108It does not accept `Infinity`.
109
110@example
111```
112import type {Float} from 'type-fest';
113
114declare function setPercentage<T extends number>(length: Float<T>): void;
115```
116
117@see Integer
118
119@category Numeric
120*/
121export type Float<T> =
122T extends unknown // To distributive type
123 ? IsFloat<T> extends true ? T : never
124 : never; // Never happens
125
126/**
127A negative (`-∞ < x < 0`) `number` that is not an integer.
128Equivalent to `Negative<Float<T>>`.
129
130Use-case: Validating and documenting parameters.
131
132@see Negative
133@see Float
134
135@category Numeric
136*/
137export type NegativeFloat<T extends number> = Negative<Float<T>>;
138
139/**
140A negative `number`/`bigint` (`-∞ < x < 0`)
141
142Use-case: Validating and documenting parameters.
143
144@see NegativeInteger
145@see NonNegative
146
147@category Numeric
148*/
149export type Negative<T extends Numeric> = T extends Zero ? never : `${T}` extends `-${string}` ? T : never;
150
151/**
152A negative (`-∞ < x < 0`) `number` that is an integer.
153Equivalent to `Negative<Integer<T>>`.
154
155You can't pass a `bigint` as they are already guaranteed to be integers, instead use `Negative<T>`.
156
157Use-case: Validating and documenting parameters.
158
159@see Negative
160@see Integer
161
162@category Numeric
163*/
164export type NegativeInteger<T extends number> = Negative<Integer<T>>;
165
166/**
167A non-negative `number`/`bigint` (`0 <= x < ∞`).
168
169Use-case: Validating and documenting parameters.
170
171@see NonNegativeInteger
172@see Negative
173
174@example
175```
176import type {NonNegative} from 'type-fest';
177
178declare function setLength<T extends number>(length: NonNegative<T>): void;
179```
180
181@category Numeric
182*/
183export type NonNegative<T extends Numeric> = T extends Zero ? T : Negative<T> extends never ? T : never;
184
185/**
186A non-negative (`0 <= x < ∞`) `number` that is an integer.
187Equivalent to `NonNegative<Integer<T>>`.
188
189You can't pass a `bigint` as they are already guaranteed to be integers, instead use `NonNegative<T>`.
190
191Use-case: Validating and documenting parameters.
192
193@see NonNegative
194@see Integer
195
196@example
197```
198import type {NonNegativeInteger} from 'type-fest';
199
200declare function setLength<T extends number>(length: NonNegativeInteger<T>): void;
201```
202
203@category Numeric
204*/
205export type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>;
206
207/**
208Returns a boolean for whether the given number is a negative number.
209
210@see Negative
211
212@example
213```
214import type {IsNegative} from 'type-fest';
215
216type ShouldBeFalse = IsNegative<1>;
217type ShouldBeTrue = IsNegative<-1>;
218```
219
220@category Numeric
221*/
222export type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false;