-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
numeric.d.ts
131 lines (118 loc) · 3.25 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
export declare type BigIntOrNumber = number | bigint;
export declare type Zero = 0 | 0n;
export declare type ParseInt<S extends string | BigIntOrNumber> = `${S}` extends
`${infer N extends number}` ? N : never;
/**
* 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 {@linkcode NegativeInfinity}
*/
export declare type PositiveInfinity = 1e999;
/**
* @see {@linkcode PositiveInfinity}
*/
export declare type NegativeInfinity = -1e999;
/**
* @category Numeric
*/
export declare type Finite<T extends BigIntOrNumber> = T extends
PositiveInfinity | NegativeInfinity ? never : T;
/**
* @category Numeric
*/
export declare type Infinite<T extends BigIntOrNumber> = T extends
PositiveInfinity | NegativeInfinity ? T : never;
/**
* @category Numeric
*/
export declare type Integer<T extends BigIntOrNumber> = `${T}` extends
`${number}` ? T : never;
/**
* @category Numeric
*/
export declare type Float<T extends BigIntOrNumber> = 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 {@linkcode Negative}
* @see {@linkcode Float}
*
* @category Numeric
*/
export declare type NegativeFloat<T extends BigIntOrNumber> = Negative<
Float<T>
>;
/**
* A negative `number`/`bigint` (`-∞ < x < 0`)
*
* Use-case: Validating and documenting parameters.
*
* @see {@linkcode NegativeInteger}
* @see {@linkcode NonNegative}
*
* @category Numeric
*/
export declare type Negative<T extends BigIntOrNumber> = 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 {@linkcode Negative}
* @see {@linkcode Integer}
*
* @category Numeric
*/
export declare type NegativeInteger<T extends BigIntOrNumber> = Negative<
Integer<T>
>;
/**
* A non-negative `number`/`bigint` (`0 <= x < ∞`).
*
* Use-case: Validating and documenting parameters.
*
* @see {@linkcode NonNegativeInteger}
* @see {@linkcode Negative}
*
* @example ```ts
* import type {NonNegative} from 'type-fest';
*
* declare function setLength<T extends BigIntOrNumber>(length: NonNegative<T>): void;
* ```
*
* @category Numeric
*/
export declare type NonNegative<T extends BigIntOrNumber> = 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 {@linkcode NonNegative}
* @see {@linkcode Integer}
* @example ```ts
* declare function setLength<T extends BigIntOrNumber>(length: NonNegativeInteger<T>): void;
* ```
* @category Numeric
*/
export declare type NonNegativeInteger<T extends BigIntOrNumber> = NonNegative<
Integer<T>
>;