forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
numeric.d.ts
168 lines (116 loc) · 4.03 KB
/
numeric.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
type Numeric = number | bigint;
type Zero = 0 | 0n;
/**
Matches the hidden `Infinity` type.
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
@see NegativeInfinity
@category Numeric
*/
// See https://github.com/microsoft/TypeScript/issues/31752
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
export type PositiveInfinity = 1e999;
/**
Matches the hidden `-Infinity` type.
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
@see PositiveInfinity
@category Numeric
*/
// See https://github.com/microsoft/TypeScript/issues/31752
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
export type NegativeInfinity = -1e999;
/**
A finite `number`.
You can't pass a `bigint` as they are already guaranteed to be finite.
Use-case: Validating and documenting parameters.
@example
```
import {Finite} from 'type-fest';
declare function setScore<T extends number>(length: Finite<T>): void;
```
@category Numeric
*/
export type Finite<T extends number> = T extends PositiveInfinity | NegativeInfinity ? never : T;
/**
A `number` that is an integer.
You can't pass a `bigint` as they are already guaranteed to be integers.
Use-case: Validating and documenting parameters.
@example
```
import {Integer} from 'type-fest';
declare function setYear<T extends number>(length: Integer<T>): void;
```
@see NegativeInteger
@see NonNegativeInteger
@category Numeric
*/
// `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1)
// Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points
export type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never;
/**
A `number` that is not an integer.
You can't pass a `bigint` as they are already guaranteed to be integers.
Use-case: Validating and documenting parameters.
@example
```
import {Float} from 'type-fest';
declare function setPercentage<T extends number>(length: Float<T>): void;
```
@see Integer
@category Numeric
*/
export type Float<T extends number> = T extends Integer<T> ? never : T;
/**
A negative (`-∞ < x < 0`) `number` that is not an integer.
Equivalent to `Negative<Float<T>>`.
Use-case: Validating and documenting parameters.
@see Negative
@see Float
@category Numeric
*/
export type NegativeFloat<T extends number> = Negative<Float<T>>;
/**
A negative `number`/`bigint` (`-∞ < x < 0`)
Use-case: Validating and documenting parameters.
@see NegativeInteger
@see NonNegative
@category Numeric
*/
export type Negative<T extends Numeric> = T extends Zero ? never : `${T}` extends `-${string}` ? T : never;
/**
A negative (`-∞ < x < 0`) `number` that is an integer.
Equivalent to `Negative<Integer<T>>`.
You can't pass a `bigint` as they are already guaranteed to be integers, instead use `Negative<T>`.
Use-case: Validating and documenting parameters.
@see Negative
@see Integer
@category Numeric
*/
export type NegativeInteger<T extends number> = Negative<Integer<T>>;
/**
A non-negative `number`/`bigint` (`0 <= x < ∞`).
Use-case: Validating and documenting parameters.
@see NonNegativeInteger
@see Negative
@example
```
import {NonNegative} from 'type-fest';
declare function setLength<T extends number>(length: NonNegative<T>): void;
```
@category Numeric
*/
export type NonNegative<T extends Numeric> = T extends Zero ? T : Negative<T> extends never ? T : never;
/**
A non-negative (`0 <= x < ∞`) `number` that is an integer.
Equivalent to `NonNegative<Integer<T>>`.
You can't pass a `bigint` as they are already guaranteed to be integers, instead use `NonNegative<T>`.
Use-case: Validating and documenting parameters.
@see NonNegative
@see Integer
@example
```
import {NonNegativeInteger} from 'type-fest';
declare function setLength<T extends number>(length: NonNegativeInteger<T>): void;
```
@category Numeric
*/
export type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>;