1 | import type {IsFloat} from './is-float';
|
2 | import type {IsInteger} from './is-integer';
|
3 |
|
4 | export type Numeric = number | bigint;
|
5 |
|
6 | export type Zero = 0 | 0n;
|
7 |
|
8 | /**
|
9 | Matches the hidden `Infinity` type.
|
10 |
|
11 | Please 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
|
19 | export type PositiveInfinity = 1e999;
|
20 |
|
21 | /**
|
22 | Matches the hidden `-Infinity` type.
|
23 |
|
24 | Please 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
|
32 | export type NegativeInfinity = -1e999;
|
33 |
|
34 | /**
|
35 | A finite `number`.
|
36 | You can't pass a `bigint` as they are already guaranteed to be finite.
|
37 |
|
38 | Use-case: Validating and documenting parameters.
|
39 |
|
40 | Note: 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 | ```
|
44 | import type {Finite} from 'type-fest';
|
45 |
|
46 | declare function setScore<T extends number>(length: Finite<T>): void;
|
47 | ```
|
48 |
|
49 | @category Numeric
|
50 | */
|
51 | export type Finite<T extends number> = T extends PositiveInfinity | NegativeInfinity ? never : T;
|
52 |
|
53 | /**
|
54 | A `number` that is an integer.
|
55 |
|
56 | Use-case: Validating and documenting parameters.
|
57 |
|
58 | @example
|
59 | ```
|
60 | type Integer = Integer<1>;
|
61 | //=> 1
|
62 |
|
63 | type IntegerWithDecimal = Integer<1.0>;
|
64 | //=> 1
|
65 |
|
66 | type NegativeInteger = Integer<-1>;
|
67 | //=> -1
|
68 |
|
69 | type Float = Integer<1.5>;
|
70 | //=> never
|
71 |
|
72 | // Supports non-decimal numbers
|
73 |
|
74 | type OctalInteger: Integer<0o10>;
|
75 | //=> 0o10
|
76 |
|
77 | type BinaryInteger: Integer<0b10>;
|
78 | //=> 0b10
|
79 |
|
80 | type HexadecimalInteger: Integer<0x10>;
|
81 | //=> 0x10
|
82 | ```
|
83 |
|
84 | @example
|
85 | ```
|
86 | import type {Integer} from 'type-fest';
|
87 |
|
88 | declare 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
|
98 | export type Integer<T> =
|
99 | T extends unknown // To distributive type
|
100 | ? IsInteger<T> extends true ? T : never
|
101 | : never; // Never happens
|
102 |
|
103 | /**
|
104 | A `number` that is not an integer.
|
105 |
|
106 | Use-case: Validating and documenting parameters.
|
107 |
|
108 | It does not accept `Infinity`.
|
109 |
|
110 | @example
|
111 | ```
|
112 | import type {Float} from 'type-fest';
|
113 |
|
114 | declare function setPercentage<T extends number>(length: Float<T>): void;
|
115 | ```
|
116 |
|
117 | @see Integer
|
118 |
|
119 | @category Numeric
|
120 | */
|
121 | export type Float<T> =
|
122 | T extends unknown // To distributive type
|
123 | ? IsFloat<T> extends true ? T : never
|
124 | : never; // Never happens
|
125 |
|
126 | /**
|
127 | A negative (`-∞ < x < 0`) `number` that is not an integer.
|
128 | Equivalent to `Negative<Float<T>>`.
|
129 |
|
130 | Use-case: Validating and documenting parameters.
|
131 |
|
132 | @see Negative
|
133 | @see Float
|
134 |
|
135 | @category Numeric
|
136 | */
|
137 | export type NegativeFloat<T extends number> = Negative<Float<T>>;
|
138 |
|
139 | /**
|
140 | A negative `number`/`bigint` (`-∞ < x < 0`)
|
141 |
|
142 | Use-case: Validating and documenting parameters.
|
143 |
|
144 | @see NegativeInteger
|
145 | @see NonNegative
|
146 |
|
147 | @category Numeric
|
148 | */
|
149 | export type Negative<T extends Numeric> = T extends Zero ? never : `${T}` extends `-${string}` ? T : never;
|
150 |
|
151 | /**
|
152 | A negative (`-∞ < x < 0`) `number` that is an integer.
|
153 | Equivalent to `Negative<Integer<T>>`.
|
154 |
|
155 | You can't pass a `bigint` as they are already guaranteed to be integers, instead use `Negative<T>`.
|
156 |
|
157 | Use-case: Validating and documenting parameters.
|
158 |
|
159 | @see Negative
|
160 | @see Integer
|
161 |
|
162 | @category Numeric
|
163 | */
|
164 | export type NegativeInteger<T extends number> = Negative<Integer<T>>;
|
165 |
|
166 | /**
|
167 | A non-negative `number`/`bigint` (`0 <= x < ∞`).
|
168 |
|
169 | Use-case: Validating and documenting parameters.
|
170 |
|
171 | @see NonNegativeInteger
|
172 | @see Negative
|
173 |
|
174 | @example
|
175 | ```
|
176 | import type {NonNegative} from 'type-fest';
|
177 |
|
178 | declare function setLength<T extends number>(length: NonNegative<T>): void;
|
179 | ```
|
180 |
|
181 | @category Numeric
|
182 | */
|
183 | export type NonNegative<T extends Numeric> = T extends Zero ? T : Negative<T> extends never ? T : never;
|
184 |
|
185 | /**
|
186 | A non-negative (`0 <= x < ∞`) `number` that is an integer.
|
187 | Equivalent to `NonNegative<Integer<T>>`.
|
188 |
|
189 | You can't pass a `bigint` as they are already guaranteed to be integers, instead use `NonNegative<T>`.
|
190 |
|
191 | Use-case: Validating and documenting parameters.
|
192 |
|
193 | @see NonNegative
|
194 | @see Integer
|
195 |
|
196 | @example
|
197 | ```
|
198 | import type {NonNegativeInteger} from 'type-fest';
|
199 |
|
200 | declare function setLength<T extends number>(length: NonNegativeInteger<T>): void;
|
201 | ```
|
202 |
|
203 | @category Numeric
|
204 | */
|
205 | export type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>;
|
206 |
|
207 | /**
|
208 | Returns a boolean for whether the given number is a negative number.
|
209 |
|
210 | @see Negative
|
211 |
|
212 | @example
|
213 | ```
|
214 | import type {IsNegative} from 'type-fest';
|
215 |
|
216 | type ShouldBeFalse = IsNegative<1>;
|
217 | type ShouldBeTrue = IsNegative<-1>;
|
218 | ```
|
219 |
|
220 | @category Numeric
|
221 | */
|
222 | export type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false;
|