@visulima/package 3.6.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/LICENSE.md +0 -116
- package/README.md +153 -51
- package/dist/error.js +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -0
- package/dist/monorepo.js +1 -0
- package/dist/package-json.d.ts +3 -1
- package/dist/package-json.js +3 -0
- package/dist/package-manager.js +13 -0
- package/dist/package.js +1 -0
- package/dist/packem_shared/PackageNotFoundError-BictYTIA.js +1 -0
- package/dist/packem_shared/json-value.d-DU4PzU4Z.d.ts +33 -0
- package/dist/packem_shared/{package-json-KA2fTML0.d.cts → package-json-k3TJ_oy7.d.ts} +1056 -710
- package/dist/pnpm.d.ts +15 -0
- package/dist/pnpm.js +1 -0
- package/package.json +27 -78
- package/dist/error.cjs +0 -1
- package/dist/error.d.cts +0 -9
- package/dist/error.d.mts +0 -9
- package/dist/error.mjs +0 -1
- package/dist/index.cjs +0 -1
- package/dist/index.d.cts +0 -7
- package/dist/index.d.mts +0 -7
- package/dist/index.mjs +0 -1
- package/dist/monorepo.cjs +0 -1
- package/dist/monorepo.d.cts +0 -9
- package/dist/monorepo.d.mts +0 -9
- package/dist/monorepo.mjs +0 -1
- package/dist/package-json.cjs +0 -3
- package/dist/package-json.d.cts +0 -3
- package/dist/package-json.d.mts +0 -3
- package/dist/package-json.mjs +0 -3
- package/dist/package-manager.cjs +0 -13
- package/dist/package-manager.d.cts +0 -21
- package/dist/package-manager.d.mts +0 -21
- package/dist/package-manager.mjs +0 -13
- package/dist/package.cjs +0 -1
- package/dist/package.d.cts +0 -4
- package/dist/package.d.mts +0 -4
- package/dist/package.mjs +0 -1
- package/dist/packem_shared/PackageNotFoundError-CEETCi0X.mjs +0 -1
- package/dist/packem_shared/PackageNotFoundError-CY57YCot.cjs +0 -1
- package/dist/packem_shared/package-json-KA2fTML0.d.mts +0 -2405
- package/dist/packem_shared/package-json-KA2fTML0.d.ts +0 -2405
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { WriteJsonOptions } from '@visulima/fs';
|
|
2
|
+
import { a as JsonValue, J as JsonObject } from './json-value.d-DU4PzU4Z.js';
|
|
3
|
+
import { InstallPackageOptions } from '@antfu/install-pkg';
|
|
2
4
|
import { Package } from 'normalize-package-data';
|
|
3
5
|
|
|
4
6
|
/**
|
|
@@ -16,73 +18,82 @@ type Primitive =
|
|
|
16
18
|
| bigint;
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
|
-
|
|
21
|
+
Returns a boolean for whether the given type is `any`.
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
@link https://stackoverflow.com/a/49928360/1490091
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
*/
|
|
25
|
-
type JsonObject = {[Key in string]: JsonValue} & {[Key in string]?: JsonValue | undefined};
|
|
25
|
+
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
@example
|
|
28
|
+
```
|
|
29
|
+
import type {IsAny} from 'type-fest';
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
type JsonArray = JsonValue[] | readonly JsonValue[];
|
|
31
|
+
const typedObject = {a: 1, b: 2} as const;
|
|
32
|
+
const anyObject: any = {a: 1, b: 2};
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
|
|
35
|
+
return obj[key];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const typedA = get(typedObject, 'a');
|
|
39
|
+
//=> 1
|
|
40
|
+
|
|
41
|
+
const anyA = get(anyObject, 'a');
|
|
42
|
+
//=> any
|
|
43
|
+
```
|
|
36
44
|
|
|
37
|
-
@category
|
|
45
|
+
@category Type Guard
|
|
46
|
+
@category Utilities
|
|
38
47
|
*/
|
|
39
|
-
type
|
|
48
|
+
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
40
49
|
|
|
41
50
|
/**
|
|
42
|
-
|
|
51
|
+
Returns a boolean for whether the given key is an optional key of type.
|
|
43
52
|
|
|
44
|
-
|
|
53
|
+
This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
|
|
45
54
|
|
|
46
|
-
@
|
|
47
|
-
|
|
48
|
-
type
|
|
55
|
+
@example
|
|
56
|
+
```
|
|
57
|
+
import type {IsOptionalKeyOf} from 'type-fest';
|
|
49
58
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
interface User {
|
|
60
|
+
name: string;
|
|
61
|
+
surname: string;
|
|
62
|
+
|
|
63
|
+
luckyNumber?: number;
|
|
55
64
|
}
|
|
56
65
|
|
|
57
|
-
|
|
66
|
+
interface Admin {
|
|
67
|
+
name: string;
|
|
68
|
+
surname?: string;
|
|
69
|
+
}
|
|
58
70
|
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
|
|
72
|
+
//=> true
|
|
61
73
|
|
|
62
|
-
|
|
74
|
+
type T2 = IsOptionalKeyOf<User, 'name'>;
|
|
75
|
+
//=> false
|
|
63
76
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
import type {EmptyObject} from 'type-fest';
|
|
77
|
+
type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
|
|
78
|
+
//=> boolean
|
|
67
79
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const foo2: {} = []; // Pass
|
|
71
|
-
const foo3: {} = 42; // Pass
|
|
72
|
-
const foo4: {} = {a: 1}; // Pass
|
|
80
|
+
type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
|
|
81
|
+
//=> false
|
|
73
82
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const bar2: EmptyObject = 42; // Fail
|
|
77
|
-
const bar3: EmptyObject = []; // Fail
|
|
78
|
-
const bar4: EmptyObject = {a: 1}; // Fail
|
|
83
|
+
type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
|
|
84
|
+
//=> boolean
|
|
79
85
|
```
|
|
80
86
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
@category Object
|
|
87
|
+
@category Type Guard
|
|
88
|
+
@category Utilities
|
|
84
89
|
*/
|
|
85
|
-
type
|
|
90
|
+
type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
|
|
91
|
+
IsAny<Type | Key> extends true ? never
|
|
92
|
+
: Key extends keyof Type
|
|
93
|
+
? Type extends Record<Key, Type[Key]>
|
|
94
|
+
? false
|
|
95
|
+
: true
|
|
96
|
+
: false;
|
|
86
97
|
|
|
87
98
|
/**
|
|
88
99
|
Extract all optional keys from the given type.
|
|
@@ -117,11 +128,14 @@ const update2: UpdateOperation<User> = {
|
|
|
117
128
|
|
|
118
129
|
@category Utilities
|
|
119
130
|
*/
|
|
120
|
-
type OptionalKeysOf<
|
|
121
|
-
|
|
122
|
-
? (keyof {
|
|
123
|
-
|
|
124
|
-
|
|
131
|
+
type OptionalKeysOf<Type extends object> =
|
|
132
|
+
Type extends unknown // For distributing `Type`
|
|
133
|
+
? (keyof {[Key in keyof Type as
|
|
134
|
+
IsOptionalKeyOf<Type, Key> extends false
|
|
135
|
+
? never
|
|
136
|
+
: Key
|
|
137
|
+
]: never
|
|
138
|
+
}) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
|
|
125
139
|
: never; // Should never happen
|
|
126
140
|
|
|
127
141
|
/**
|
|
@@ -148,9 +162,9 @@ const validator2 = createValidation<User>('surname', value => value.length < 25)
|
|
|
148
162
|
|
|
149
163
|
@category Utilities
|
|
150
164
|
*/
|
|
151
|
-
type RequiredKeysOf<
|
|
152
|
-
|
|
153
|
-
? Exclude<keyof
|
|
165
|
+
type RequiredKeysOf<Type extends object> =
|
|
166
|
+
Type extends unknown // For distributing `Type`
|
|
167
|
+
? Exclude<keyof Type, OptionalKeysOf<Type>>
|
|
154
168
|
: never; // Should never happen
|
|
155
169
|
|
|
156
170
|
/**
|
|
@@ -197,27 +211,68 @@ endIfEqual('abc', '123');
|
|
|
197
211
|
type IsNever<T> = [T] extends [never] ? true : false;
|
|
198
212
|
|
|
199
213
|
/**
|
|
200
|
-
An if-else-like type that resolves depending on whether the given type is `
|
|
214
|
+
An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
|
|
201
215
|
|
|
202
|
-
|
|
216
|
+
Use-cases:
|
|
217
|
+
- You can use this in combination with `Is*` types to create an if-else-like experience. For example, `If<IsAny<any>, 'is any', 'not any'>`.
|
|
218
|
+
|
|
219
|
+
Note:
|
|
220
|
+
- Returns a union of if branch and else branch if the given type is `boolean` or `any`. For example, `If<boolean, 'Y', 'N'>` will return `'Y' | 'N'`.
|
|
221
|
+
- Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
|
|
203
222
|
|
|
204
223
|
@example
|
|
205
224
|
```
|
|
206
|
-
import
|
|
225
|
+
import {If} from 'type-fest';
|
|
207
226
|
|
|
208
|
-
type
|
|
209
|
-
//=>
|
|
227
|
+
type A = If<true, 'yes', 'no'>;
|
|
228
|
+
//=> 'yes'
|
|
229
|
+
|
|
230
|
+
type B = If<false, 'yes', 'no'>;
|
|
231
|
+
//=> 'no'
|
|
232
|
+
|
|
233
|
+
type C = If<boolean, 'yes', 'no'>;
|
|
234
|
+
//=> 'yes' | 'no'
|
|
235
|
+
|
|
236
|
+
type D = If<any, 'yes', 'no'>;
|
|
237
|
+
//=> 'yes' | 'no'
|
|
238
|
+
|
|
239
|
+
type E = If<never, 'yes', 'no'>;
|
|
240
|
+
//=> 'no'
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
@example
|
|
244
|
+
```
|
|
245
|
+
import {If, IsAny, IsNever} from 'type-fest';
|
|
246
|
+
|
|
247
|
+
type A = If<IsAny<unknown>, 'is any', 'not any'>;
|
|
248
|
+
//=> 'not any'
|
|
249
|
+
|
|
250
|
+
type B = If<IsNever<never>, 'is never', 'not never'>;
|
|
251
|
+
//=> 'is never'
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
@example
|
|
255
|
+
```
|
|
256
|
+
import {If, IsEqual} from 'type-fest';
|
|
257
|
+
|
|
258
|
+
type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
|
|
259
|
+
|
|
260
|
+
type A = IfEqual<string, string, 'equal', 'not equal'>;
|
|
261
|
+
//=> 'equal'
|
|
210
262
|
|
|
211
|
-
type
|
|
212
|
-
//=> '
|
|
263
|
+
type B = IfEqual<string, number, 'equal', 'not equal'>;
|
|
264
|
+
//=> 'not equal'
|
|
213
265
|
```
|
|
214
266
|
|
|
215
267
|
@category Type Guard
|
|
216
268
|
@category Utilities
|
|
217
269
|
*/
|
|
218
|
-
type
|
|
219
|
-
IsNever<
|
|
220
|
-
|
|
270
|
+
type If<Type extends boolean, IfBranch, ElseBranch> =
|
|
271
|
+
IsNever<Type> extends true
|
|
272
|
+
? ElseBranch
|
|
273
|
+
: Type extends true
|
|
274
|
+
? IfBranch
|
|
275
|
+
: ElseBranch;
|
|
221
276
|
|
|
222
277
|
/**
|
|
223
278
|
Represents an array with `unknown` value.
|
|
@@ -245,6 +300,89 @@ type C = IsArray<string>;
|
|
|
245
300
|
*/
|
|
246
301
|
type UnknownArray = readonly unknown[];
|
|
247
302
|
|
|
303
|
+
/**
|
|
304
|
+
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
305
|
+
*/
|
|
306
|
+
type BuiltIns = Primitive | void | Date | RegExp;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
Matches non-recursive types.
|
|
310
|
+
*/
|
|
311
|
+
type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown);
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
Returns a boolean for whether the given `boolean` is not `false`.
|
|
315
|
+
*/
|
|
316
|
+
type IsNotFalse<T extends boolean> = [T] extends [false] ? false : true;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
Returns a boolean for whether A is false.
|
|
320
|
+
|
|
321
|
+
@example
|
|
322
|
+
```
|
|
323
|
+
Not<true>;
|
|
324
|
+
//=> false
|
|
325
|
+
|
|
326
|
+
Not<false>;
|
|
327
|
+
//=> true
|
|
328
|
+
```
|
|
329
|
+
*/
|
|
330
|
+
type Not<A extends boolean> = A extends true
|
|
331
|
+
? false
|
|
332
|
+
: A extends false
|
|
333
|
+
? true
|
|
334
|
+
: never;
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
An if-else-like type that resolves depending on whether the given type is `any` or `never`.
|
|
338
|
+
|
|
339
|
+
@example
|
|
340
|
+
```
|
|
341
|
+
// When `T` is a NOT `any` or `never` (like `string`) => Returns `IfNotAnyOrNever` branch
|
|
342
|
+
type A = IfNotAnyOrNever<string, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
343
|
+
//=> 'VALID'
|
|
344
|
+
|
|
345
|
+
// When `T` is `any` => Returns `IfAny` branch
|
|
346
|
+
type B = IfNotAnyOrNever<any, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
347
|
+
//=> 'IS_ANY'
|
|
348
|
+
|
|
349
|
+
// When `T` is `never` => Returns `IfNever` branch
|
|
350
|
+
type C = IfNotAnyOrNever<never, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
351
|
+
//=> 'IS_NEVER'
|
|
352
|
+
```
|
|
353
|
+
*/
|
|
354
|
+
type IfNotAnyOrNever<T, IfNotAnyOrNever, IfAny = any, IfNever = never> =
|
|
355
|
+
If<IsAny<T>, IfAny, If<IsNever<T>, IfNever, IfNotAnyOrNever>>;
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
Returns a boolean for whether the given type is `any` or `never`.
|
|
359
|
+
|
|
360
|
+
This type can be better to use than {@link IfNotAnyOrNever `IfNotAnyOrNever`} in recursive types because it does not evaluate any branches.
|
|
361
|
+
|
|
362
|
+
@example
|
|
363
|
+
```
|
|
364
|
+
// When `T` is a NOT `any` or `never` (like `string`) => Returns `false`
|
|
365
|
+
type A = IsAnyOrNever<string>;
|
|
366
|
+
//=> false
|
|
367
|
+
|
|
368
|
+
// When `T` is `any` => Returns `true`
|
|
369
|
+
type B = IsAnyOrNever<any>;
|
|
370
|
+
//=> true
|
|
371
|
+
|
|
372
|
+
// When `T` is `never` => Returns `true`
|
|
373
|
+
type C = IsAnyOrNever<never>;
|
|
374
|
+
//=> true
|
|
375
|
+
```
|
|
376
|
+
*/
|
|
377
|
+
type IsAnyOrNever<T> = IsNotFalse<IsAny<T> | IsNever<T>>;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
Indicates the value of `exactOptionalPropertyTypes` compiler option.
|
|
381
|
+
*/
|
|
382
|
+
type IsExactOptionalPropertyTypesEnabled = [(string | undefined)?] extends [string?]
|
|
383
|
+
? false
|
|
384
|
+
: true;
|
|
385
|
+
|
|
248
386
|
/**
|
|
249
387
|
Returns the static, fixed-length portion of the given array, excluding variable-length parts.
|
|
250
388
|
|
|
@@ -281,39 +419,66 @@ type VariablePartOfArray<T extends UnknownArray> =
|
|
|
281
419
|
: []
|
|
282
420
|
: never;
|
|
283
421
|
|
|
284
|
-
// Can eventually be replaced with the built-in once this library supports
|
|
285
|
-
// TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
|
|
286
|
-
type NoInfer<T> = T extends infer U ? U : never;
|
|
287
|
-
|
|
288
422
|
/**
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
@link https://stackoverflow.com/a/49928360/1490091
|
|
292
|
-
|
|
293
|
-
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
423
|
+
Transforms a tuple type by replacing it's rest element with a single element that has the same type as the rest element, while keeping all the non-rest elements intact.
|
|
294
424
|
|
|
295
425
|
@example
|
|
296
426
|
```
|
|
297
|
-
|
|
427
|
+
type A = CollapseRestElement<[string, string, ...number[]]>;
|
|
428
|
+
//=> [string, string, number]
|
|
298
429
|
|
|
299
|
-
|
|
300
|
-
|
|
430
|
+
type B = CollapseRestElement<[...string[], number, number]>;
|
|
431
|
+
//=> [string, number, number]
|
|
301
432
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
433
|
+
type C = CollapseRestElement<[string, string, ...Array<number | bigint>]>;
|
|
434
|
+
//=> [string, string, number | bigint]
|
|
305
435
|
|
|
306
|
-
|
|
307
|
-
//=>
|
|
436
|
+
type D = CollapseRestElement<[string, number]>;
|
|
437
|
+
//=> [string, number]
|
|
438
|
+
```
|
|
308
439
|
|
|
309
|
-
|
|
310
|
-
|
|
440
|
+
Note: Optional modifiers (`?`) are removed from elements unless the `exactOptionalPropertyTypes` compiler option is disabled. When disabled, there's an additional `| undefined` for optional elements.
|
|
441
|
+
|
|
442
|
+
@example
|
|
311
443
|
```
|
|
444
|
+
// `exactOptionalPropertyTypes` enabled
|
|
445
|
+
type A = CollapseRestElement<[string?, string?, ...number[]]>;
|
|
446
|
+
//=> [string, string, number]
|
|
312
447
|
|
|
313
|
-
|
|
314
|
-
|
|
448
|
+
// `exactOptionalPropertyTypes` disabled
|
|
449
|
+
type B = CollapseRestElement<[string?, string?, ...number[]]>;
|
|
450
|
+
//=> [string | undefined, string | undefined, number]
|
|
451
|
+
```
|
|
315
452
|
*/
|
|
316
|
-
type
|
|
453
|
+
type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, _CollapseRestElement<TArray>>;
|
|
454
|
+
|
|
455
|
+
type _CollapseRestElement<
|
|
456
|
+
TArray extends UnknownArray,
|
|
457
|
+
ForwardAccumulator extends UnknownArray = [],
|
|
458
|
+
BackwardAccumulator extends UnknownArray = [],
|
|
459
|
+
> =
|
|
460
|
+
TArray extends UnknownArray // For distributing `TArray`
|
|
461
|
+
? keyof TArray & `${number}` extends never
|
|
462
|
+
// Enters this branch, if `TArray` is empty (e.g., []),
|
|
463
|
+
// or `TArray` contains no non-rest elements preceding the rest element (e.g., `[...string[]]` or `[...string[], string]`).
|
|
464
|
+
? TArray extends readonly [...infer Rest, infer Last]
|
|
465
|
+
? _CollapseRestElement<Rest, ForwardAccumulator, [Last, ...BackwardAccumulator]> // Accumulate elements that are present after the rest element.
|
|
466
|
+
: TArray extends readonly []
|
|
467
|
+
? [...ForwardAccumulator, ...BackwardAccumulator]
|
|
468
|
+
: [...ForwardAccumulator, TArray[number], ...BackwardAccumulator] // Add the rest element between the accumulated elements.
|
|
469
|
+
: TArray extends readonly [(infer First)?, ...infer Rest]
|
|
470
|
+
? _CollapseRestElement<
|
|
471
|
+
Rest,
|
|
472
|
+
[
|
|
473
|
+
...ForwardAccumulator,
|
|
474
|
+
'0' extends OptionalKeysOf<TArray>
|
|
475
|
+
? If<IsExactOptionalPropertyTypesEnabled, First, First | undefined> // Add `| undefined` for optional elements, if `exactOptionalPropertyTypes` is disabled.
|
|
476
|
+
: First,
|
|
477
|
+
],
|
|
478
|
+
BackwardAccumulator
|
|
479
|
+
>
|
|
480
|
+
: never // Should never happen, since `[(infer First)?, ...infer Rest]` is a top-type for arrays.
|
|
481
|
+
: never; // Should never happen
|
|
317
482
|
|
|
318
483
|
type Numeric = number | bigint;
|
|
319
484
|
|
|
@@ -329,7 +494,7 @@ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277)
|
|
|
329
494
|
@category Numeric
|
|
330
495
|
*/
|
|
331
496
|
// See https://github.com/microsoft/TypeScript/issues/31752
|
|
332
|
-
// eslint-disable-next-line
|
|
497
|
+
// eslint-disable-next-line no-loss-of-precision
|
|
333
498
|
type PositiveInfinity = 1e999;
|
|
334
499
|
|
|
335
500
|
/**
|
|
@@ -342,7 +507,7 @@ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277)
|
|
|
342
507
|
@category Numeric
|
|
343
508
|
*/
|
|
344
509
|
// See https://github.com/microsoft/TypeScript/issues/31752
|
|
345
|
-
// eslint-disable-next-line
|
|
510
|
+
// eslint-disable-next-line no-loss-of-precision
|
|
346
511
|
type NegativeInfinity = -1e999;
|
|
347
512
|
|
|
348
513
|
/**
|
|
@@ -407,737 +572,918 @@ type IsEqual<A, B> =
|
|
|
407
572
|
: false;
|
|
408
573
|
|
|
409
574
|
/**
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
|
|
575
|
+
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
413
576
|
|
|
414
577
|
@example
|
|
415
578
|
```
|
|
416
|
-
import type {
|
|
579
|
+
import type {Simplify} from 'type-fest';
|
|
417
580
|
|
|
418
|
-
|
|
419
|
-
|
|
581
|
+
type PositionProps = {
|
|
582
|
+
top: number;
|
|
583
|
+
left: number;
|
|
584
|
+
};
|
|
420
585
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
586
|
+
type SizeProps = {
|
|
587
|
+
width: number;
|
|
588
|
+
height: number;
|
|
589
|
+
};
|
|
424
590
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
? true
|
|
429
|
-
: true extends [IsEqual<A, false>, IsEqual<B, false>][number]
|
|
430
|
-
? false
|
|
431
|
-
: never;
|
|
591
|
+
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
592
|
+
type Props = Simplify<PositionProps & SizeProps>;
|
|
593
|
+
```
|
|
432
594
|
|
|
433
|
-
|
|
434
|
-
Returns a boolean for whether either of two given types are true.
|
|
595
|
+
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
|
|
435
596
|
|
|
436
|
-
|
|
597
|
+
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
437
598
|
|
|
438
599
|
@example
|
|
439
600
|
```
|
|
440
|
-
import type {
|
|
601
|
+
import type {Simplify} from 'type-fest';
|
|
441
602
|
|
|
442
|
-
|
|
443
|
-
|
|
603
|
+
interface SomeInterface {
|
|
604
|
+
foo: number;
|
|
605
|
+
bar?: string;
|
|
606
|
+
baz: number | undefined;
|
|
607
|
+
}
|
|
444
608
|
|
|
445
|
-
|
|
446
|
-
|
|
609
|
+
type SomeType = {
|
|
610
|
+
foo: number;
|
|
611
|
+
bar?: string;
|
|
612
|
+
baz: number | undefined;
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
616
|
+
const someType: SomeType = literal;
|
|
617
|
+
const someInterface: SomeInterface = literal;
|
|
618
|
+
|
|
619
|
+
function fn(object: Record<string, unknown>): void {}
|
|
620
|
+
|
|
621
|
+
fn(literal); // Good: literal object type is sealed
|
|
622
|
+
fn(someType); // Good: type is sealed
|
|
623
|
+
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
624
|
+
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
447
625
|
```
|
|
448
626
|
|
|
449
|
-
@
|
|
627
|
+
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
628
|
+
@see SimplifyDeep
|
|
629
|
+
@category Object
|
|
450
630
|
*/
|
|
451
|
-
type
|
|
452
|
-
? false
|
|
453
|
-
: true extends [IsEqual<A, true>, IsEqual<B, true>][number]
|
|
454
|
-
? true
|
|
455
|
-
: never;
|
|
631
|
+
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
456
632
|
|
|
457
633
|
/**
|
|
458
|
-
|
|
634
|
+
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
635
|
+
|
|
636
|
+
This is the counterpart of `PickIndexSignature`.
|
|
637
|
+
|
|
638
|
+
Use-cases:
|
|
639
|
+
- Remove overly permissive signatures from third-party types.
|
|
640
|
+
|
|
641
|
+
This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
|
|
642
|
+
|
|
643
|
+
It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
|
|
644
|
+
|
|
645
|
+
(The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
|
|
459
646
|
|
|
460
|
-
@example
|
|
461
647
|
```
|
|
462
|
-
|
|
648
|
+
const indexed: Record<string, unknown> = {}; // Allowed
|
|
463
649
|
|
|
464
|
-
|
|
465
|
-
|
|
650
|
+
const keyed: Record<'foo', unknown> = {}; // Error
|
|
651
|
+
// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
652
|
+
```
|
|
466
653
|
|
|
467
|
-
|
|
468
|
-
//=> false
|
|
654
|
+
Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
|
|
469
655
|
|
|
470
|
-
GreaterThan<1, 5>;
|
|
471
|
-
//=> false
|
|
472
656
|
```
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
? number extends A | B
|
|
478
|
-
? never
|
|
479
|
-
: [
|
|
480
|
-
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
481
|
-
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
482
|
-
] extends infer R extends [boolean, boolean, boolean, boolean]
|
|
483
|
-
? Or<
|
|
484
|
-
And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
|
|
485
|
-
And<IsEqual<R[3], true>, IsEqual<R[1], false>>
|
|
486
|
-
> extends true
|
|
487
|
-
? true
|
|
488
|
-
: Or<
|
|
489
|
-
And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
|
|
490
|
-
And<IsEqual<R[2], true>, IsEqual<R[0], false>>
|
|
491
|
-
> extends true
|
|
492
|
-
? false
|
|
493
|
-
: true extends R[number]
|
|
494
|
-
? false
|
|
495
|
-
: [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean]
|
|
496
|
-
? [true, false] extends R
|
|
497
|
-
? false
|
|
498
|
-
: [false, true] extends R
|
|
499
|
-
? true
|
|
500
|
-
: [false, false] extends R
|
|
501
|
-
? PositiveNumericStringGt<`${A}`, `${B}`>
|
|
502
|
-
: PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
|
|
503
|
-
: never
|
|
504
|
-
: never
|
|
505
|
-
: never // Should never happen
|
|
506
|
-
: never; // Should never happen
|
|
657
|
+
type Indexed = {} extends Record<string, unknown>
|
|
658
|
+
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
659
|
+
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
660
|
+
// => '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
507
661
|
|
|
508
|
-
|
|
509
|
-
|
|
662
|
+
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
663
|
+
? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
|
|
664
|
+
: "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
|
|
665
|
+
// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
|
|
669
|
+
|
|
670
|
+
```
|
|
671
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
672
|
+
|
|
673
|
+
type OmitIndexSignature<ObjectType> = {
|
|
674
|
+
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
675
|
+
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
676
|
+
};
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
680
|
+
|
|
681
|
+
```
|
|
682
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
683
|
+
|
|
684
|
+
type OmitIndexSignature<ObjectType> = {
|
|
685
|
+
[KeyType in keyof ObjectType
|
|
686
|
+
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
687
|
+
as {} extends Record<KeyType, unknown>
|
|
688
|
+
? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
689
|
+
: ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
690
|
+
]: ObjectType[KeyType];
|
|
691
|
+
};
|
|
692
|
+
```
|
|
693
|
+
|
|
694
|
+
If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
|
|
510
695
|
|
|
511
696
|
@example
|
|
512
697
|
```
|
|
513
|
-
import type {
|
|
698
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
514
699
|
|
|
515
|
-
|
|
516
|
-
|
|
700
|
+
interface Example {
|
|
701
|
+
// These index signatures will be removed.
|
|
702
|
+
[x: string]: any
|
|
703
|
+
[x: number]: any
|
|
704
|
+
[x: symbol]: any
|
|
705
|
+
[x: `head-${string}`]: string
|
|
706
|
+
[x: `${string}-tail`]: string
|
|
707
|
+
[x: `head-${string}-tail`]: string
|
|
708
|
+
[x: `${bigint}`]: string
|
|
709
|
+
[x: `embedded-${number}`]: string
|
|
517
710
|
|
|
518
|
-
|
|
519
|
-
|
|
711
|
+
// These explicitly defined keys will remain.
|
|
712
|
+
foo: 'bar';
|
|
713
|
+
qux?: 'baz';
|
|
714
|
+
}
|
|
520
715
|
|
|
521
|
-
|
|
522
|
-
|
|
716
|
+
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
717
|
+
// => { foo: 'bar'; qux?: 'baz' | undefined; }
|
|
523
718
|
```
|
|
719
|
+
|
|
720
|
+
@see PickIndexSignature
|
|
721
|
+
@category Object
|
|
524
722
|
*/
|
|
525
|
-
type
|
|
526
|
-
|
|
527
|
-
|
|
723
|
+
type OmitIndexSignature<ObjectType> = {
|
|
724
|
+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
725
|
+
? never
|
|
726
|
+
: KeyType]: ObjectType[KeyType];
|
|
727
|
+
};
|
|
528
728
|
|
|
529
729
|
/**
|
|
530
|
-
|
|
730
|
+
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
|
731
|
+
|
|
732
|
+
This is the counterpart of `OmitIndexSignature`.
|
|
531
733
|
|
|
532
734
|
@example
|
|
533
735
|
```
|
|
534
|
-
import type {
|
|
736
|
+
import type {PickIndexSignature} from 'type-fest';
|
|
535
737
|
|
|
536
|
-
|
|
537
|
-
//=> false
|
|
738
|
+
declare const symbolKey: unique symbol;
|
|
538
739
|
|
|
539
|
-
|
|
540
|
-
|
|
740
|
+
type Example = {
|
|
741
|
+
// These index signatures will remain.
|
|
742
|
+
[x: string]: unknown;
|
|
743
|
+
[x: number]: unknown;
|
|
744
|
+
[x: symbol]: unknown;
|
|
745
|
+
[x: `head-${string}`]: string;
|
|
746
|
+
[x: `${string}-tail`]: string;
|
|
747
|
+
[x: `head-${string}-tail`]: string;
|
|
748
|
+
[x: `${bigint}`]: string;
|
|
749
|
+
[x: `embedded-${number}`]: string;
|
|
541
750
|
|
|
542
|
-
|
|
543
|
-
|
|
751
|
+
// These explicitly defined keys will be removed.
|
|
752
|
+
['kebab-case-key']: string;
|
|
753
|
+
[symbolKey]: string;
|
|
754
|
+
foo: 'bar';
|
|
755
|
+
qux?: 'baz';
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
type ExampleIndexSignature = PickIndexSignature<Example>;
|
|
759
|
+
// {
|
|
760
|
+
// [x: string]: unknown;
|
|
761
|
+
// [x: number]: unknown;
|
|
762
|
+
// [x: symbol]: unknown;
|
|
763
|
+
// [x: `head-${string}`]: string;
|
|
764
|
+
// [x: `${string}-tail`]: string;
|
|
765
|
+
// [x: `head-${string}-tail`]: string;
|
|
766
|
+
// [x: `${bigint}`]: string;
|
|
767
|
+
// [x: `embedded-${number}`]: string;
|
|
768
|
+
// }
|
|
544
769
|
```
|
|
770
|
+
|
|
771
|
+
@see OmitIndexSignature
|
|
772
|
+
@category Object
|
|
545
773
|
*/
|
|
546
|
-
type
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
: true
|
|
552
|
-
: never; // Should never happen
|
|
774
|
+
type PickIndexSignature<ObjectType> = {
|
|
775
|
+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
776
|
+
? KeyType
|
|
777
|
+
: never]: ObjectType[KeyType];
|
|
778
|
+
};
|
|
553
779
|
|
|
554
|
-
//
|
|
780
|
+
// Merges two objects without worrying about index signatures.
|
|
781
|
+
type SimpleMerge<Destination, Source> = {
|
|
782
|
+
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
783
|
+
} & Source;
|
|
555
784
|
|
|
556
785
|
/**
|
|
557
|
-
|
|
786
|
+
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
558
787
|
|
|
559
|
-
|
|
788
|
+
@example
|
|
789
|
+
```
|
|
790
|
+
import type {Merge} from 'type-fest';
|
|
560
791
|
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
:
|
|
566
|
-
|
|
567
|
-
: BuildTuple<L, Fill, [...T, Fill]>;
|
|
792
|
+
interface Foo {
|
|
793
|
+
[x: string]: unknown;
|
|
794
|
+
[x: number]: unknown;
|
|
795
|
+
foo: string;
|
|
796
|
+
bar: symbol;
|
|
797
|
+
}
|
|
568
798
|
|
|
569
|
-
|
|
570
|
-
|
|
799
|
+
type Bar = {
|
|
800
|
+
[x: number]: number;
|
|
801
|
+
[x: symbol]: unknown;
|
|
802
|
+
bar: Date;
|
|
803
|
+
baz: boolean;
|
|
804
|
+
};
|
|
571
805
|
|
|
572
|
-
|
|
806
|
+
export type FooBar = Merge<Foo, Bar>;
|
|
807
|
+
// => {
|
|
808
|
+
// [x: string]: unknown;
|
|
809
|
+
// [x: number]: number;
|
|
810
|
+
// [x: symbol]: unknown;
|
|
811
|
+
// foo: string;
|
|
812
|
+
// bar: Date;
|
|
813
|
+
// baz: boolean;
|
|
814
|
+
// }
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
@category Object
|
|
573
818
|
*/
|
|
574
|
-
type
|
|
819
|
+
type Merge<Destination, Source> =
|
|
820
|
+
Simplify<
|
|
821
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
822
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
823
|
+
>;
|
|
575
824
|
|
|
576
825
|
/**
|
|
577
|
-
|
|
826
|
+
Merges user specified options with default options.
|
|
578
827
|
|
|
579
828
|
@example
|
|
580
829
|
```
|
|
581
|
-
type
|
|
582
|
-
|
|
830
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
831
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
832
|
+
type SpecifiedOptions = {leavesOnly: true};
|
|
583
833
|
|
|
584
|
-
type
|
|
585
|
-
//=>
|
|
834
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
835
|
+
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
836
|
+
```
|
|
586
837
|
|
|
587
|
-
|
|
588
|
-
|
|
838
|
+
@example
|
|
839
|
+
```
|
|
840
|
+
// Complains if default values are not provided for optional options
|
|
589
841
|
|
|
590
|
-
type
|
|
591
|
-
|
|
842
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
843
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10};
|
|
844
|
+
type SpecifiedOptions = {};
|
|
592
845
|
|
|
593
|
-
type
|
|
594
|
-
|
|
846
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
847
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
848
|
+
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
849
|
+
```
|
|
595
850
|
|
|
596
|
-
|
|
597
|
-
//=> -Infinity
|
|
851
|
+
@example
|
|
598
852
|
```
|
|
853
|
+
// Complains if an option's default type does not conform to the expected type
|
|
599
854
|
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
*/
|
|
604
|
-
type StringToNumber<S extends string> = S extends `${infer N extends number}`
|
|
605
|
-
? N
|
|
606
|
-
: S extends 'Infinity'
|
|
607
|
-
? PositiveInfinity
|
|
608
|
-
: S extends '-Infinity'
|
|
609
|
-
? NegativeInfinity
|
|
610
|
-
: never;
|
|
855
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
856
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
857
|
+
type SpecifiedOptions = {};
|
|
611
858
|
|
|
612
|
-
|
|
613
|
-
|
|
859
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
860
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
861
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
862
|
+
```
|
|
614
863
|
|
|
615
864
|
@example
|
|
616
865
|
```
|
|
617
|
-
|
|
618
|
-
//=> ['a', 'b', 'c', 'd', 'e']
|
|
866
|
+
// Complains if an option's specified type does not conform to the expected type
|
|
619
867
|
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
868
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
869
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
870
|
+
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
623
871
|
|
|
624
|
-
|
|
872
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
873
|
+
// ~~~~~~~~~~~~~~~~
|
|
874
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
875
|
+
```
|
|
625
876
|
*/
|
|
626
|
-
type
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
877
|
+
type ApplyDefaultOptions<
|
|
878
|
+
Options extends object,
|
|
879
|
+
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
|
|
880
|
+
SpecifiedOptions extends Options,
|
|
881
|
+
> =
|
|
882
|
+
If<IsAny<SpecifiedOptions>, Defaults,
|
|
883
|
+
If<IsNever<SpecifiedOptions>, Defaults,
|
|
884
|
+
Simplify<Merge<Defaults, {
|
|
885
|
+
[Key in keyof SpecifiedOptions
|
|
886
|
+
as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
|
|
887
|
+
]: SpecifiedOptions[Key]
|
|
888
|
+
}> & Required<Options>>>>;
|
|
631
889
|
|
|
632
890
|
/**
|
|
633
|
-
Returns
|
|
891
|
+
Returns a boolean for whether either of two given types are true.
|
|
892
|
+
|
|
893
|
+
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
|
|
634
894
|
|
|
635
895
|
@example
|
|
636
896
|
```
|
|
637
|
-
|
|
638
|
-
//=> 5
|
|
897
|
+
import type {Or} from 'type-fest';
|
|
639
898
|
|
|
640
|
-
|
|
641
|
-
//=>
|
|
899
|
+
type TT = Or<true, false>;
|
|
900
|
+
//=> true
|
|
901
|
+
|
|
902
|
+
type TF = Or<true, false>;
|
|
903
|
+
//=> true
|
|
904
|
+
|
|
905
|
+
type FT = Or<false, true>;
|
|
906
|
+
//=> true
|
|
907
|
+
|
|
908
|
+
type FF = Or<false, false>;
|
|
909
|
+
//=> false
|
|
642
910
|
```
|
|
643
911
|
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
: StringToArray<S>['length'];
|
|
912
|
+
Note: When `boolean` is passed as an argument, it is distributed into separate cases, and the final result is a union of those cases.
|
|
913
|
+
For example, `And<false, boolean>` expands to `And<false, true> | And<false, false>`, which simplifies to `true | false` (i.e., `boolean`).
|
|
914
|
+
@example
|
|
915
|
+
```
|
|
916
|
+
import type {And} from 'type-fest';
|
|
650
917
|
|
|
651
|
-
|
|
652
|
-
|
|
918
|
+
type A = Or<false, boolean>;
|
|
919
|
+
//=> boolean
|
|
920
|
+
|
|
921
|
+
type B = Or<boolean, false>;
|
|
922
|
+
//=> boolean
|
|
923
|
+
|
|
924
|
+
type C = Or<true, boolean>;
|
|
925
|
+
//=> true
|
|
926
|
+
|
|
927
|
+
type D = Or<boolean, true>;
|
|
928
|
+
//=> true
|
|
929
|
+
|
|
930
|
+
type E = Or<boolean, boolean>;
|
|
931
|
+
//=> boolean
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
Note: If `never` is passed as an argument, it is treated as `false` and the result is computed accordingly.
|
|
653
935
|
|
|
654
936
|
@example
|
|
655
937
|
```
|
|
656
|
-
|
|
938
|
+
import type {Or} from 'type-fest';
|
|
939
|
+
|
|
940
|
+
type A = Or<true, never>;
|
|
657
941
|
//=> true
|
|
658
942
|
|
|
659
|
-
|
|
943
|
+
type B = Or<never, true>;
|
|
944
|
+
//=> true
|
|
945
|
+
|
|
946
|
+
type C = Or<false, never>;
|
|
947
|
+
//=> false
|
|
948
|
+
|
|
949
|
+
type D = Or<never, false>;
|
|
950
|
+
//=> false
|
|
951
|
+
|
|
952
|
+
type E = Or<boolean, never>;
|
|
953
|
+
//=> boolean
|
|
954
|
+
|
|
955
|
+
type F = Or<never, boolean>;
|
|
956
|
+
//=> boolean
|
|
957
|
+
|
|
958
|
+
type G = Or<never, never>;
|
|
660
959
|
//=> false
|
|
661
960
|
```
|
|
961
|
+
|
|
962
|
+
@see {@link And}
|
|
662
963
|
*/
|
|
663
|
-
type
|
|
664
|
-
|
|
665
|
-
? FirstA extends FirstB
|
|
666
|
-
? SameLengthPositiveNumericStringGt<RestA, RestB>
|
|
667
|
-
: PositiveNumericCharacterGt<FirstA, FirstB>
|
|
668
|
-
: never
|
|
669
|
-
: false;
|
|
964
|
+
type Or<A extends boolean, B extends boolean> =
|
|
965
|
+
_Or<If<IsNever<A>, false, A>, If<IsNever<B>, false, B>>; // `never` is treated as `false`
|
|
670
966
|
|
|
671
|
-
type
|
|
967
|
+
type _Or<A extends boolean, B extends boolean> = A extends true
|
|
968
|
+
? true
|
|
969
|
+
: B extends true
|
|
970
|
+
? true
|
|
971
|
+
: false;
|
|
672
972
|
|
|
673
973
|
/**
|
|
674
|
-
|
|
974
|
+
@see {@link AllExtend}
|
|
975
|
+
*/
|
|
976
|
+
type AllExtendOptions = {
|
|
977
|
+
/**
|
|
978
|
+
Consider `never` elements to match the target type only if the target type itself is `never` (or `any`).
|
|
979
|
+
|
|
980
|
+
- When set to `true` (default), `never` is _not_ treated as a bottom type, instead, it is treated as a type that matches only itself (or `any`).
|
|
981
|
+
- When set to `false`, `never` is treated as a bottom type, and behaves as it normally would.
|
|
982
|
+
|
|
983
|
+
@default true
|
|
984
|
+
|
|
985
|
+
@example
|
|
986
|
+
```
|
|
987
|
+
import type {AllExtend} from 'type-fest';
|
|
988
|
+
|
|
989
|
+
type A = AllExtend<[1, 2, never], number, {strictNever: true}>;
|
|
990
|
+
//=> false
|
|
991
|
+
|
|
992
|
+
type B = AllExtend<[1, 2, never], number, {strictNever: false}>;
|
|
993
|
+
//=> true
|
|
994
|
+
|
|
995
|
+
type C = AllExtend<[never, never], never, {strictNever: true}>;
|
|
996
|
+
//=> true
|
|
997
|
+
|
|
998
|
+
type D = AllExtend<[never, never], never, {strictNever: false}>;
|
|
999
|
+
//=> true
|
|
1000
|
+
|
|
1001
|
+
type E = AllExtend<['a', 'b', never], any, {strictNever: true}>;
|
|
1002
|
+
//=> true
|
|
1003
|
+
|
|
1004
|
+
type F = AllExtend<['a', 'b', never], any, {strictNever: false}>;
|
|
1005
|
+
//=> true
|
|
1006
|
+
|
|
1007
|
+
type G = AllExtend<[never, 1], never, {strictNever: true}>;
|
|
1008
|
+
//=> false
|
|
1009
|
+
|
|
1010
|
+
type H = AllExtend<[never, 1], never, {strictNever: false}>;
|
|
1011
|
+
//=> false
|
|
1012
|
+
```
|
|
1013
|
+
*/
|
|
1014
|
+
strictNever?: boolean;
|
|
1015
|
+
};
|
|
1016
|
+
|
|
1017
|
+
type DefaultAllExtendOptions = {
|
|
1018
|
+
strictNever: true;
|
|
1019
|
+
};
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
Returns a boolean for whether every element in an array type extends another type.
|
|
675
1023
|
|
|
676
1024
|
@example
|
|
677
1025
|
```
|
|
678
|
-
|
|
1026
|
+
import type {AllExtend} from 'type-fest';
|
|
1027
|
+
|
|
1028
|
+
type A = AllExtend<[1, 2, 3], number>;
|
|
679
1029
|
//=> true
|
|
680
1030
|
|
|
681
|
-
|
|
1031
|
+
type B = AllExtend<[1, 2, '3'], number>;
|
|
682
1032
|
//=> false
|
|
683
1033
|
|
|
684
|
-
|
|
1034
|
+
type C = AllExtend<[number, number | string], number>;
|
|
1035
|
+
//=> boolean
|
|
1036
|
+
|
|
1037
|
+
type D = AllExtend<[true, boolean, true], true>;
|
|
1038
|
+
//=> boolean
|
|
1039
|
+
```
|
|
1040
|
+
|
|
1041
|
+
Note: Behaviour of optional elements depend on the `exactOptionalPropertyTypes` compiler option. When the option is disabled, the target type must include `undefined` for a successful match.
|
|
1042
|
+
|
|
1043
|
+
```
|
|
1044
|
+
import type {AllExtend} from 'type-fest';
|
|
1045
|
+
|
|
1046
|
+
// `exactOptionalPropertyTypes` enabled
|
|
1047
|
+
type A = AllExtend<[1?, 2?, 3?], number>;
|
|
1048
|
+
//=> true
|
|
1049
|
+
|
|
1050
|
+
// `exactOptionalPropertyTypes` disabled
|
|
1051
|
+
type B = AllExtend<[1?, 2?, 3?], number>;
|
|
685
1052
|
//=> false
|
|
1053
|
+
|
|
1054
|
+
// `exactOptionalPropertyTypes` disabled
|
|
1055
|
+
type C = AllExtend<[1?, 2?, 3?], number | undefined>;
|
|
1056
|
+
//=> true
|
|
686
1057
|
```
|
|
1058
|
+
|
|
1059
|
+
@see {@link AllExtendOptions}
|
|
1060
|
+
|
|
1061
|
+
@category Utilities
|
|
1062
|
+
@category Array
|
|
687
1063
|
*/
|
|
688
|
-
type
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
1064
|
+
type AllExtend<TArray extends UnknownArray, Type, Options extends AllExtendOptions = {}> =
|
|
1065
|
+
_AllExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<AllExtendOptions, DefaultAllExtendOptions, Options>>;
|
|
1066
|
+
|
|
1067
|
+
type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray, If<IsAny<Type>, true,
|
|
1068
|
+
TArray extends readonly [infer First, ...infer Rest]
|
|
1069
|
+
? IsNever<First> extends true
|
|
1070
|
+
? Or<IsNever<Type>, Not<Options['strictNever']>> extends true
|
|
1071
|
+
// If target `Type` is also `never` OR `strictNever` is disabled, recurse further.
|
|
1072
|
+
? _AllExtend<Rest, Type, Options>
|
|
1073
|
+
: false
|
|
1074
|
+
: First extends Type
|
|
1075
|
+
? _AllExtend<Rest, Type, Options>
|
|
1076
|
+
: false
|
|
1077
|
+
: true
|
|
1078
|
+
>, false, false>;
|
|
697
1079
|
|
|
698
1080
|
/**
|
|
699
|
-
Returns a boolean for whether
|
|
1081
|
+
Returns a boolean for whether two given types are both true.
|
|
1082
|
+
|
|
1083
|
+
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
|
|
700
1084
|
|
|
701
1085
|
@example
|
|
702
1086
|
```
|
|
703
|
-
|
|
1087
|
+
import type {And} from 'type-fest';
|
|
1088
|
+
|
|
1089
|
+
type TT = And<true, true>;
|
|
704
1090
|
//=> true
|
|
705
1091
|
|
|
706
|
-
|
|
1092
|
+
type TF = And<true, false>;
|
|
1093
|
+
//=> false
|
|
1094
|
+
|
|
1095
|
+
type FT = And<false, true>;
|
|
1096
|
+
//=> false
|
|
1097
|
+
|
|
1098
|
+
type FF = And<false, false>;
|
|
707
1099
|
//=> false
|
|
708
1100
|
```
|
|
709
|
-
*/
|
|
710
|
-
type PositiveNumericCharacterGt<A extends string, B extends string> = NumericString extends `${infer HeadA}${A}${infer TailA}`
|
|
711
|
-
? NumericString extends `${infer HeadB}${B}${infer TailB}`
|
|
712
|
-
? HeadA extends `${HeadB}${infer _}${infer __}`
|
|
713
|
-
? true
|
|
714
|
-
: false
|
|
715
|
-
: never
|
|
716
|
-
: never;
|
|
717
1101
|
|
|
718
|
-
|
|
719
|
-
|
|
1102
|
+
Note: When `boolean` is passed as an argument, it is distributed into separate cases, and the final result is a union of those cases.
|
|
1103
|
+
For example, `And<true, boolean>` expands to `And<true, true> | And<true, false>`, which simplifies to `true | false` (i.e., `boolean`).
|
|
720
1104
|
|
|
721
1105
|
@example
|
|
722
1106
|
```
|
|
723
|
-
|
|
724
|
-
//=> 1
|
|
1107
|
+
import type {And} from 'type-fest';
|
|
725
1108
|
|
|
726
|
-
|
|
727
|
-
//=>
|
|
1109
|
+
type A = And<true, boolean>;
|
|
1110
|
+
//=> boolean
|
|
728
1111
|
|
|
729
|
-
|
|
730
|
-
//=>
|
|
731
|
-
```
|
|
732
|
-
*/
|
|
733
|
-
type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N;
|
|
1112
|
+
type B = And<boolean, true>;
|
|
1113
|
+
//=> boolean
|
|
734
1114
|
|
|
735
|
-
|
|
736
|
-
|
|
1115
|
+
type C = And<false, boolean>;
|
|
1116
|
+
//=> false
|
|
737
1117
|
|
|
738
|
-
|
|
1118
|
+
type D = And<boolean, false>;
|
|
1119
|
+
//=> false
|
|
1120
|
+
|
|
1121
|
+
type E = And<boolean, boolean>;
|
|
1122
|
+
//=> boolean
|
|
1123
|
+
```
|
|
739
1124
|
|
|
1125
|
+
Note: If either of the types is `never`, the result becomes `false`.
|
|
740
1126
|
@example
|
|
741
1127
|
```
|
|
742
|
-
type
|
|
743
|
-
|
|
1128
|
+
import type {And} from 'type-fest';
|
|
1129
|
+
|
|
1130
|
+
type A = And<true, never>;
|
|
1131
|
+
//=> false
|
|
1132
|
+
|
|
1133
|
+
type B = And<never, true>;
|
|
1134
|
+
//=> false
|
|
1135
|
+
|
|
1136
|
+
type C = And<false, never>;
|
|
1137
|
+
//=> false
|
|
744
1138
|
|
|
745
|
-
type
|
|
746
|
-
//=>
|
|
1139
|
+
type D = And<never, false>;
|
|
1140
|
+
//=> false
|
|
747
1141
|
|
|
748
|
-
type
|
|
749
|
-
//=>
|
|
1142
|
+
type E = And<boolean, never>;
|
|
1143
|
+
//=> false
|
|
1144
|
+
|
|
1145
|
+
type F = And<never, boolean>;
|
|
1146
|
+
//=> false
|
|
750
1147
|
|
|
751
|
-
type
|
|
1148
|
+
type G = And<never, never>;
|
|
752
1149
|
//=> false
|
|
1150
|
+
```
|
|
1151
|
+
|
|
1152
|
+
@see {@link Or}
|
|
753
1153
|
*/
|
|
754
|
-
type
|
|
755
|
-
N extends number ? true
|
|
756
|
-
: N extends `${number}`
|
|
757
|
-
? true
|
|
758
|
-
: N extends `${number}.${number}`
|
|
759
|
-
? true
|
|
760
|
-
: false;
|
|
1154
|
+
type And<A extends boolean, B extends boolean> = AllExtend<[A, B], true>;
|
|
761
1155
|
|
|
762
1156
|
/**
|
|
763
|
-
Returns
|
|
1157
|
+
Returns a boolean for whether a given number is greater than another number.
|
|
764
1158
|
|
|
765
1159
|
@example
|
|
766
1160
|
```
|
|
767
|
-
|
|
768
|
-
//=> 1
|
|
1161
|
+
import type {GreaterThan} from 'type-fest';
|
|
769
1162
|
|
|
770
|
-
|
|
771
|
-
//=>
|
|
1163
|
+
GreaterThan<1, -5>;
|
|
1164
|
+
//=> true
|
|
772
1165
|
|
|
773
|
-
|
|
774
|
-
//=>
|
|
1166
|
+
GreaterThan<1, 1>;
|
|
1167
|
+
//=> false
|
|
775
1168
|
|
|
776
|
-
|
|
777
|
-
//=>
|
|
1169
|
+
GreaterThan<1, 5>;
|
|
1170
|
+
//=> false
|
|
778
1171
|
```
|
|
779
1172
|
*/
|
|
780
|
-
type
|
|
781
|
-
//
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
1173
|
+
type GreaterThan<A extends number, B extends number> =
|
|
1174
|
+
A extends number // For distributing `A`
|
|
1175
|
+
? B extends number // For distributing `B`
|
|
1176
|
+
? number extends A | B
|
|
1177
|
+
? never
|
|
1178
|
+
: [
|
|
1179
|
+
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
1180
|
+
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
1181
|
+
] extends infer R extends [boolean, boolean, boolean, boolean]
|
|
1182
|
+
? Or<
|
|
1183
|
+
And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
|
|
1184
|
+
And<IsEqual<R[3], true>, IsEqual<R[1], false>>
|
|
1185
|
+
> extends true
|
|
1186
|
+
? true
|
|
1187
|
+
: Or<
|
|
1188
|
+
And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
|
|
1189
|
+
And<IsEqual<R[2], true>, IsEqual<R[0], false>>
|
|
1190
|
+
> extends true
|
|
1191
|
+
? false
|
|
1192
|
+
: true extends R[number]
|
|
1193
|
+
? false
|
|
1194
|
+
: [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean]
|
|
1195
|
+
? [true, false] extends R
|
|
1196
|
+
? false
|
|
1197
|
+
: [false, true] extends R
|
|
1198
|
+
? true
|
|
1199
|
+
: [false, false] extends R
|
|
1200
|
+
? PositiveNumericStringGt<`${A}`, `${B}`>
|
|
1201
|
+
: PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
|
|
1202
|
+
: never
|
|
1203
|
+
: never
|
|
1204
|
+
: never // Should never happen
|
|
1205
|
+
: never; // Should never happen
|
|
787
1206
|
|
|
788
1207
|
/**
|
|
789
|
-
|
|
1208
|
+
Returns a boolean for whether a given number is greater than or equal to another number.
|
|
790
1209
|
|
|
791
1210
|
@example
|
|
792
1211
|
```
|
|
793
|
-
import type {
|
|
1212
|
+
import type {GreaterThanOrEqual} from 'type-fest';
|
|
794
1213
|
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
left: number;
|
|
798
|
-
};
|
|
1214
|
+
GreaterThanOrEqual<1, -5>;
|
|
1215
|
+
//=> true
|
|
799
1216
|
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
height: number;
|
|
803
|
-
};
|
|
1217
|
+
GreaterThanOrEqual<1, 1>;
|
|
1218
|
+
//=> true
|
|
804
1219
|
|
|
805
|
-
|
|
806
|
-
|
|
1220
|
+
GreaterThanOrEqual<1, 5>;
|
|
1221
|
+
//=> false
|
|
807
1222
|
```
|
|
1223
|
+
*/
|
|
1224
|
+
type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
|
|
1225
|
+
? never
|
|
1226
|
+
: A extends B ? true : GreaterThan<A, B>;
|
|
808
1227
|
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
1228
|
+
/**
|
|
1229
|
+
Returns a boolean for whether a given number is less than another number.
|
|
812
1230
|
|
|
813
1231
|
@example
|
|
814
1232
|
```
|
|
815
|
-
import type {
|
|
816
|
-
|
|
817
|
-
interface SomeInterface {
|
|
818
|
-
foo: number;
|
|
819
|
-
bar?: string;
|
|
820
|
-
baz: number | undefined;
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
type SomeType = {
|
|
824
|
-
foo: number;
|
|
825
|
-
bar?: string;
|
|
826
|
-
baz: number | undefined;
|
|
827
|
-
};
|
|
1233
|
+
import type {LessThan} from 'type-fest';
|
|
828
1234
|
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
const someInterface: SomeInterface = literal;
|
|
1235
|
+
LessThan<1, -5>;
|
|
1236
|
+
//=> false
|
|
832
1237
|
|
|
833
|
-
|
|
1238
|
+
LessThan<1, 1>;
|
|
1239
|
+
//=> false
|
|
834
1240
|
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
838
|
-
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
1241
|
+
LessThan<1, 5>;
|
|
1242
|
+
//=> true
|
|
839
1243
|
```
|
|
840
|
-
|
|
841
|
-
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
842
|
-
@see SimplifyDeep
|
|
843
|
-
@category Object
|
|
844
1244
|
*/
|
|
845
|
-
type
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
Use-cases:
|
|
853
|
-
- Remove overly permissive signatures from third-party types.
|
|
1245
|
+
type LessThan<A extends number, B extends number> = number extends A | B
|
|
1246
|
+
? never
|
|
1247
|
+
: GreaterThanOrEqual<A, B> extends infer Result
|
|
1248
|
+
? Result extends true
|
|
1249
|
+
? false
|
|
1250
|
+
: true
|
|
1251
|
+
: never; // Should never happen
|
|
854
1252
|
|
|
855
|
-
|
|
1253
|
+
// Should never happen
|
|
856
1254
|
|
|
857
|
-
|
|
1255
|
+
/**
|
|
1256
|
+
Create a tuple type of the given length `<L>` and fill it with the given type `<Fill>`.
|
|
858
1257
|
|
|
859
|
-
|
|
1258
|
+
If `<Fill>` is not provided, it will default to `unknown`.
|
|
860
1259
|
|
|
861
|
-
|
|
862
|
-
|
|
1260
|
+
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
1261
|
+
*/
|
|
1262
|
+
type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = number extends L
|
|
1263
|
+
? Fill[]
|
|
1264
|
+
: L extends T['length']
|
|
1265
|
+
? T
|
|
1266
|
+
: BuildTuple<L, Fill, [...T, Fill]>;
|
|
863
1267
|
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
```
|
|
1268
|
+
/**
|
|
1269
|
+
Return a string representation of the given string or number.
|
|
867
1270
|
|
|
868
|
-
|
|
1271
|
+
Note: This type is not the return type of the `.toString()` function.
|
|
1272
|
+
*/
|
|
1273
|
+
type ToString<T> = T extends string | number ? `${T}` : never;
|
|
869
1274
|
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
873
|
-
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
874
|
-
// => '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
1275
|
+
/**
|
|
1276
|
+
Converts a numeric string to a number.
|
|
875
1277
|
|
|
876
|
-
|
|
877
|
-
? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
|
|
878
|
-
: "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
|
|
879
|
-
// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
|
|
1278
|
+
@example
|
|
880
1279
|
```
|
|
1280
|
+
type PositiveInt = StringToNumber<'1234'>;
|
|
1281
|
+
//=> 1234
|
|
881
1282
|
|
|
882
|
-
|
|
1283
|
+
type NegativeInt = StringToNumber<'-1234'>;
|
|
1284
|
+
//=> -1234
|
|
883
1285
|
|
|
884
|
-
|
|
885
|
-
|
|
1286
|
+
type PositiveFloat = StringToNumber<'1234.56'>;
|
|
1287
|
+
//=> 1234.56
|
|
886
1288
|
|
|
887
|
-
type
|
|
888
|
-
|
|
889
|
-
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
890
|
-
};
|
|
891
|
-
```
|
|
1289
|
+
type NegativeFloat = StringToNumber<'-1234.56'>;
|
|
1290
|
+
//=> -1234.56
|
|
892
1291
|
|
|
893
|
-
|
|
1292
|
+
type PositiveInfinity = StringToNumber<'Infinity'>;
|
|
1293
|
+
//=> Infinity
|
|
894
1294
|
|
|
1295
|
+
type NegativeInfinity = StringToNumber<'-Infinity'>;
|
|
1296
|
+
//=> -Infinity
|
|
895
1297
|
```
|
|
896
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
897
1298
|
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
1299
|
+
@category String
|
|
1300
|
+
@category Numeric
|
|
1301
|
+
@category Template literal
|
|
1302
|
+
*/
|
|
1303
|
+
type StringToNumber<S extends string> = S extends `${infer N extends number}`
|
|
1304
|
+
? N
|
|
1305
|
+
: S extends 'Infinity'
|
|
1306
|
+
? PositiveInfinity
|
|
1307
|
+
: S extends '-Infinity'
|
|
1308
|
+
? NegativeInfinity
|
|
1309
|
+
: never;
|
|
907
1310
|
|
|
908
|
-
|
|
1311
|
+
/**
|
|
1312
|
+
Returns an array of the characters of the string.
|
|
909
1313
|
|
|
910
1314
|
@example
|
|
911
1315
|
```
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
interface Example {
|
|
915
|
-
// These index signatures will be removed.
|
|
916
|
-
[x: string]: any
|
|
917
|
-
[x: number]: any
|
|
918
|
-
[x: symbol]: any
|
|
919
|
-
[x: `head-${string}`]: string
|
|
920
|
-
[x: `${string}-tail`]: string
|
|
921
|
-
[x: `head-${string}-tail`]: string
|
|
922
|
-
[x: `${bigint}`]: string
|
|
923
|
-
[x: `embedded-${number}`]: string
|
|
924
|
-
|
|
925
|
-
// These explicitly defined keys will remain.
|
|
926
|
-
foo: 'bar';
|
|
927
|
-
qux?: 'baz';
|
|
928
|
-
}
|
|
1316
|
+
StringToArray<'abcde'>;
|
|
1317
|
+
//=> ['a', 'b', 'c', 'd', 'e']
|
|
929
1318
|
|
|
930
|
-
|
|
931
|
-
|
|
1319
|
+
StringToArray<string>;
|
|
1320
|
+
//=> never
|
|
932
1321
|
```
|
|
933
1322
|
|
|
934
|
-
@
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
?
|
|
940
|
-
:
|
|
941
|
-
};
|
|
1323
|
+
@category String
|
|
1324
|
+
*/
|
|
1325
|
+
type StringToArray<S extends string, Result extends string[] = []> = string extends S
|
|
1326
|
+
? never
|
|
1327
|
+
: S extends `${infer F}${infer R}`
|
|
1328
|
+
? StringToArray<R, [...Result, F]>
|
|
1329
|
+
: Result;
|
|
942
1330
|
|
|
943
1331
|
/**
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
This is the counterpart of `OmitIndexSignature`.
|
|
1332
|
+
Returns the length of the given string.
|
|
947
1333
|
|
|
948
1334
|
@example
|
|
949
1335
|
```
|
|
950
|
-
|
|
1336
|
+
StringLength<'abcde'>;
|
|
1337
|
+
//=> 5
|
|
951
1338
|
|
|
952
|
-
|
|
1339
|
+
StringLength<string>;
|
|
1340
|
+
//=> never
|
|
1341
|
+
```
|
|
953
1342
|
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
[x: `${string}-tail`]: string;
|
|
961
|
-
[x: `head-${string}-tail`]: string;
|
|
962
|
-
[x: `${bigint}`]: string;
|
|
963
|
-
[x: `embedded-${number}`]: string;
|
|
1343
|
+
@category String
|
|
1344
|
+
@category Template literal
|
|
1345
|
+
*/
|
|
1346
|
+
type StringLength<S extends string> = string extends S
|
|
1347
|
+
? never
|
|
1348
|
+
: StringToArray<S>['length'];
|
|
964
1349
|
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
[symbolKey]: string;
|
|
968
|
-
foo: 'bar';
|
|
969
|
-
qux?: 'baz';
|
|
970
|
-
};
|
|
1350
|
+
/**
|
|
1351
|
+
Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both numeric strings and have the same length.
|
|
971
1352
|
|
|
972
|
-
|
|
973
|
-
// {
|
|
974
|
-
// [x: string]: unknown;
|
|
975
|
-
// [x: number]: unknown;
|
|
976
|
-
// [x: symbol]: unknown;
|
|
977
|
-
// [x: `head-${string}`]: string;
|
|
978
|
-
// [x: `${string}-tail`]: string;
|
|
979
|
-
// [x: `head-${string}-tail`]: string;
|
|
980
|
-
// [x: `${bigint}`]: string;
|
|
981
|
-
// [x: `embedded-${number}`]: string;
|
|
982
|
-
// }
|
|
1353
|
+
@example
|
|
983
1354
|
```
|
|
1355
|
+
SameLengthPositiveNumericStringGt<'50', '10'>;
|
|
1356
|
+
//=> true
|
|
984
1357
|
|
|
985
|
-
|
|
986
|
-
|
|
1358
|
+
SameLengthPositiveNumericStringGt<'10', '10'>;
|
|
1359
|
+
//=> false
|
|
1360
|
+
```
|
|
987
1361
|
*/
|
|
988
|
-
type
|
|
989
|
-
|
|
990
|
-
?
|
|
991
|
-
|
|
992
|
-
|
|
1362
|
+
type SameLengthPositiveNumericStringGt<A extends string, B extends string> = A extends `${infer FirstA}${infer RestA}`
|
|
1363
|
+
? B extends `${infer FirstB}${infer RestB}`
|
|
1364
|
+
? FirstA extends FirstB
|
|
1365
|
+
? SameLengthPositiveNumericStringGt<RestA, RestB>
|
|
1366
|
+
: PositiveNumericCharacterGt<FirstA, FirstB>
|
|
1367
|
+
: never
|
|
1368
|
+
: false;
|
|
993
1369
|
|
|
994
|
-
|
|
995
|
-
type SimpleMerge<Destination, Source> = {
|
|
996
|
-
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
997
|
-
} & Source;
|
|
1370
|
+
type NumericString = '0123456789';
|
|
998
1371
|
|
|
999
1372
|
/**
|
|
1000
|
-
|
|
1373
|
+
Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are both positive numeric strings.
|
|
1001
1374
|
|
|
1002
1375
|
@example
|
|
1003
1376
|
```
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
interface Foo {
|
|
1007
|
-
[x: string]: unknown;
|
|
1008
|
-
[x: number]: unknown;
|
|
1009
|
-
foo: string;
|
|
1010
|
-
bar: symbol;
|
|
1011
|
-
}
|
|
1377
|
+
PositiveNumericStringGt<'500', '1'>;
|
|
1378
|
+
//=> true
|
|
1012
1379
|
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
[x: symbol]: unknown;
|
|
1016
|
-
bar: Date;
|
|
1017
|
-
baz: boolean;
|
|
1018
|
-
};
|
|
1380
|
+
PositiveNumericStringGt<'1', '1'>;
|
|
1381
|
+
//=> false
|
|
1019
1382
|
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
// [x: string]: unknown;
|
|
1023
|
-
// [x: number]: number;
|
|
1024
|
-
// [x: symbol]: unknown;
|
|
1025
|
-
// foo: string;
|
|
1026
|
-
// bar: Date;
|
|
1027
|
-
// baz: boolean;
|
|
1028
|
-
// }
|
|
1383
|
+
PositiveNumericStringGt<'1', '500'>;
|
|
1384
|
+
//=> false
|
|
1029
1385
|
```
|
|
1030
|
-
|
|
1031
|
-
@category Object
|
|
1032
1386
|
*/
|
|
1033
|
-
type
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1387
|
+
type PositiveNumericStringGt<A extends string, B extends string> = A extends B
|
|
1388
|
+
? false
|
|
1389
|
+
: [BuildTuple<StringLength<A>, 0>, BuildTuple<StringLength<B>, 0>] extends infer R extends [readonly unknown[], readonly unknown[]]
|
|
1390
|
+
? R[0] extends [...R[1], ...infer Remain extends readonly unknown[]]
|
|
1391
|
+
? 0 extends Remain['length']
|
|
1392
|
+
? SameLengthPositiveNumericStringGt<A, B>
|
|
1393
|
+
: true
|
|
1394
|
+
: false
|
|
1395
|
+
: never;
|
|
1038
1396
|
|
|
1039
1397
|
/**
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
@see {@link IsAny}
|
|
1398
|
+
Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters.
|
|
1043
1399
|
|
|
1044
1400
|
@example
|
|
1045
1401
|
```
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
type ShouldBeTrue = IfAny<any>;
|
|
1402
|
+
PositiveNumericCharacterGt<'5', '1'>;
|
|
1049
1403
|
//=> true
|
|
1050
1404
|
|
|
1051
|
-
|
|
1052
|
-
//=>
|
|
1405
|
+
PositiveNumericCharacterGt<'1', '1'>;
|
|
1406
|
+
//=> false
|
|
1053
1407
|
```
|
|
1054
|
-
|
|
1055
|
-
@category Type Guard
|
|
1056
|
-
@category Utilities
|
|
1057
|
-
*/
|
|
1058
|
-
type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
|
|
1059
|
-
IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
|
|
1060
|
-
);
|
|
1061
|
-
|
|
1062
|
-
/**
|
|
1063
|
-
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
1064
|
-
*/
|
|
1065
|
-
type BuiltIns = Primitive | void | Date | RegExp;
|
|
1066
|
-
|
|
1067
|
-
/**
|
|
1068
|
-
Matches non-recursive types.
|
|
1069
1408
|
*/
|
|
1070
|
-
type
|
|
1409
|
+
type PositiveNumericCharacterGt<A extends string, B extends string> = NumericString extends `${infer HeadA}${A}${infer TailA}`
|
|
1410
|
+
? NumericString extends `${infer HeadB}${B}${infer TailB}`
|
|
1411
|
+
? HeadA extends `${HeadB}${infer _}${infer __}`
|
|
1412
|
+
? true
|
|
1413
|
+
: false
|
|
1414
|
+
: never
|
|
1415
|
+
: never;
|
|
1071
1416
|
|
|
1072
1417
|
/**
|
|
1073
|
-
|
|
1418
|
+
Returns the absolute value of a given value.
|
|
1074
1419
|
|
|
1075
1420
|
@example
|
|
1076
1421
|
```
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
type SpecifiedOptions = {leavesOnly: true};
|
|
1422
|
+
NumberAbsolute<-1>;
|
|
1423
|
+
//=> 1
|
|
1080
1424
|
|
|
1081
|
-
|
|
1082
|
-
//=>
|
|
1083
|
-
```
|
|
1425
|
+
NumberAbsolute<1>;
|
|
1426
|
+
//=> 1
|
|
1084
1427
|
|
|
1085
|
-
|
|
1428
|
+
NumberAbsolute<NegativeInfinity>
|
|
1429
|
+
//=> PositiveInfinity
|
|
1086
1430
|
```
|
|
1087
|
-
|
|
1431
|
+
*/
|
|
1432
|
+
type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N;
|
|
1088
1433
|
|
|
1089
|
-
|
|
1090
|
-
type
|
|
1091
|
-
type SpecifiedOptions = {};
|
|
1434
|
+
/**
|
|
1435
|
+
Check whether the given type is a number or a number string.
|
|
1092
1436
|
|
|
1093
|
-
|
|
1094
|
-
// ~~~~~~~~~~~~~~~~~~~
|
|
1095
|
-
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
1096
|
-
```
|
|
1437
|
+
Supports floating-point as a string.
|
|
1097
1438
|
|
|
1098
1439
|
@example
|
|
1099
1440
|
```
|
|
1100
|
-
|
|
1441
|
+
type A = IsNumberLike<'1'>;
|
|
1442
|
+
//=> true
|
|
1101
1443
|
|
|
1102
|
-
type
|
|
1103
|
-
|
|
1104
|
-
type SpecifiedOptions = {};
|
|
1444
|
+
type B = IsNumberLike<'-1.1'>;
|
|
1445
|
+
//=> true
|
|
1105
1446
|
|
|
1106
|
-
type
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1447
|
+
type C = IsNumberLike<'5e-20'>;
|
|
1448
|
+
//=> true
|
|
1449
|
+
|
|
1450
|
+
type D = IsNumberLike<1>;
|
|
1451
|
+
//=> true
|
|
1452
|
+
|
|
1453
|
+
type E = IsNumberLike<'a'>;
|
|
1454
|
+
//=> false
|
|
1455
|
+
*/
|
|
1456
|
+
type IsNumberLike<N> =
|
|
1457
|
+
IsAnyOrNever<N> extends true ? N
|
|
1458
|
+
: N extends number | `${number}`
|
|
1459
|
+
? true
|
|
1460
|
+
: false;
|
|
1461
|
+
|
|
1462
|
+
/**
|
|
1463
|
+
Returns the number with reversed sign.
|
|
1110
1464
|
|
|
1111
1465
|
@example
|
|
1112
1466
|
```
|
|
1113
|
-
|
|
1467
|
+
ReverseSign<-1>;
|
|
1468
|
+
//=> 1
|
|
1114
1469
|
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
1470
|
+
ReverseSign<1>;
|
|
1471
|
+
//=> -1
|
|
1118
1472
|
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1473
|
+
ReverseSign<NegativeInfinity>
|
|
1474
|
+
//=> PositiveInfinity
|
|
1475
|
+
|
|
1476
|
+
ReverseSign<PositiveInfinity>
|
|
1477
|
+
//=> NegativeInfinity
|
|
1122
1478
|
```
|
|
1123
1479
|
*/
|
|
1124
|
-
type
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
Simplify<Merge<Defaults, {
|
|
1132
|
-
[Key in keyof SpecifiedOptions
|
|
1133
|
-
as Key extends OptionalKeysOf<Options>
|
|
1134
|
-
? Extract<SpecifiedOptions[Key], undefined> extends never
|
|
1135
|
-
? Key
|
|
1136
|
-
: never
|
|
1137
|
-
: Key
|
|
1138
|
-
]: SpecifiedOptions[Key]
|
|
1139
|
-
}> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
|
|
1140
|
-
>>;
|
|
1480
|
+
type ReverseSign<N extends number> =
|
|
1481
|
+
// Handle edge cases
|
|
1482
|
+
N extends 0 ? 0 : N extends PositiveInfinity ? NegativeInfinity : N extends NegativeInfinity ? PositiveInfinity :
|
|
1483
|
+
// Handle negative numbers
|
|
1484
|
+
`${N}` extends `-${infer P extends number}` ? P
|
|
1485
|
+
// Handle positive numbers
|
|
1486
|
+
: `-${N}` extends `${infer R extends number}` ? R : never;
|
|
1141
1487
|
|
|
1142
1488
|
/**
|
|
1143
1489
|
Returns the difference between two numbers.
|
|
@@ -1226,9 +1572,9 @@ Paths options.
|
|
|
1226
1572
|
*/
|
|
1227
1573
|
type PathsOptions = {
|
|
1228
1574
|
/**
|
|
1229
|
-
The maximum depth to recurse when searching for paths.
|
|
1575
|
+
The maximum depth to recurse when searching for paths. Range: 0 ~ 10.
|
|
1230
1576
|
|
|
1231
|
-
@default
|
|
1577
|
+
@default 5
|
|
1232
1578
|
*/
|
|
1233
1579
|
maxRecursionDepth?: number;
|
|
1234
1580
|
|
|
@@ -1343,7 +1689,7 @@ type PathsOptions = {
|
|
|
1343
1689
|
};
|
|
1344
1690
|
|
|
1345
1691
|
type DefaultPathsOptions = {
|
|
1346
|
-
maxRecursionDepth:
|
|
1692
|
+
maxRecursionDepth: 5;
|
|
1347
1693
|
bracketNotation: false;
|
|
1348
1694
|
leavesOnly: false;
|
|
1349
1695
|
depth: number;
|
|
@@ -1400,8 +1746,7 @@ type _Paths<T, Options extends Required<PathsOptions>> =
|
|
|
1400
1746
|
: T extends UnknownArray
|
|
1401
1747
|
? number extends T['length']
|
|
1402
1748
|
// We need to handle the fixed and non-fixed index part of the array separately.
|
|
1403
|
-
? InternalPaths<StaticPartOfArray<T>, Options>
|
|
1404
|
-
| InternalPaths<Array<VariablePartOfArray<T>[number]>, Options>
|
|
1749
|
+
? InternalPaths<StaticPartOfArray<T>, Options> | InternalPaths<Array<VariablePartOfArray<T>[number]>, Options>
|
|
1405
1750
|
: InternalPaths<T, Options>
|
|
1406
1751
|
: T extends object
|
|
1407
1752
|
? InternalPaths<T, Options>
|
|
@@ -1410,30 +1755,34 @@ type _Paths<T, Options extends Required<PathsOptions>> =
|
|
|
1410
1755
|
type InternalPaths<T, Options extends Required<PathsOptions>> =
|
|
1411
1756
|
Options['maxRecursionDepth'] extends infer MaxDepth extends number
|
|
1412
1757
|
? Required<T> extends infer T
|
|
1413
|
-
? T extends
|
|
1758
|
+
? T extends readonly []
|
|
1414
1759
|
? never
|
|
1415
|
-
:
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1760
|
+
: IsNever<keyof T> extends true // Check for empty object
|
|
1761
|
+
? never
|
|
1762
|
+
: {
|
|
1763
|
+
[Key in keyof T]:
|
|
1764
|
+
Key extends string | number // Limit `Key` to string or number.
|
|
1765
|
+
? (
|
|
1766
|
+
Options['bracketNotation'] extends true
|
|
1767
|
+
? IsNumberLike<Key> extends true
|
|
1768
|
+
? `[${Key}]`
|
|
1769
|
+
: (Key | ToString<Key>)
|
|
1770
|
+
: Options['bracketNotation'] extends false
|
|
1771
|
+
// If `Key` is a number, return `Key | `${Key}``, because both `array[0]` and `array['0']` work.
|
|
1772
|
+
? (Key | ToString<Key>)
|
|
1773
|
+
: never
|
|
1774
|
+
) extends infer TranformedKey extends string | number ?
|
|
1775
|
+
// 1. If style is 'a[0].b' and 'Key' is a numberlike value like 3 or '3', transform 'Key' to `[${Key}]`, else to `${Key}` | Key
|
|
1776
|
+
// 2. If style is 'a.0.b', transform 'Key' to `${Key}` | Key
|
|
1432
1777
|
| ((Options['leavesOnly'] extends true
|
|
1433
1778
|
? MaxDepth extends 0
|
|
1434
1779
|
? TranformedKey
|
|
1435
|
-
: T[Key] extends
|
|
1436
|
-
?
|
|
1780
|
+
: T[Key] extends infer Value
|
|
1781
|
+
? (Value extends readonly [] | NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
|
|
1782
|
+
? TranformedKey
|
|
1783
|
+
: IsNever<keyof Value> extends true // Check for empty object
|
|
1784
|
+
? TranformedKey
|
|
1785
|
+
: never)
|
|
1437
1786
|
: never
|
|
1438
1787
|
: TranformedKey
|
|
1439
1788
|
) extends infer _TransformedKey
|
|
@@ -1447,12 +1796,12 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
|
|
|
1447
1796
|
// Recursively generate paths for the current key
|
|
1448
1797
|
GreaterThan<MaxDepth, 0> extends true // Limit the depth to prevent infinite recursion
|
|
1449
1798
|
? _Paths<T[Key],
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1799
|
+
{
|
|
1800
|
+
bracketNotation: Options['bracketNotation'];
|
|
1801
|
+
maxRecursionDepth: Subtract<MaxDepth, 1>;
|
|
1802
|
+
leavesOnly: Options['leavesOnly'];
|
|
1803
|
+
depth: Subtract<Options['depth'], 1>;
|
|
1804
|
+
}> extends infer SubPath
|
|
1456
1805
|
? SubPath extends string | number
|
|
1457
1806
|
? (
|
|
1458
1807
|
Options['bracketNotation'] extends true
|
|
@@ -1469,9 +1818,9 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
|
|
|
1469
1818
|
: never
|
|
1470
1819
|
: never
|
|
1471
1820
|
)
|
|
1821
|
+
: never
|
|
1472
1822
|
: never
|
|
1473
|
-
|
|
1474
|
-
}[keyof T & (T extends UnknownArray ? number : unknown)]
|
|
1823
|
+
}[keyof T & (T extends UnknownArray ? number : unknown)]
|
|
1475
1824
|
: never
|
|
1476
1825
|
: never;
|
|
1477
1826
|
|
|
@@ -1719,7 +2068,7 @@ declare namespace PackageJson$1 {
|
|
|
1719
2068
|
/**
|
|
1720
2069
|
A mapping of conditions and the paths to which they resolve.
|
|
1721
2070
|
*/
|
|
1722
|
-
type ExportConditions = {
|
|
2071
|
+
type ExportConditions = {
|
|
1723
2072
|
[condition: string]: Exports;
|
|
1724
2073
|
};
|
|
1725
2074
|
|
|
@@ -1727,15 +2076,15 @@ declare namespace PackageJson$1 {
|
|
|
1727
2076
|
Entry points of a module, optionally with conditions and subpath exports.
|
|
1728
2077
|
*/
|
|
1729
2078
|
export type Exports =
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
2079
|
+
| null
|
|
2080
|
+
| string
|
|
2081
|
+
| Array<string | ExportConditions>
|
|
2082
|
+
| ExportConditions;
|
|
1734
2083
|
|
|
1735
2084
|
/**
|
|
1736
2085
|
Import map entries of a module, optionally with conditions and subpath imports.
|
|
1737
2086
|
*/
|
|
1738
|
-
export type Imports = {
|
|
2087
|
+
export type Imports = {
|
|
1739
2088
|
[key: `#${string}`]: Exports;
|
|
1740
2089
|
};
|
|
1741
2090
|
|
|
@@ -1750,19 +2099,19 @@ declare namespace PackageJson$1 {
|
|
|
1750
2099
|
A module ID with untranspiled code that is the primary entry point to the program.
|
|
1751
2100
|
*/
|
|
1752
2101
|
esnext?:
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
2102
|
+
| string
|
|
2103
|
+
| {
|
|
2104
|
+
[moduleName: string]: string | undefined;
|
|
2105
|
+
main?: string;
|
|
2106
|
+
browser?: string;
|
|
2107
|
+
};
|
|
1759
2108
|
|
|
1760
2109
|
/**
|
|
1761
2110
|
A hint to JavaScript bundlers or component tools when packaging modules for client side use.
|
|
1762
2111
|
*/
|
|
1763
2112
|
browser?:
|
|
1764
|
-
|
|
1765
|
-
|
|
2113
|
+
| string
|
|
2114
|
+
| Partial<Record<string, string | false>>;
|
|
1766
2115
|
|
|
1767
2116
|
/**
|
|
1768
2117
|
Denote which files in your project are "pure" and therefore safe for Webpack to prune if unused.
|
|
@@ -1934,8 +2283,8 @@ declare namespace PackageJson$1 {
|
|
|
1934
2283
|
The executable files that should be installed into the `PATH`.
|
|
1935
2284
|
*/
|
|
1936
2285
|
bin?:
|
|
1937
|
-
|
|
1938
|
-
|
|
2286
|
+
| string
|
|
2287
|
+
| Partial<Record<string, string>>;
|
|
1939
2288
|
|
|
1940
2289
|
/**
|
|
1941
2290
|
Filenames to put in place for the `man` program to find.
|
|
@@ -1951,18 +2300,18 @@ declare namespace PackageJson$1 {
|
|
|
1951
2300
|
Location for the code repository.
|
|
1952
2301
|
*/
|
|
1953
2302
|
repository?:
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
2303
|
+
| string
|
|
2304
|
+
| {
|
|
2305
|
+
type: string;
|
|
2306
|
+
url: string;
|
|
1958
2307
|
|
|
1959
|
-
|
|
2308
|
+
/**
|
|
1960
2309
|
Relative path to package.json if it is placed in non-root directory (for example if it is part of a monorepo).
|
|
1961
2310
|
|
|
1962
2311
|
[Read more.](https://github.com/npm/rfcs/blob/latest/implemented/0010-monorepo-subdirectory-declaration.md)
|
|
1963
2312
|
*/
|
|
1964
|
-
|
|
1965
|
-
|
|
2313
|
+
directory?: string;
|
|
2314
|
+
};
|
|
1966
2315
|
|
|
1967
2316
|
/**
|
|
1968
2317
|
Script commands that are run at various times in the lifecycle of the package. The key is the lifecycle event, and the value is the command to run at that point.
|
|
@@ -2025,50 +2374,50 @@ declare namespace PackageJson$1 {
|
|
|
2025
2374
|
Operating systems the module runs on.
|
|
2026
2375
|
*/
|
|
2027
2376
|
os?: Array<LiteralUnion<
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2377
|
+
| 'aix'
|
|
2378
|
+
| 'darwin'
|
|
2379
|
+
| 'freebsd'
|
|
2380
|
+
| 'linux'
|
|
2381
|
+
| 'openbsd'
|
|
2382
|
+
| 'sunos'
|
|
2383
|
+
| 'win32'
|
|
2384
|
+
| '!aix'
|
|
2385
|
+
| '!darwin'
|
|
2386
|
+
| '!freebsd'
|
|
2387
|
+
| '!linux'
|
|
2388
|
+
| '!openbsd'
|
|
2389
|
+
| '!sunos'
|
|
2390
|
+
| '!win32',
|
|
2391
|
+
string
|
|
2043
2392
|
>>;
|
|
2044
2393
|
|
|
2045
2394
|
/**
|
|
2046
2395
|
CPU architectures the module runs on.
|
|
2047
2396
|
*/
|
|
2048
2397
|
cpu?: Array<LiteralUnion<
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2398
|
+
| 'arm'
|
|
2399
|
+
| 'arm64'
|
|
2400
|
+
| 'ia32'
|
|
2401
|
+
| 'mips'
|
|
2402
|
+
| 'mipsel'
|
|
2403
|
+
| 'ppc'
|
|
2404
|
+
| 'ppc64'
|
|
2405
|
+
| 's390'
|
|
2406
|
+
| 's390x'
|
|
2407
|
+
| 'x32'
|
|
2408
|
+
| 'x64'
|
|
2409
|
+
| '!arm'
|
|
2410
|
+
| '!arm64'
|
|
2411
|
+
| '!ia32'
|
|
2412
|
+
| '!mips'
|
|
2413
|
+
| '!mipsel'
|
|
2414
|
+
| '!ppc'
|
|
2415
|
+
| '!ppc64'
|
|
2416
|
+
| '!s390'
|
|
2417
|
+
| '!s390x'
|
|
2418
|
+
| '!x32'
|
|
2419
|
+
| '!x64',
|
|
2420
|
+
string
|
|
2072
2421
|
>>;
|
|
2073
2422
|
|
|
2074
2423
|
/**
|
|
@@ -2091,20 +2440,20 @@ declare namespace PackageJson$1 {
|
|
|
2091
2440
|
/**
|
|
2092
2441
|
Describes and notifies consumers of a package's monetary support information.
|
|
2093
2442
|
|
|
2094
|
-
[Read more.](https://github.com/npm/rfcs/blob/
|
|
2443
|
+
[Read more.](https://github.com/npm/rfcs/blob/main/implemented/0017-add-funding-support.md)
|
|
2095
2444
|
*/
|
|
2096
2445
|
funding?: string | {
|
|
2097
2446
|
/**
|
|
2098
2447
|
The type of funding.
|
|
2099
2448
|
*/
|
|
2100
2449
|
type?: LiteralUnion<
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2450
|
+
| 'github'
|
|
2451
|
+
| 'opencollective'
|
|
2452
|
+
| 'patreon'
|
|
2453
|
+
| 'individual'
|
|
2454
|
+
| 'foundation'
|
|
2455
|
+
| 'corporation',
|
|
2456
|
+
string
|
|
2108
2457
|
>;
|
|
2109
2458
|
|
|
2110
2459
|
/**
|
|
@@ -2175,22 +2524,13 @@ Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-j
|
|
|
2175
2524
|
@category File
|
|
2176
2525
|
*/
|
|
2177
2526
|
type PackageJson$1 =
|
|
2178
|
-
JsonObject &
|
|
2179
|
-
PackageJson$1.NodeJsStandard &
|
|
2180
|
-
PackageJson$1.PackageJsonStandard &
|
|
2181
|
-
PackageJson$1.NonStandardEntryPoints &
|
|
2182
|
-
PackageJson$1.TypeScriptConfiguration &
|
|
2183
|
-
PackageJson$1.YarnConfiguration &
|
|
2184
|
-
PackageJson$1.JSPMConfiguration;
|
|
2185
|
-
|
|
2186
|
-
interface InstallPackageOptions {
|
|
2187
|
-
cwd?: string;
|
|
2188
|
-
dev?: boolean;
|
|
2189
|
-
silent?: boolean;
|
|
2190
|
-
packageManager?: string;
|
|
2191
|
-
preferOffline?: boolean;
|
|
2192
|
-
additionalArgs?: string[] | ((agent: string, detectedAgent: string) => string[] | undefined);
|
|
2193
|
-
}
|
|
2527
|
+
JsonObject &
|
|
2528
|
+
PackageJson$1.NodeJsStandard &
|
|
2529
|
+
PackageJson$1.PackageJsonStandard &
|
|
2530
|
+
PackageJson$1.NonStandardEntryPoints &
|
|
2531
|
+
PackageJson$1.TypeScriptConfiguration &
|
|
2532
|
+
PackageJson$1.YarnConfiguration &
|
|
2533
|
+
PackageJson$1.JSPMConfiguration;
|
|
2194
2534
|
|
|
2195
2535
|
type Prettify<T> = {
|
|
2196
2536
|
[K in keyof T]: T[K];
|
|
@@ -2366,10 +2706,10 @@ type EnsurePackagesOptions = {
|
|
|
2366
2706
|
deps?: boolean;
|
|
2367
2707
|
devDeps?: boolean;
|
|
2368
2708
|
installPackage?: Omit<InstallPackageOptions, "cwd" | "dev">;
|
|
2369
|
-
peerDeps?: boolean;
|
|
2370
2709
|
logger?: {
|
|
2371
2710
|
warn: (message: string) => void;
|
|
2372
2711
|
};
|
|
2712
|
+
peerDeps?: boolean;
|
|
2373
2713
|
throwOnWarn?: boolean;
|
|
2374
2714
|
};
|
|
2375
2715
|
|
|
@@ -2391,10 +2731,16 @@ declare const writePackageJson: <T = PackageJson>(data: T, options?: WriteJsonOp
|
|
|
2391
2731
|
declare const writePackageJsonSync: <T = PackageJson>(data: T, options?: WriteJsonOptions & {
|
|
2392
2732
|
cwd?: URL | string;
|
|
2393
2733
|
}) => void;
|
|
2394
|
-
declare const
|
|
2734
|
+
declare const parsePackageJsonSync: (packageFile: JsonObject | string, options?: {
|
|
2395
2735
|
ignoreWarnings?: (RegExp | string)[];
|
|
2736
|
+
resolveCatalogs?: boolean;
|
|
2396
2737
|
strict?: boolean;
|
|
2397
2738
|
}) => NormalizedPackageJson;
|
|
2739
|
+
declare const parsePackageJson: (packageFile: JsonObject | string, options?: {
|
|
2740
|
+
ignoreWarnings?: (RegExp | string)[];
|
|
2741
|
+
resolveCatalogs?: boolean;
|
|
2742
|
+
strict?: boolean;
|
|
2743
|
+
}) => Promise<NormalizedPackageJson>;
|
|
2398
2744
|
declare const getPackageJsonProperty: <T = unknown>(packageJson: NormalizedPackageJson, property: Paths<NormalizedPackageJson>, defaultValue?: T) => T;
|
|
2399
2745
|
declare const hasPackageJsonProperty: (packageJson: NormalizedPackageJson, property: Paths<NormalizedPackageJson>) => boolean;
|
|
2400
2746
|
declare const hasPackageJsonAnyDependency: (packageJson: NormalizedPackageJson, arguments_: string[], options?: {
|
|
@@ -2402,4 +2748,4 @@ declare const hasPackageJsonAnyDependency: (packageJson: NormalizedPackageJson,
|
|
|
2402
2748
|
}) => boolean;
|
|
2403
2749
|
declare const ensurePackages: (packageJson: NormalizedPackageJson, packages: string[], installKey?: "dependencies" | "devDependencies", options?: EnsurePackagesOptions) => Promise<void>;
|
|
2404
2750
|
|
|
2405
|
-
export { type EnsurePackagesOptions as E, type FindPackageJsonCache as F, type NormalizedReadResult as N, type PackageJson as P, findPackageJsonSync as a, hasPackageJsonProperty as b, writePackageJsonSync as c, type NormalizedPackageJson as d, ensurePackages as e, findPackageJson as f, getPackageJsonProperty as g, hasPackageJsonAnyDependency as h, parsePackageJson as p, writePackageJson as w };
|
|
2751
|
+
export { type EnsurePackagesOptions as E, type FindPackageJsonCache as F, type NormalizedReadResult as N, type PackageJson as P, findPackageJsonSync as a, hasPackageJsonProperty as b, writePackageJsonSync as c, type NormalizedPackageJson as d, ensurePackages as e, findPackageJson as f, getPackageJsonProperty as g, hasPackageJsonAnyDependency as h, parsePackageJsonSync as i, parsePackageJson as p, writePackageJson as w };
|