-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.d.ts
183 lines (160 loc) · 5.07 KB
/
mod.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
declare type Maybe<T> = T | null | undefined;
declare type NotNullish<T> = T & {};
declare type Arrayable<T> = T | T[];
declare type LiteralUnion<U extends T, T extends {} = string> = U | (T & {});
declare type StringLiteralUnion<S extends string> = `${S}` extends
`${infer Str extends string}` ? Str | (string & {}) : never;
declare type BigIntOrNumber = number | bigint;
declare type Zero = 0 | 0n;
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}
*/
declare type PositiveInfinity = 1e999;
/**
* @see {@linkcode PositiveInfinity}
*/
declare type NegativeInfinity = -1e999;
/**
* @category Numeric
*/
declare type Finite<T extends BigIntOrNumber> = T extends
PositiveInfinity | NegativeInfinity ? never : T;
/**
* @category Numeric
*/
declare type Infinite<T extends BigIntOrNumber> = T extends
PositiveInfinity | NegativeInfinity ? T : never;
/**
* @category Numeric
*/
declare type Integer<T extends BigIntOrNumber> = `${T}` extends `${number}` ? T
: never;
/**
* @category Numeric
*/
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
*/
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
*/
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
*/
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
*/
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
*/
declare type NonNegativeInteger<T extends BigIntOrNumber> = NonNegative<
Integer<T>
>;
/**
* Remove the `readonly` directive from all of an object's properties.
*
* @note This only affects the top-most level of properties.
* @see {@linkcode DeepMutable} for a recursive version of this utility.
*/
declare type Mutable<T extends {}> = {
-readonly [K in keyof T]: T[K];
};
/**
* Recursively remove the `readonly` directive from all properties within an
* object and any of its children.
*/
declare type DeepMutable<T extends {}> = {
-readonly [K in keyof T]: T[K] extends object ? DeepMutable<T[K]> : T[K];
};
/**
* Recursively adds the `readonly` directive too all properties within an
* object and any child objects it may contain as well. A recursive version
* of the Readonly type that comes builtin with TypeScript.
*
* @see {@linkcode DeepMutable} if you need to remove the `readonly` directives
*/
declare type DeepReadonly<T extends {}> = {
readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K];
};
/**
* Recursively makes all properties of an object (and any objects within it)
* optional, adding the `?` directive. Essentially the recursive counterpart of
* TypeScript's builtin Partial type.
*/
declare type DeepPartial<T extends {}> = {
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
};
/**
* Recursively make all of an object's properties (and any objects within it)
* required, removing the `?` directive. Opposite of {@linkcode DeepPartial}.
* Essentially the recursive counterpart of TypeScript's builtin Required type.
*/
declare type DeepRequired<T extends {}> = {
[K in keyof T]-?: T[K] extends object ? DeepRequired<T[K]> : T[K];
};