@visulima/package 3.6.1 → 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 +12 -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-DoazDpIb.d.cts → package-json-k3TJ_oy7.d.ts} +10 -43
- package/dist/pnpm.d.ts +15 -0
- package/dist/pnpm.js +1 -0
- package/package.json +25 -76
- 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-DoazDpIb.d.mts +0 -2784
- package/dist/packem_shared/package-json-DoazDpIb.d.ts +0 -2784
|
@@ -1,2784 +0,0 @@
|
|
|
1
|
-
import { WriteJsonOptions } from '@visulima/fs';
|
|
2
|
-
import { Package } from 'normalize-package-data';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
6
|
-
|
|
7
|
-
@category Type
|
|
8
|
-
*/
|
|
9
|
-
type Primitive =
|
|
10
|
-
| null
|
|
11
|
-
| undefined
|
|
12
|
-
| string
|
|
13
|
-
| number
|
|
14
|
-
| boolean
|
|
15
|
-
| symbol
|
|
16
|
-
| bigint;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
Matches a JSON object.
|
|
20
|
-
|
|
21
|
-
This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`.
|
|
22
|
-
|
|
23
|
-
@category JSON
|
|
24
|
-
*/
|
|
25
|
-
type JsonObject = {[Key in string]: JsonValue} & {[Key in string]?: JsonValue | undefined};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
Matches a JSON array.
|
|
29
|
-
|
|
30
|
-
@category JSON
|
|
31
|
-
*/
|
|
32
|
-
type JsonArray = JsonValue[] | readonly JsonValue[];
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
Matches any valid JSON primitive value.
|
|
36
|
-
|
|
37
|
-
@category JSON
|
|
38
|
-
*/
|
|
39
|
-
type JsonPrimitive = string | number | boolean | null;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
Matches any valid JSON value.
|
|
43
|
-
|
|
44
|
-
@see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`.
|
|
45
|
-
|
|
46
|
-
@category JSON
|
|
47
|
-
*/
|
|
48
|
-
type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
Returns a boolean for whether the given type is `any`.
|
|
52
|
-
|
|
53
|
-
@link https://stackoverflow.com/a/49928360/1490091
|
|
54
|
-
|
|
55
|
-
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
56
|
-
|
|
57
|
-
@example
|
|
58
|
-
```
|
|
59
|
-
import type {IsAny} from 'type-fest';
|
|
60
|
-
|
|
61
|
-
const typedObject = {a: 1, b: 2} as const;
|
|
62
|
-
const anyObject: any = {a: 1, b: 2};
|
|
63
|
-
|
|
64
|
-
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
|
|
65
|
-
return obj[key];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const typedA = get(typedObject, 'a');
|
|
69
|
-
//=> 1
|
|
70
|
-
|
|
71
|
-
const anyA = get(anyObject, 'a');
|
|
72
|
-
//=> any
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
@category Type Guard
|
|
76
|
-
@category Utilities
|
|
77
|
-
*/
|
|
78
|
-
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
Returns a boolean for whether the given key is an optional key of type.
|
|
82
|
-
|
|
83
|
-
This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
|
|
84
|
-
|
|
85
|
-
@example
|
|
86
|
-
```
|
|
87
|
-
import type {IsOptionalKeyOf} from 'type-fest';
|
|
88
|
-
|
|
89
|
-
interface User {
|
|
90
|
-
name: string;
|
|
91
|
-
surname: string;
|
|
92
|
-
|
|
93
|
-
luckyNumber?: number;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
interface Admin {
|
|
97
|
-
name: string;
|
|
98
|
-
surname?: string;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
|
|
102
|
-
//=> true
|
|
103
|
-
|
|
104
|
-
type T2 = IsOptionalKeyOf<User, 'name'>;
|
|
105
|
-
//=> false
|
|
106
|
-
|
|
107
|
-
type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
|
|
108
|
-
//=> boolean
|
|
109
|
-
|
|
110
|
-
type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
|
|
111
|
-
//=> false
|
|
112
|
-
|
|
113
|
-
type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
|
|
114
|
-
//=> boolean
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
@category Type Guard
|
|
118
|
-
@category Utilities
|
|
119
|
-
*/
|
|
120
|
-
type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
|
|
121
|
-
IsAny<Type | Key> extends true ? never
|
|
122
|
-
: Key extends keyof Type
|
|
123
|
-
? Type extends Record<Key, Type[Key]>
|
|
124
|
-
? false
|
|
125
|
-
: true
|
|
126
|
-
: false;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
Extract all optional keys from the given type.
|
|
130
|
-
|
|
131
|
-
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
132
|
-
|
|
133
|
-
@example
|
|
134
|
-
```
|
|
135
|
-
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
136
|
-
|
|
137
|
-
interface User {
|
|
138
|
-
name: string;
|
|
139
|
-
surname: string;
|
|
140
|
-
|
|
141
|
-
luckyNumber?: number;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
145
|
-
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
146
|
-
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
const update1: UpdateOperation<User> = {
|
|
150
|
-
name: 'Alice'
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const update2: UpdateOperation<User> = {
|
|
154
|
-
name: 'Bob',
|
|
155
|
-
luckyNumber: REMOVE_FIELD
|
|
156
|
-
};
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
@category Utilities
|
|
160
|
-
*/
|
|
161
|
-
type OptionalKeysOf<Type extends object> =
|
|
162
|
-
Type extends unknown // For distributing `Type`
|
|
163
|
-
? (keyof {[Key in keyof Type as
|
|
164
|
-
IsOptionalKeyOf<Type, Key> extends false
|
|
165
|
-
? never
|
|
166
|
-
: Key
|
|
167
|
-
]: never
|
|
168
|
-
}) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
|
|
169
|
-
: never; // Should never happen
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
Extract all required keys from the given type.
|
|
173
|
-
|
|
174
|
-
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
|
|
175
|
-
|
|
176
|
-
@example
|
|
177
|
-
```
|
|
178
|
-
import type {RequiredKeysOf} from 'type-fest';
|
|
179
|
-
|
|
180
|
-
declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
|
|
181
|
-
|
|
182
|
-
interface User {
|
|
183
|
-
name: string;
|
|
184
|
-
surname: string;
|
|
185
|
-
|
|
186
|
-
luckyNumber?: number;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
190
|
-
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
@category Utilities
|
|
194
|
-
*/
|
|
195
|
-
type RequiredKeysOf<Type extends object> =
|
|
196
|
-
Type extends unknown // For distributing `Type`
|
|
197
|
-
? Exclude<keyof Type, OptionalKeysOf<Type>>
|
|
198
|
-
: never; // Should never happen
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
Returns a boolean for whether the given type is `never`.
|
|
202
|
-
|
|
203
|
-
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
204
|
-
@link https://stackoverflow.com/a/53984913/10292952
|
|
205
|
-
@link https://www.zhenghao.io/posts/ts-never
|
|
206
|
-
|
|
207
|
-
Useful in type utilities, such as checking if something does not occur.
|
|
208
|
-
|
|
209
|
-
@example
|
|
210
|
-
```
|
|
211
|
-
import type {IsNever, And} from 'type-fest';
|
|
212
|
-
|
|
213
|
-
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
214
|
-
type AreStringsEqual<A extends string, B extends string> =
|
|
215
|
-
And<
|
|
216
|
-
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
217
|
-
IsNever<Exclude<B, A>> extends true ? true : false
|
|
218
|
-
>;
|
|
219
|
-
|
|
220
|
-
type EndIfEqual<I extends string, O extends string> =
|
|
221
|
-
AreStringsEqual<I, O> extends true
|
|
222
|
-
? never
|
|
223
|
-
: void;
|
|
224
|
-
|
|
225
|
-
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
226
|
-
if (input === output) {
|
|
227
|
-
process.exit(0);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
endIfEqual('abc', 'abc');
|
|
232
|
-
//=> never
|
|
233
|
-
|
|
234
|
-
endIfEqual('abc', '123');
|
|
235
|
-
//=> void
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
@category Type Guard
|
|
239
|
-
@category Utilities
|
|
240
|
-
*/
|
|
241
|
-
type IsNever<T> = [T] extends [never] ? true : false;
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
|
|
245
|
-
|
|
246
|
-
Use-cases:
|
|
247
|
-
- 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'>`.
|
|
248
|
-
|
|
249
|
-
Note:
|
|
250
|
-
- 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'`.
|
|
251
|
-
- Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
|
|
252
|
-
|
|
253
|
-
@example
|
|
254
|
-
```
|
|
255
|
-
import {If} from 'type-fest';
|
|
256
|
-
|
|
257
|
-
type A = If<true, 'yes', 'no'>;
|
|
258
|
-
//=> 'yes'
|
|
259
|
-
|
|
260
|
-
type B = If<false, 'yes', 'no'>;
|
|
261
|
-
//=> 'no'
|
|
262
|
-
|
|
263
|
-
type C = If<boolean, 'yes', 'no'>;
|
|
264
|
-
//=> 'yes' | 'no'
|
|
265
|
-
|
|
266
|
-
type D = If<any, 'yes', 'no'>;
|
|
267
|
-
//=> 'yes' | 'no'
|
|
268
|
-
|
|
269
|
-
type E = If<never, 'yes', 'no'>;
|
|
270
|
-
//=> 'no'
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
@example
|
|
274
|
-
```
|
|
275
|
-
import {If, IsAny, IsNever} from 'type-fest';
|
|
276
|
-
|
|
277
|
-
type A = If<IsAny<unknown>, 'is any', 'not any'>;
|
|
278
|
-
//=> 'not any'
|
|
279
|
-
|
|
280
|
-
type B = If<IsNever<never>, 'is never', 'not never'>;
|
|
281
|
-
//=> 'is never'
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
@example
|
|
285
|
-
```
|
|
286
|
-
import {If, IsEqual} from 'type-fest';
|
|
287
|
-
|
|
288
|
-
type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
|
|
289
|
-
|
|
290
|
-
type A = IfEqual<string, string, 'equal', 'not equal'>;
|
|
291
|
-
//=> 'equal'
|
|
292
|
-
|
|
293
|
-
type B = IfEqual<string, number, 'equal', 'not equal'>;
|
|
294
|
-
//=> 'not equal'
|
|
295
|
-
```
|
|
296
|
-
|
|
297
|
-
@category Type Guard
|
|
298
|
-
@category Utilities
|
|
299
|
-
*/
|
|
300
|
-
type If<Type extends boolean, IfBranch, ElseBranch> =
|
|
301
|
-
IsNever<Type> extends true
|
|
302
|
-
? ElseBranch
|
|
303
|
-
: Type extends true
|
|
304
|
-
? IfBranch
|
|
305
|
-
: ElseBranch;
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
Represents an array with `unknown` value.
|
|
309
|
-
|
|
310
|
-
Use case: You want a type that all arrays can be assigned to, but you don't care about the value.
|
|
311
|
-
|
|
312
|
-
@example
|
|
313
|
-
```
|
|
314
|
-
import type {UnknownArray} from 'type-fest';
|
|
315
|
-
|
|
316
|
-
type IsArray<T> = T extends UnknownArray ? true : false;
|
|
317
|
-
|
|
318
|
-
type A = IsArray<['foo']>;
|
|
319
|
-
//=> true
|
|
320
|
-
|
|
321
|
-
type B = IsArray<readonly number[]>;
|
|
322
|
-
//=> true
|
|
323
|
-
|
|
324
|
-
type C = IsArray<string>;
|
|
325
|
-
//=> false
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
@category Type
|
|
329
|
-
@category Array
|
|
330
|
-
*/
|
|
331
|
-
type UnknownArray = readonly unknown[];
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
335
|
-
*/
|
|
336
|
-
type BuiltIns = Primitive | void | Date | RegExp;
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
Matches non-recursive types.
|
|
340
|
-
*/
|
|
341
|
-
type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown);
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
Returns a boolean for whether the given `boolean` is not `false`.
|
|
345
|
-
*/
|
|
346
|
-
type IsNotFalse<T extends boolean> = [T] extends [false] ? false : true;
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
Returns a boolean for whether A is false.
|
|
350
|
-
|
|
351
|
-
@example
|
|
352
|
-
```
|
|
353
|
-
Not<true>;
|
|
354
|
-
//=> false
|
|
355
|
-
|
|
356
|
-
Not<false>;
|
|
357
|
-
//=> true
|
|
358
|
-
```
|
|
359
|
-
*/
|
|
360
|
-
type Not<A extends boolean> = A extends true
|
|
361
|
-
? false
|
|
362
|
-
: A extends false
|
|
363
|
-
? true
|
|
364
|
-
: never;
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
An if-else-like type that resolves depending on whether the given type is `any` or `never`.
|
|
368
|
-
|
|
369
|
-
@example
|
|
370
|
-
```
|
|
371
|
-
// When `T` is a NOT `any` or `never` (like `string`) => Returns `IfNotAnyOrNever` branch
|
|
372
|
-
type A = IfNotAnyOrNever<string, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
373
|
-
//=> 'VALID'
|
|
374
|
-
|
|
375
|
-
// When `T` is `any` => Returns `IfAny` branch
|
|
376
|
-
type B = IfNotAnyOrNever<any, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
377
|
-
//=> 'IS_ANY'
|
|
378
|
-
|
|
379
|
-
// When `T` is `never` => Returns `IfNever` branch
|
|
380
|
-
type C = IfNotAnyOrNever<never, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
381
|
-
//=> 'IS_NEVER'
|
|
382
|
-
```
|
|
383
|
-
*/
|
|
384
|
-
type IfNotAnyOrNever<T, IfNotAnyOrNever, IfAny = any, IfNever = never> =
|
|
385
|
-
If<IsAny<T>, IfAny, If<IsNever<T>, IfNever, IfNotAnyOrNever>>;
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
Returns a boolean for whether the given type is `any` or `never`.
|
|
389
|
-
|
|
390
|
-
This type can be better to use than {@link IfNotAnyOrNever `IfNotAnyOrNever`} in recursive types because it does not evaluate any branches.
|
|
391
|
-
|
|
392
|
-
@example
|
|
393
|
-
```
|
|
394
|
-
// When `T` is a NOT `any` or `never` (like `string`) => Returns `false`
|
|
395
|
-
type A = IsAnyOrNever<string>;
|
|
396
|
-
//=> false
|
|
397
|
-
|
|
398
|
-
// When `T` is `any` => Returns `true`
|
|
399
|
-
type B = IsAnyOrNever<any>;
|
|
400
|
-
//=> true
|
|
401
|
-
|
|
402
|
-
// When `T` is `never` => Returns `true`
|
|
403
|
-
type C = IsAnyOrNever<never>;
|
|
404
|
-
//=> true
|
|
405
|
-
```
|
|
406
|
-
*/
|
|
407
|
-
type IsAnyOrNever<T> = IsNotFalse<IsAny<T> | IsNever<T>>;
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
Indicates the value of `exactOptionalPropertyTypes` compiler option.
|
|
411
|
-
*/
|
|
412
|
-
type IsExactOptionalPropertyTypesEnabled = [(string | undefined)?] extends [string?]
|
|
413
|
-
? false
|
|
414
|
-
: true;
|
|
415
|
-
|
|
416
|
-
/**
|
|
417
|
-
Returns the static, fixed-length portion of the given array, excluding variable-length parts.
|
|
418
|
-
|
|
419
|
-
@example
|
|
420
|
-
```
|
|
421
|
-
type A = [string, number, boolean, ...string[]];
|
|
422
|
-
type B = StaticPartOfArray<A>;
|
|
423
|
-
//=> [string, number, boolean]
|
|
424
|
-
```
|
|
425
|
-
*/
|
|
426
|
-
type StaticPartOfArray<T extends UnknownArray, Result extends UnknownArray = []> =
|
|
427
|
-
T extends unknown
|
|
428
|
-
? number extends T['length'] ?
|
|
429
|
-
T extends readonly [infer U, ...infer V]
|
|
430
|
-
? StaticPartOfArray<V, [...Result, U]>
|
|
431
|
-
: Result
|
|
432
|
-
: T
|
|
433
|
-
: never; // Should never happen
|
|
434
|
-
|
|
435
|
-
/**
|
|
436
|
-
Returns the variable, non-fixed-length portion of the given array, excluding static-length parts.
|
|
437
|
-
|
|
438
|
-
@example
|
|
439
|
-
```
|
|
440
|
-
type A = [string, number, boolean, ...string[]];
|
|
441
|
-
type B = VariablePartOfArray<A>;
|
|
442
|
-
//=> string[]
|
|
443
|
-
```
|
|
444
|
-
*/
|
|
445
|
-
type VariablePartOfArray<T extends UnknownArray> =
|
|
446
|
-
T extends unknown
|
|
447
|
-
? T extends readonly [...StaticPartOfArray<T>, ...infer U]
|
|
448
|
-
? U
|
|
449
|
-
: []
|
|
450
|
-
: never;
|
|
451
|
-
|
|
452
|
-
/**
|
|
453
|
-
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.
|
|
454
|
-
|
|
455
|
-
@example
|
|
456
|
-
```
|
|
457
|
-
type A = CollapseRestElement<[string, string, ...number[]]>;
|
|
458
|
-
//=> [string, string, number]
|
|
459
|
-
|
|
460
|
-
type B = CollapseRestElement<[...string[], number, number]>;
|
|
461
|
-
//=> [string, number, number]
|
|
462
|
-
|
|
463
|
-
type C = CollapseRestElement<[string, string, ...Array<number | bigint>]>;
|
|
464
|
-
//=> [string, string, number | bigint]
|
|
465
|
-
|
|
466
|
-
type D = CollapseRestElement<[string, number]>;
|
|
467
|
-
//=> [string, number]
|
|
468
|
-
```
|
|
469
|
-
|
|
470
|
-
Note: Optional modifiers (`?`) are removed from elements unless the `exactOptionalPropertyTypes` compiler option is disabled. When disabled, there's an additional `| undefined` for optional elements.
|
|
471
|
-
|
|
472
|
-
@example
|
|
473
|
-
```
|
|
474
|
-
// `exactOptionalPropertyTypes` enabled
|
|
475
|
-
type A = CollapseRestElement<[string?, string?, ...number[]]>;
|
|
476
|
-
//=> [string, string, number]
|
|
477
|
-
|
|
478
|
-
// `exactOptionalPropertyTypes` disabled
|
|
479
|
-
type B = CollapseRestElement<[string?, string?, ...number[]]>;
|
|
480
|
-
//=> [string | undefined, string | undefined, number]
|
|
481
|
-
```
|
|
482
|
-
*/
|
|
483
|
-
type CollapseRestElement<TArray extends UnknownArray> = IfNotAnyOrNever<TArray, _CollapseRestElement<TArray>>;
|
|
484
|
-
|
|
485
|
-
type _CollapseRestElement<
|
|
486
|
-
TArray extends UnknownArray,
|
|
487
|
-
ForwardAccumulator extends UnknownArray = [],
|
|
488
|
-
BackwardAccumulator extends UnknownArray = [],
|
|
489
|
-
> =
|
|
490
|
-
TArray extends UnknownArray // For distributing `TArray`
|
|
491
|
-
? keyof TArray & `${number}` extends never
|
|
492
|
-
// Enters this branch, if `TArray` is empty (e.g., []),
|
|
493
|
-
// or `TArray` contains no non-rest elements preceding the rest element (e.g., `[...string[]]` or `[...string[], string]`).
|
|
494
|
-
? TArray extends readonly [...infer Rest, infer Last]
|
|
495
|
-
? _CollapseRestElement<Rest, ForwardAccumulator, [Last, ...BackwardAccumulator]> // Accumulate elements that are present after the rest element.
|
|
496
|
-
: TArray extends readonly []
|
|
497
|
-
? [...ForwardAccumulator, ...BackwardAccumulator]
|
|
498
|
-
: [...ForwardAccumulator, TArray[number], ...BackwardAccumulator] // Add the rest element between the accumulated elements.
|
|
499
|
-
: TArray extends readonly [(infer First)?, ...infer Rest]
|
|
500
|
-
? _CollapseRestElement<
|
|
501
|
-
Rest,
|
|
502
|
-
[
|
|
503
|
-
...ForwardAccumulator,
|
|
504
|
-
'0' extends OptionalKeysOf<TArray>
|
|
505
|
-
? If<IsExactOptionalPropertyTypesEnabled, First, First | undefined> // Add `| undefined` for optional elements, if `exactOptionalPropertyTypes` is disabled.
|
|
506
|
-
: First,
|
|
507
|
-
],
|
|
508
|
-
BackwardAccumulator
|
|
509
|
-
>
|
|
510
|
-
: never // Should never happen, since `[(infer First)?, ...infer Rest]` is a top-type for arrays.
|
|
511
|
-
: never; // Should never happen
|
|
512
|
-
|
|
513
|
-
type Numeric = number | bigint;
|
|
514
|
-
|
|
515
|
-
type Zero = 0 | 0n;
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
Matches the hidden `Infinity` type.
|
|
519
|
-
|
|
520
|
-
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
|
|
521
|
-
|
|
522
|
-
@see NegativeInfinity
|
|
523
|
-
|
|
524
|
-
@category Numeric
|
|
525
|
-
*/
|
|
526
|
-
// See https://github.com/microsoft/TypeScript/issues/31752
|
|
527
|
-
// eslint-disable-next-line no-loss-of-precision
|
|
528
|
-
type PositiveInfinity = 1e999;
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
Matches the hidden `-Infinity` type.
|
|
532
|
-
|
|
533
|
-
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
|
|
534
|
-
|
|
535
|
-
@see PositiveInfinity
|
|
536
|
-
|
|
537
|
-
@category Numeric
|
|
538
|
-
*/
|
|
539
|
-
// See https://github.com/microsoft/TypeScript/issues/31752
|
|
540
|
-
// eslint-disable-next-line no-loss-of-precision
|
|
541
|
-
type NegativeInfinity = -1e999;
|
|
542
|
-
|
|
543
|
-
/**
|
|
544
|
-
A negative `number`/`bigint` (`-∞ < x < 0`)
|
|
545
|
-
|
|
546
|
-
Use-case: Validating and documenting parameters.
|
|
547
|
-
|
|
548
|
-
@see NegativeInteger
|
|
549
|
-
@see NonNegative
|
|
550
|
-
|
|
551
|
-
@category Numeric
|
|
552
|
-
*/
|
|
553
|
-
type Negative<T extends Numeric> = T extends Zero ? never : `${T}` extends `-${string}` ? T : never;
|
|
554
|
-
|
|
555
|
-
/**
|
|
556
|
-
Returns a boolean for whether the given number is a negative number.
|
|
557
|
-
|
|
558
|
-
@see Negative
|
|
559
|
-
|
|
560
|
-
@example
|
|
561
|
-
```
|
|
562
|
-
import type {IsNegative} from 'type-fest';
|
|
563
|
-
|
|
564
|
-
type ShouldBeFalse = IsNegative<1>;
|
|
565
|
-
type ShouldBeTrue = IsNegative<-1>;
|
|
566
|
-
```
|
|
567
|
-
|
|
568
|
-
@category Numeric
|
|
569
|
-
*/
|
|
570
|
-
type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false;
|
|
571
|
-
|
|
572
|
-
/**
|
|
573
|
-
Returns a boolean for whether the two given types are equal.
|
|
574
|
-
|
|
575
|
-
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
576
|
-
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
577
|
-
|
|
578
|
-
Use-cases:
|
|
579
|
-
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
580
|
-
|
|
581
|
-
@example
|
|
582
|
-
```
|
|
583
|
-
import type {IsEqual} from 'type-fest';
|
|
584
|
-
|
|
585
|
-
// This type returns a boolean for whether the given array includes the given item.
|
|
586
|
-
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
587
|
-
type Includes<Value extends readonly any[], Item> =
|
|
588
|
-
Value extends readonly [Value[0], ...infer rest]
|
|
589
|
-
? IsEqual<Value[0], Item> extends true
|
|
590
|
-
? true
|
|
591
|
-
: Includes<rest, Item>
|
|
592
|
-
: false;
|
|
593
|
-
```
|
|
594
|
-
|
|
595
|
-
@category Type Guard
|
|
596
|
-
@category Utilities
|
|
597
|
-
*/
|
|
598
|
-
type IsEqual<A, B> =
|
|
599
|
-
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
600
|
-
(<G>() => G extends B & G | G ? 1 : 2)
|
|
601
|
-
? true
|
|
602
|
-
: false;
|
|
603
|
-
|
|
604
|
-
/**
|
|
605
|
-
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.
|
|
606
|
-
|
|
607
|
-
@example
|
|
608
|
-
```
|
|
609
|
-
import type {Simplify} from 'type-fest';
|
|
610
|
-
|
|
611
|
-
type PositionProps = {
|
|
612
|
-
top: number;
|
|
613
|
-
left: number;
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
type SizeProps = {
|
|
617
|
-
width: number;
|
|
618
|
-
height: number;
|
|
619
|
-
};
|
|
620
|
-
|
|
621
|
-
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
622
|
-
type Props = Simplify<PositionProps & SizeProps>;
|
|
623
|
-
```
|
|
624
|
-
|
|
625
|
-
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.
|
|
626
|
-
|
|
627
|
-
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`.
|
|
628
|
-
|
|
629
|
-
@example
|
|
630
|
-
```
|
|
631
|
-
import type {Simplify} from 'type-fest';
|
|
632
|
-
|
|
633
|
-
interface SomeInterface {
|
|
634
|
-
foo: number;
|
|
635
|
-
bar?: string;
|
|
636
|
-
baz: number | undefined;
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
type SomeType = {
|
|
640
|
-
foo: number;
|
|
641
|
-
bar?: string;
|
|
642
|
-
baz: number | undefined;
|
|
643
|
-
};
|
|
644
|
-
|
|
645
|
-
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
646
|
-
const someType: SomeType = literal;
|
|
647
|
-
const someInterface: SomeInterface = literal;
|
|
648
|
-
|
|
649
|
-
function fn(object: Record<string, unknown>): void {}
|
|
650
|
-
|
|
651
|
-
fn(literal); // Good: literal object type is sealed
|
|
652
|
-
fn(someType); // Good: type is sealed
|
|
653
|
-
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
654
|
-
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
655
|
-
```
|
|
656
|
-
|
|
657
|
-
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
658
|
-
@see SimplifyDeep
|
|
659
|
-
@category Object
|
|
660
|
-
*/
|
|
661
|
-
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
662
|
-
|
|
663
|
-
/**
|
|
664
|
-
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
665
|
-
|
|
666
|
-
This is the counterpart of `PickIndexSignature`.
|
|
667
|
-
|
|
668
|
-
Use-cases:
|
|
669
|
-
- Remove overly permissive signatures from third-party types.
|
|
670
|
-
|
|
671
|
-
This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
|
|
672
|
-
|
|
673
|
-
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>`.
|
|
674
|
-
|
|
675
|
-
(The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
|
|
676
|
-
|
|
677
|
-
```
|
|
678
|
-
const indexed: Record<string, unknown> = {}; // Allowed
|
|
679
|
-
|
|
680
|
-
const keyed: Record<'foo', unknown> = {}; // Error
|
|
681
|
-
// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
682
|
-
```
|
|
683
|
-
|
|
684
|
-
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:
|
|
685
|
-
|
|
686
|
-
```
|
|
687
|
-
type Indexed = {} extends Record<string, unknown>
|
|
688
|
-
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
689
|
-
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
690
|
-
// => '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
691
|
-
|
|
692
|
-
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
693
|
-
? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
|
|
694
|
-
: "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
|
|
695
|
-
// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
|
|
696
|
-
```
|
|
697
|
-
|
|
698
|
-
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`...
|
|
699
|
-
|
|
700
|
-
```
|
|
701
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
702
|
-
|
|
703
|
-
type OmitIndexSignature<ObjectType> = {
|
|
704
|
-
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
705
|
-
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
706
|
-
};
|
|
707
|
-
```
|
|
708
|
-
|
|
709
|
-
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
710
|
-
|
|
711
|
-
```
|
|
712
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
713
|
-
|
|
714
|
-
type OmitIndexSignature<ObjectType> = {
|
|
715
|
-
[KeyType in keyof ObjectType
|
|
716
|
-
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
717
|
-
as {} extends Record<KeyType, unknown>
|
|
718
|
-
? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
719
|
-
: ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
720
|
-
]: ObjectType[KeyType];
|
|
721
|
-
};
|
|
722
|
-
```
|
|
723
|
-
|
|
724
|
-
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.
|
|
725
|
-
|
|
726
|
-
@example
|
|
727
|
-
```
|
|
728
|
-
import type {OmitIndexSignature} from 'type-fest';
|
|
729
|
-
|
|
730
|
-
interface Example {
|
|
731
|
-
// These index signatures will be removed.
|
|
732
|
-
[x: string]: any
|
|
733
|
-
[x: number]: any
|
|
734
|
-
[x: symbol]: any
|
|
735
|
-
[x: `head-${string}`]: string
|
|
736
|
-
[x: `${string}-tail`]: string
|
|
737
|
-
[x: `head-${string}-tail`]: string
|
|
738
|
-
[x: `${bigint}`]: string
|
|
739
|
-
[x: `embedded-${number}`]: string
|
|
740
|
-
|
|
741
|
-
// These explicitly defined keys will remain.
|
|
742
|
-
foo: 'bar';
|
|
743
|
-
qux?: 'baz';
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
747
|
-
// => { foo: 'bar'; qux?: 'baz' | undefined; }
|
|
748
|
-
```
|
|
749
|
-
|
|
750
|
-
@see PickIndexSignature
|
|
751
|
-
@category Object
|
|
752
|
-
*/
|
|
753
|
-
type OmitIndexSignature<ObjectType> = {
|
|
754
|
-
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
755
|
-
? never
|
|
756
|
-
: KeyType]: ObjectType[KeyType];
|
|
757
|
-
};
|
|
758
|
-
|
|
759
|
-
/**
|
|
760
|
-
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
|
761
|
-
|
|
762
|
-
This is the counterpart of `OmitIndexSignature`.
|
|
763
|
-
|
|
764
|
-
@example
|
|
765
|
-
```
|
|
766
|
-
import type {PickIndexSignature} from 'type-fest';
|
|
767
|
-
|
|
768
|
-
declare const symbolKey: unique symbol;
|
|
769
|
-
|
|
770
|
-
type Example = {
|
|
771
|
-
// These index signatures will remain.
|
|
772
|
-
[x: string]: unknown;
|
|
773
|
-
[x: number]: unknown;
|
|
774
|
-
[x: symbol]: unknown;
|
|
775
|
-
[x: `head-${string}`]: string;
|
|
776
|
-
[x: `${string}-tail`]: string;
|
|
777
|
-
[x: `head-${string}-tail`]: string;
|
|
778
|
-
[x: `${bigint}`]: string;
|
|
779
|
-
[x: `embedded-${number}`]: string;
|
|
780
|
-
|
|
781
|
-
// These explicitly defined keys will be removed.
|
|
782
|
-
['kebab-case-key']: string;
|
|
783
|
-
[symbolKey]: string;
|
|
784
|
-
foo: 'bar';
|
|
785
|
-
qux?: 'baz';
|
|
786
|
-
};
|
|
787
|
-
|
|
788
|
-
type ExampleIndexSignature = PickIndexSignature<Example>;
|
|
789
|
-
// {
|
|
790
|
-
// [x: string]: unknown;
|
|
791
|
-
// [x: number]: unknown;
|
|
792
|
-
// [x: symbol]: unknown;
|
|
793
|
-
// [x: `head-${string}`]: string;
|
|
794
|
-
// [x: `${string}-tail`]: string;
|
|
795
|
-
// [x: `head-${string}-tail`]: string;
|
|
796
|
-
// [x: `${bigint}`]: string;
|
|
797
|
-
// [x: `embedded-${number}`]: string;
|
|
798
|
-
// }
|
|
799
|
-
```
|
|
800
|
-
|
|
801
|
-
@see OmitIndexSignature
|
|
802
|
-
@category Object
|
|
803
|
-
*/
|
|
804
|
-
type PickIndexSignature<ObjectType> = {
|
|
805
|
-
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
806
|
-
? KeyType
|
|
807
|
-
: never]: ObjectType[KeyType];
|
|
808
|
-
};
|
|
809
|
-
|
|
810
|
-
// Merges two objects without worrying about index signatures.
|
|
811
|
-
type SimpleMerge<Destination, Source> = {
|
|
812
|
-
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
813
|
-
} & Source;
|
|
814
|
-
|
|
815
|
-
/**
|
|
816
|
-
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
817
|
-
|
|
818
|
-
@example
|
|
819
|
-
```
|
|
820
|
-
import type {Merge} from 'type-fest';
|
|
821
|
-
|
|
822
|
-
interface Foo {
|
|
823
|
-
[x: string]: unknown;
|
|
824
|
-
[x: number]: unknown;
|
|
825
|
-
foo: string;
|
|
826
|
-
bar: symbol;
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
type Bar = {
|
|
830
|
-
[x: number]: number;
|
|
831
|
-
[x: symbol]: unknown;
|
|
832
|
-
bar: Date;
|
|
833
|
-
baz: boolean;
|
|
834
|
-
};
|
|
835
|
-
|
|
836
|
-
export type FooBar = Merge<Foo, Bar>;
|
|
837
|
-
// => {
|
|
838
|
-
// [x: string]: unknown;
|
|
839
|
-
// [x: number]: number;
|
|
840
|
-
// [x: symbol]: unknown;
|
|
841
|
-
// foo: string;
|
|
842
|
-
// bar: Date;
|
|
843
|
-
// baz: boolean;
|
|
844
|
-
// }
|
|
845
|
-
```
|
|
846
|
-
|
|
847
|
-
@category Object
|
|
848
|
-
*/
|
|
849
|
-
type Merge<Destination, Source> =
|
|
850
|
-
Simplify<
|
|
851
|
-
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
852
|
-
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
853
|
-
>;
|
|
854
|
-
|
|
855
|
-
/**
|
|
856
|
-
Merges user specified options with default options.
|
|
857
|
-
|
|
858
|
-
@example
|
|
859
|
-
```
|
|
860
|
-
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
861
|
-
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
862
|
-
type SpecifiedOptions = {leavesOnly: true};
|
|
863
|
-
|
|
864
|
-
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
865
|
-
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
866
|
-
```
|
|
867
|
-
|
|
868
|
-
@example
|
|
869
|
-
```
|
|
870
|
-
// Complains if default values are not provided for optional options
|
|
871
|
-
|
|
872
|
-
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
873
|
-
type DefaultPathsOptions = {maxRecursionDepth: 10};
|
|
874
|
-
type SpecifiedOptions = {};
|
|
875
|
-
|
|
876
|
-
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
877
|
-
// ~~~~~~~~~~~~~~~~~~~
|
|
878
|
-
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
879
|
-
```
|
|
880
|
-
|
|
881
|
-
@example
|
|
882
|
-
```
|
|
883
|
-
// Complains if an option's default type does not conform to the expected type
|
|
884
|
-
|
|
885
|
-
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
886
|
-
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
887
|
-
type SpecifiedOptions = {};
|
|
888
|
-
|
|
889
|
-
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
890
|
-
// ~~~~~~~~~~~~~~~~~~~
|
|
891
|
-
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
892
|
-
```
|
|
893
|
-
|
|
894
|
-
@example
|
|
895
|
-
```
|
|
896
|
-
// Complains if an option's specified type does not conform to the expected type
|
|
897
|
-
|
|
898
|
-
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
899
|
-
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
900
|
-
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
901
|
-
|
|
902
|
-
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
903
|
-
// ~~~~~~~~~~~~~~~~
|
|
904
|
-
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
905
|
-
```
|
|
906
|
-
*/
|
|
907
|
-
type ApplyDefaultOptions<
|
|
908
|
-
Options extends object,
|
|
909
|
-
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
|
|
910
|
-
SpecifiedOptions extends Options,
|
|
911
|
-
> =
|
|
912
|
-
If<IsAny<SpecifiedOptions>, Defaults,
|
|
913
|
-
If<IsNever<SpecifiedOptions>, Defaults,
|
|
914
|
-
Simplify<Merge<Defaults, {
|
|
915
|
-
[Key in keyof SpecifiedOptions
|
|
916
|
-
as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
|
|
917
|
-
]: SpecifiedOptions[Key]
|
|
918
|
-
}> & Required<Options>>>>;
|
|
919
|
-
|
|
920
|
-
/**
|
|
921
|
-
Returns a boolean for whether either of two given types are true.
|
|
922
|
-
|
|
923
|
-
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
|
|
924
|
-
|
|
925
|
-
@example
|
|
926
|
-
```
|
|
927
|
-
import type {Or} from 'type-fest';
|
|
928
|
-
|
|
929
|
-
type TT = Or<true, false>;
|
|
930
|
-
//=> true
|
|
931
|
-
|
|
932
|
-
type TF = Or<true, false>;
|
|
933
|
-
//=> true
|
|
934
|
-
|
|
935
|
-
type FT = Or<false, true>;
|
|
936
|
-
//=> true
|
|
937
|
-
|
|
938
|
-
type FF = Or<false, false>;
|
|
939
|
-
//=> false
|
|
940
|
-
```
|
|
941
|
-
|
|
942
|
-
Note: When `boolean` is passed as an argument, it is distributed into separate cases, and the final result is a union of those cases.
|
|
943
|
-
For example, `And<false, boolean>` expands to `And<false, true> | And<false, false>`, which simplifies to `true | false` (i.e., `boolean`).
|
|
944
|
-
@example
|
|
945
|
-
```
|
|
946
|
-
import type {And} from 'type-fest';
|
|
947
|
-
|
|
948
|
-
type A = Or<false, boolean>;
|
|
949
|
-
//=> boolean
|
|
950
|
-
|
|
951
|
-
type B = Or<boolean, false>;
|
|
952
|
-
//=> boolean
|
|
953
|
-
|
|
954
|
-
type C = Or<true, boolean>;
|
|
955
|
-
//=> true
|
|
956
|
-
|
|
957
|
-
type D = Or<boolean, true>;
|
|
958
|
-
//=> true
|
|
959
|
-
|
|
960
|
-
type E = Or<boolean, boolean>;
|
|
961
|
-
//=> boolean
|
|
962
|
-
```
|
|
963
|
-
|
|
964
|
-
Note: If `never` is passed as an argument, it is treated as `false` and the result is computed accordingly.
|
|
965
|
-
|
|
966
|
-
@example
|
|
967
|
-
```
|
|
968
|
-
import type {Or} from 'type-fest';
|
|
969
|
-
|
|
970
|
-
type A = Or<true, never>;
|
|
971
|
-
//=> true
|
|
972
|
-
|
|
973
|
-
type B = Or<never, true>;
|
|
974
|
-
//=> true
|
|
975
|
-
|
|
976
|
-
type C = Or<false, never>;
|
|
977
|
-
//=> false
|
|
978
|
-
|
|
979
|
-
type D = Or<never, false>;
|
|
980
|
-
//=> false
|
|
981
|
-
|
|
982
|
-
type E = Or<boolean, never>;
|
|
983
|
-
//=> boolean
|
|
984
|
-
|
|
985
|
-
type F = Or<never, boolean>;
|
|
986
|
-
//=> boolean
|
|
987
|
-
|
|
988
|
-
type G = Or<never, never>;
|
|
989
|
-
//=> false
|
|
990
|
-
```
|
|
991
|
-
|
|
992
|
-
@see {@link And}
|
|
993
|
-
*/
|
|
994
|
-
type Or<A extends boolean, B extends boolean> =
|
|
995
|
-
_Or<If<IsNever<A>, false, A>, If<IsNever<B>, false, B>>; // `never` is treated as `false`
|
|
996
|
-
|
|
997
|
-
type _Or<A extends boolean, B extends boolean> = A extends true
|
|
998
|
-
? true
|
|
999
|
-
: B extends true
|
|
1000
|
-
? true
|
|
1001
|
-
: false;
|
|
1002
|
-
|
|
1003
|
-
/**
|
|
1004
|
-
@see {@link AllExtend}
|
|
1005
|
-
*/
|
|
1006
|
-
type AllExtendOptions = {
|
|
1007
|
-
/**
|
|
1008
|
-
Consider `never` elements to match the target type only if the target type itself is `never` (or `any`).
|
|
1009
|
-
|
|
1010
|
-
- 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`).
|
|
1011
|
-
- When set to `false`, `never` is treated as a bottom type, and behaves as it normally would.
|
|
1012
|
-
|
|
1013
|
-
@default true
|
|
1014
|
-
|
|
1015
|
-
@example
|
|
1016
|
-
```
|
|
1017
|
-
import type {AllExtend} from 'type-fest';
|
|
1018
|
-
|
|
1019
|
-
type A = AllExtend<[1, 2, never], number, {strictNever: true}>;
|
|
1020
|
-
//=> false
|
|
1021
|
-
|
|
1022
|
-
type B = AllExtend<[1, 2, never], number, {strictNever: false}>;
|
|
1023
|
-
//=> true
|
|
1024
|
-
|
|
1025
|
-
type C = AllExtend<[never, never], never, {strictNever: true}>;
|
|
1026
|
-
//=> true
|
|
1027
|
-
|
|
1028
|
-
type D = AllExtend<[never, never], never, {strictNever: false}>;
|
|
1029
|
-
//=> true
|
|
1030
|
-
|
|
1031
|
-
type E = AllExtend<['a', 'b', never], any, {strictNever: true}>;
|
|
1032
|
-
//=> true
|
|
1033
|
-
|
|
1034
|
-
type F = AllExtend<['a', 'b', never], any, {strictNever: false}>;
|
|
1035
|
-
//=> true
|
|
1036
|
-
|
|
1037
|
-
type G = AllExtend<[never, 1], never, {strictNever: true}>;
|
|
1038
|
-
//=> false
|
|
1039
|
-
|
|
1040
|
-
type H = AllExtend<[never, 1], never, {strictNever: false}>;
|
|
1041
|
-
//=> false
|
|
1042
|
-
```
|
|
1043
|
-
*/
|
|
1044
|
-
strictNever?: boolean;
|
|
1045
|
-
};
|
|
1046
|
-
|
|
1047
|
-
type DefaultAllExtendOptions = {
|
|
1048
|
-
strictNever: true;
|
|
1049
|
-
};
|
|
1050
|
-
|
|
1051
|
-
/**
|
|
1052
|
-
Returns a boolean for whether every element in an array type extends another type.
|
|
1053
|
-
|
|
1054
|
-
@example
|
|
1055
|
-
```
|
|
1056
|
-
import type {AllExtend} from 'type-fest';
|
|
1057
|
-
|
|
1058
|
-
type A = AllExtend<[1, 2, 3], number>;
|
|
1059
|
-
//=> true
|
|
1060
|
-
|
|
1061
|
-
type B = AllExtend<[1, 2, '3'], number>;
|
|
1062
|
-
//=> false
|
|
1063
|
-
|
|
1064
|
-
type C = AllExtend<[number, number | string], number>;
|
|
1065
|
-
//=> boolean
|
|
1066
|
-
|
|
1067
|
-
type D = AllExtend<[true, boolean, true], true>;
|
|
1068
|
-
//=> boolean
|
|
1069
|
-
```
|
|
1070
|
-
|
|
1071
|
-
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.
|
|
1072
|
-
|
|
1073
|
-
```
|
|
1074
|
-
import type {AllExtend} from 'type-fest';
|
|
1075
|
-
|
|
1076
|
-
// `exactOptionalPropertyTypes` enabled
|
|
1077
|
-
type A = AllExtend<[1?, 2?, 3?], number>;
|
|
1078
|
-
//=> true
|
|
1079
|
-
|
|
1080
|
-
// `exactOptionalPropertyTypes` disabled
|
|
1081
|
-
type B = AllExtend<[1?, 2?, 3?], number>;
|
|
1082
|
-
//=> false
|
|
1083
|
-
|
|
1084
|
-
// `exactOptionalPropertyTypes` disabled
|
|
1085
|
-
type C = AllExtend<[1?, 2?, 3?], number | undefined>;
|
|
1086
|
-
//=> true
|
|
1087
|
-
```
|
|
1088
|
-
|
|
1089
|
-
@see {@link AllExtendOptions}
|
|
1090
|
-
|
|
1091
|
-
@category Utilities
|
|
1092
|
-
@category Array
|
|
1093
|
-
*/
|
|
1094
|
-
type AllExtend<TArray extends UnknownArray, Type, Options extends AllExtendOptions = {}> =
|
|
1095
|
-
_AllExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<AllExtendOptions, DefaultAllExtendOptions, Options>>;
|
|
1096
|
-
|
|
1097
|
-
type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray, If<IsAny<Type>, true,
|
|
1098
|
-
TArray extends readonly [infer First, ...infer Rest]
|
|
1099
|
-
? IsNever<First> extends true
|
|
1100
|
-
? Or<IsNever<Type>, Not<Options['strictNever']>> extends true
|
|
1101
|
-
// If target `Type` is also `never` OR `strictNever` is disabled, recurse further.
|
|
1102
|
-
? _AllExtend<Rest, Type, Options>
|
|
1103
|
-
: false
|
|
1104
|
-
: First extends Type
|
|
1105
|
-
? _AllExtend<Rest, Type, Options>
|
|
1106
|
-
: false
|
|
1107
|
-
: true
|
|
1108
|
-
>, false, false>;
|
|
1109
|
-
|
|
1110
|
-
/**
|
|
1111
|
-
Returns a boolean for whether two given types are both true.
|
|
1112
|
-
|
|
1113
|
-
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
|
|
1114
|
-
|
|
1115
|
-
@example
|
|
1116
|
-
```
|
|
1117
|
-
import type {And} from 'type-fest';
|
|
1118
|
-
|
|
1119
|
-
type TT = And<true, true>;
|
|
1120
|
-
//=> true
|
|
1121
|
-
|
|
1122
|
-
type TF = And<true, false>;
|
|
1123
|
-
//=> false
|
|
1124
|
-
|
|
1125
|
-
type FT = And<false, true>;
|
|
1126
|
-
//=> false
|
|
1127
|
-
|
|
1128
|
-
type FF = And<false, false>;
|
|
1129
|
-
//=> false
|
|
1130
|
-
```
|
|
1131
|
-
|
|
1132
|
-
Note: When `boolean` is passed as an argument, it is distributed into separate cases, and the final result is a union of those cases.
|
|
1133
|
-
For example, `And<true, boolean>` expands to `And<true, true> | And<true, false>`, which simplifies to `true | false` (i.e., `boolean`).
|
|
1134
|
-
|
|
1135
|
-
@example
|
|
1136
|
-
```
|
|
1137
|
-
import type {And} from 'type-fest';
|
|
1138
|
-
|
|
1139
|
-
type A = And<true, boolean>;
|
|
1140
|
-
//=> boolean
|
|
1141
|
-
|
|
1142
|
-
type B = And<boolean, true>;
|
|
1143
|
-
//=> boolean
|
|
1144
|
-
|
|
1145
|
-
type C = And<false, boolean>;
|
|
1146
|
-
//=> false
|
|
1147
|
-
|
|
1148
|
-
type D = And<boolean, false>;
|
|
1149
|
-
//=> false
|
|
1150
|
-
|
|
1151
|
-
type E = And<boolean, boolean>;
|
|
1152
|
-
//=> boolean
|
|
1153
|
-
```
|
|
1154
|
-
|
|
1155
|
-
Note: If either of the types is `never`, the result becomes `false`.
|
|
1156
|
-
@example
|
|
1157
|
-
```
|
|
1158
|
-
import type {And} from 'type-fest';
|
|
1159
|
-
|
|
1160
|
-
type A = And<true, never>;
|
|
1161
|
-
//=> false
|
|
1162
|
-
|
|
1163
|
-
type B = And<never, true>;
|
|
1164
|
-
//=> false
|
|
1165
|
-
|
|
1166
|
-
type C = And<false, never>;
|
|
1167
|
-
//=> false
|
|
1168
|
-
|
|
1169
|
-
type D = And<never, false>;
|
|
1170
|
-
//=> false
|
|
1171
|
-
|
|
1172
|
-
type E = And<boolean, never>;
|
|
1173
|
-
//=> false
|
|
1174
|
-
|
|
1175
|
-
type F = And<never, boolean>;
|
|
1176
|
-
//=> false
|
|
1177
|
-
|
|
1178
|
-
type G = And<never, never>;
|
|
1179
|
-
//=> false
|
|
1180
|
-
```
|
|
1181
|
-
|
|
1182
|
-
@see {@link Or}
|
|
1183
|
-
*/
|
|
1184
|
-
type And<A extends boolean, B extends boolean> = AllExtend<[A, B], true>;
|
|
1185
|
-
|
|
1186
|
-
/**
|
|
1187
|
-
Returns a boolean for whether a given number is greater than another number.
|
|
1188
|
-
|
|
1189
|
-
@example
|
|
1190
|
-
```
|
|
1191
|
-
import type {GreaterThan} from 'type-fest';
|
|
1192
|
-
|
|
1193
|
-
GreaterThan<1, -5>;
|
|
1194
|
-
//=> true
|
|
1195
|
-
|
|
1196
|
-
GreaterThan<1, 1>;
|
|
1197
|
-
//=> false
|
|
1198
|
-
|
|
1199
|
-
GreaterThan<1, 5>;
|
|
1200
|
-
//=> false
|
|
1201
|
-
```
|
|
1202
|
-
*/
|
|
1203
|
-
type GreaterThan<A extends number, B extends number> =
|
|
1204
|
-
A extends number // For distributing `A`
|
|
1205
|
-
? B extends number // For distributing `B`
|
|
1206
|
-
? number extends A | B
|
|
1207
|
-
? never
|
|
1208
|
-
: [
|
|
1209
|
-
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
1210
|
-
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
1211
|
-
] extends infer R extends [boolean, boolean, boolean, boolean]
|
|
1212
|
-
? Or<
|
|
1213
|
-
And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
|
|
1214
|
-
And<IsEqual<R[3], true>, IsEqual<R[1], false>>
|
|
1215
|
-
> extends true
|
|
1216
|
-
? true
|
|
1217
|
-
: Or<
|
|
1218
|
-
And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
|
|
1219
|
-
And<IsEqual<R[2], true>, IsEqual<R[0], false>>
|
|
1220
|
-
> extends true
|
|
1221
|
-
? false
|
|
1222
|
-
: true extends R[number]
|
|
1223
|
-
? false
|
|
1224
|
-
: [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean]
|
|
1225
|
-
? [true, false] extends R
|
|
1226
|
-
? false
|
|
1227
|
-
: [false, true] extends R
|
|
1228
|
-
? true
|
|
1229
|
-
: [false, false] extends R
|
|
1230
|
-
? PositiveNumericStringGt<`${A}`, `${B}`>
|
|
1231
|
-
: PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
|
|
1232
|
-
: never
|
|
1233
|
-
: never
|
|
1234
|
-
: never // Should never happen
|
|
1235
|
-
: never; // Should never happen
|
|
1236
|
-
|
|
1237
|
-
/**
|
|
1238
|
-
Returns a boolean for whether a given number is greater than or equal to another number.
|
|
1239
|
-
|
|
1240
|
-
@example
|
|
1241
|
-
```
|
|
1242
|
-
import type {GreaterThanOrEqual} from 'type-fest';
|
|
1243
|
-
|
|
1244
|
-
GreaterThanOrEqual<1, -5>;
|
|
1245
|
-
//=> true
|
|
1246
|
-
|
|
1247
|
-
GreaterThanOrEqual<1, 1>;
|
|
1248
|
-
//=> true
|
|
1249
|
-
|
|
1250
|
-
GreaterThanOrEqual<1, 5>;
|
|
1251
|
-
//=> false
|
|
1252
|
-
```
|
|
1253
|
-
*/
|
|
1254
|
-
type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
|
|
1255
|
-
? never
|
|
1256
|
-
: A extends B ? true : GreaterThan<A, B>;
|
|
1257
|
-
|
|
1258
|
-
/**
|
|
1259
|
-
Returns a boolean for whether a given number is less than another number.
|
|
1260
|
-
|
|
1261
|
-
@example
|
|
1262
|
-
```
|
|
1263
|
-
import type {LessThan} from 'type-fest';
|
|
1264
|
-
|
|
1265
|
-
LessThan<1, -5>;
|
|
1266
|
-
//=> false
|
|
1267
|
-
|
|
1268
|
-
LessThan<1, 1>;
|
|
1269
|
-
//=> false
|
|
1270
|
-
|
|
1271
|
-
LessThan<1, 5>;
|
|
1272
|
-
//=> true
|
|
1273
|
-
```
|
|
1274
|
-
*/
|
|
1275
|
-
type LessThan<A extends number, B extends number> = number extends A | B
|
|
1276
|
-
? never
|
|
1277
|
-
: GreaterThanOrEqual<A, B> extends infer Result
|
|
1278
|
-
? Result extends true
|
|
1279
|
-
? false
|
|
1280
|
-
: true
|
|
1281
|
-
: never; // Should never happen
|
|
1282
|
-
|
|
1283
|
-
// Should never happen
|
|
1284
|
-
|
|
1285
|
-
/**
|
|
1286
|
-
Create a tuple type of the given length `<L>` and fill it with the given type `<Fill>`.
|
|
1287
|
-
|
|
1288
|
-
If `<Fill>` is not provided, it will default to `unknown`.
|
|
1289
|
-
|
|
1290
|
-
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
1291
|
-
*/
|
|
1292
|
-
type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = number extends L
|
|
1293
|
-
? Fill[]
|
|
1294
|
-
: L extends T['length']
|
|
1295
|
-
? T
|
|
1296
|
-
: BuildTuple<L, Fill, [...T, Fill]>;
|
|
1297
|
-
|
|
1298
|
-
/**
|
|
1299
|
-
Return a string representation of the given string or number.
|
|
1300
|
-
|
|
1301
|
-
Note: This type is not the return type of the `.toString()` function.
|
|
1302
|
-
*/
|
|
1303
|
-
type ToString<T> = T extends string | number ? `${T}` : never;
|
|
1304
|
-
|
|
1305
|
-
/**
|
|
1306
|
-
Converts a numeric string to a number.
|
|
1307
|
-
|
|
1308
|
-
@example
|
|
1309
|
-
```
|
|
1310
|
-
type PositiveInt = StringToNumber<'1234'>;
|
|
1311
|
-
//=> 1234
|
|
1312
|
-
|
|
1313
|
-
type NegativeInt = StringToNumber<'-1234'>;
|
|
1314
|
-
//=> -1234
|
|
1315
|
-
|
|
1316
|
-
type PositiveFloat = StringToNumber<'1234.56'>;
|
|
1317
|
-
//=> 1234.56
|
|
1318
|
-
|
|
1319
|
-
type NegativeFloat = StringToNumber<'-1234.56'>;
|
|
1320
|
-
//=> -1234.56
|
|
1321
|
-
|
|
1322
|
-
type PositiveInfinity = StringToNumber<'Infinity'>;
|
|
1323
|
-
//=> Infinity
|
|
1324
|
-
|
|
1325
|
-
type NegativeInfinity = StringToNumber<'-Infinity'>;
|
|
1326
|
-
//=> -Infinity
|
|
1327
|
-
```
|
|
1328
|
-
|
|
1329
|
-
@category String
|
|
1330
|
-
@category Numeric
|
|
1331
|
-
@category Template literal
|
|
1332
|
-
*/
|
|
1333
|
-
type StringToNumber<S extends string> = S extends `${infer N extends number}`
|
|
1334
|
-
? N
|
|
1335
|
-
: S extends 'Infinity'
|
|
1336
|
-
? PositiveInfinity
|
|
1337
|
-
: S extends '-Infinity'
|
|
1338
|
-
? NegativeInfinity
|
|
1339
|
-
: never;
|
|
1340
|
-
|
|
1341
|
-
/**
|
|
1342
|
-
Returns an array of the characters of the string.
|
|
1343
|
-
|
|
1344
|
-
@example
|
|
1345
|
-
```
|
|
1346
|
-
StringToArray<'abcde'>;
|
|
1347
|
-
//=> ['a', 'b', 'c', 'd', 'e']
|
|
1348
|
-
|
|
1349
|
-
StringToArray<string>;
|
|
1350
|
-
//=> never
|
|
1351
|
-
```
|
|
1352
|
-
|
|
1353
|
-
@category String
|
|
1354
|
-
*/
|
|
1355
|
-
type StringToArray<S extends string, Result extends string[] = []> = string extends S
|
|
1356
|
-
? never
|
|
1357
|
-
: S extends `${infer F}${infer R}`
|
|
1358
|
-
? StringToArray<R, [...Result, F]>
|
|
1359
|
-
: Result;
|
|
1360
|
-
|
|
1361
|
-
/**
|
|
1362
|
-
Returns the length of the given string.
|
|
1363
|
-
|
|
1364
|
-
@example
|
|
1365
|
-
```
|
|
1366
|
-
StringLength<'abcde'>;
|
|
1367
|
-
//=> 5
|
|
1368
|
-
|
|
1369
|
-
StringLength<string>;
|
|
1370
|
-
//=> never
|
|
1371
|
-
```
|
|
1372
|
-
|
|
1373
|
-
@category String
|
|
1374
|
-
@category Template literal
|
|
1375
|
-
*/
|
|
1376
|
-
type StringLength<S extends string> = string extends S
|
|
1377
|
-
? never
|
|
1378
|
-
: StringToArray<S>['length'];
|
|
1379
|
-
|
|
1380
|
-
/**
|
|
1381
|
-
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.
|
|
1382
|
-
|
|
1383
|
-
@example
|
|
1384
|
-
```
|
|
1385
|
-
SameLengthPositiveNumericStringGt<'50', '10'>;
|
|
1386
|
-
//=> true
|
|
1387
|
-
|
|
1388
|
-
SameLengthPositiveNumericStringGt<'10', '10'>;
|
|
1389
|
-
//=> false
|
|
1390
|
-
```
|
|
1391
|
-
*/
|
|
1392
|
-
type SameLengthPositiveNumericStringGt<A extends string, B extends string> = A extends `${infer FirstA}${infer RestA}`
|
|
1393
|
-
? B extends `${infer FirstB}${infer RestB}`
|
|
1394
|
-
? FirstA extends FirstB
|
|
1395
|
-
? SameLengthPositiveNumericStringGt<RestA, RestB>
|
|
1396
|
-
: PositiveNumericCharacterGt<FirstA, FirstB>
|
|
1397
|
-
: never
|
|
1398
|
-
: false;
|
|
1399
|
-
|
|
1400
|
-
type NumericString = '0123456789';
|
|
1401
|
-
|
|
1402
|
-
/**
|
|
1403
|
-
Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are both positive numeric strings.
|
|
1404
|
-
|
|
1405
|
-
@example
|
|
1406
|
-
```
|
|
1407
|
-
PositiveNumericStringGt<'500', '1'>;
|
|
1408
|
-
//=> true
|
|
1409
|
-
|
|
1410
|
-
PositiveNumericStringGt<'1', '1'>;
|
|
1411
|
-
//=> false
|
|
1412
|
-
|
|
1413
|
-
PositiveNumericStringGt<'1', '500'>;
|
|
1414
|
-
//=> false
|
|
1415
|
-
```
|
|
1416
|
-
*/
|
|
1417
|
-
type PositiveNumericStringGt<A extends string, B extends string> = A extends B
|
|
1418
|
-
? false
|
|
1419
|
-
: [BuildTuple<StringLength<A>, 0>, BuildTuple<StringLength<B>, 0>] extends infer R extends [readonly unknown[], readonly unknown[]]
|
|
1420
|
-
? R[0] extends [...R[1], ...infer Remain extends readonly unknown[]]
|
|
1421
|
-
? 0 extends Remain['length']
|
|
1422
|
-
? SameLengthPositiveNumericStringGt<A, B>
|
|
1423
|
-
: true
|
|
1424
|
-
: false
|
|
1425
|
-
: never;
|
|
1426
|
-
|
|
1427
|
-
/**
|
|
1428
|
-
Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters.
|
|
1429
|
-
|
|
1430
|
-
@example
|
|
1431
|
-
```
|
|
1432
|
-
PositiveNumericCharacterGt<'5', '1'>;
|
|
1433
|
-
//=> true
|
|
1434
|
-
|
|
1435
|
-
PositiveNumericCharacterGt<'1', '1'>;
|
|
1436
|
-
//=> false
|
|
1437
|
-
```
|
|
1438
|
-
*/
|
|
1439
|
-
type PositiveNumericCharacterGt<A extends string, B extends string> = NumericString extends `${infer HeadA}${A}${infer TailA}`
|
|
1440
|
-
? NumericString extends `${infer HeadB}${B}${infer TailB}`
|
|
1441
|
-
? HeadA extends `${HeadB}${infer _}${infer __}`
|
|
1442
|
-
? true
|
|
1443
|
-
: false
|
|
1444
|
-
: never
|
|
1445
|
-
: never;
|
|
1446
|
-
|
|
1447
|
-
/**
|
|
1448
|
-
Returns the absolute value of a given value.
|
|
1449
|
-
|
|
1450
|
-
@example
|
|
1451
|
-
```
|
|
1452
|
-
NumberAbsolute<-1>;
|
|
1453
|
-
//=> 1
|
|
1454
|
-
|
|
1455
|
-
NumberAbsolute<1>;
|
|
1456
|
-
//=> 1
|
|
1457
|
-
|
|
1458
|
-
NumberAbsolute<NegativeInfinity>
|
|
1459
|
-
//=> PositiveInfinity
|
|
1460
|
-
```
|
|
1461
|
-
*/
|
|
1462
|
-
type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N;
|
|
1463
|
-
|
|
1464
|
-
/**
|
|
1465
|
-
Check whether the given type is a number or a number string.
|
|
1466
|
-
|
|
1467
|
-
Supports floating-point as a string.
|
|
1468
|
-
|
|
1469
|
-
@example
|
|
1470
|
-
```
|
|
1471
|
-
type A = IsNumberLike<'1'>;
|
|
1472
|
-
//=> true
|
|
1473
|
-
|
|
1474
|
-
type B = IsNumberLike<'-1.1'>;
|
|
1475
|
-
//=> true
|
|
1476
|
-
|
|
1477
|
-
type C = IsNumberLike<'5e-20'>;
|
|
1478
|
-
//=> true
|
|
1479
|
-
|
|
1480
|
-
type D = IsNumberLike<1>;
|
|
1481
|
-
//=> true
|
|
1482
|
-
|
|
1483
|
-
type E = IsNumberLike<'a'>;
|
|
1484
|
-
//=> false
|
|
1485
|
-
*/
|
|
1486
|
-
type IsNumberLike<N> =
|
|
1487
|
-
IsAnyOrNever<N> extends true ? N
|
|
1488
|
-
: N extends number | `${number}`
|
|
1489
|
-
? true
|
|
1490
|
-
: false;
|
|
1491
|
-
|
|
1492
|
-
/**
|
|
1493
|
-
Returns the number with reversed sign.
|
|
1494
|
-
|
|
1495
|
-
@example
|
|
1496
|
-
```
|
|
1497
|
-
ReverseSign<-1>;
|
|
1498
|
-
//=> 1
|
|
1499
|
-
|
|
1500
|
-
ReverseSign<1>;
|
|
1501
|
-
//=> -1
|
|
1502
|
-
|
|
1503
|
-
ReverseSign<NegativeInfinity>
|
|
1504
|
-
//=> PositiveInfinity
|
|
1505
|
-
|
|
1506
|
-
ReverseSign<PositiveInfinity>
|
|
1507
|
-
//=> NegativeInfinity
|
|
1508
|
-
```
|
|
1509
|
-
*/
|
|
1510
|
-
type ReverseSign<N extends number> =
|
|
1511
|
-
// Handle edge cases
|
|
1512
|
-
N extends 0 ? 0 : N extends PositiveInfinity ? NegativeInfinity : N extends NegativeInfinity ? PositiveInfinity :
|
|
1513
|
-
// Handle negative numbers
|
|
1514
|
-
`${N}` extends `-${infer P extends number}` ? P
|
|
1515
|
-
// Handle positive numbers
|
|
1516
|
-
: `-${N}` extends `${infer R extends number}` ? R : never;
|
|
1517
|
-
|
|
1518
|
-
/**
|
|
1519
|
-
Returns the difference between two numbers.
|
|
1520
|
-
|
|
1521
|
-
Note:
|
|
1522
|
-
- A or B can only support `-999` ~ `999`.
|
|
1523
|
-
|
|
1524
|
-
@example
|
|
1525
|
-
```
|
|
1526
|
-
import type {Subtract} from 'type-fest';
|
|
1527
|
-
|
|
1528
|
-
Subtract<333, 222>;
|
|
1529
|
-
//=> 111
|
|
1530
|
-
|
|
1531
|
-
Subtract<111, -222>;
|
|
1532
|
-
//=> 333
|
|
1533
|
-
|
|
1534
|
-
Subtract<-111, 222>;
|
|
1535
|
-
//=> -333
|
|
1536
|
-
|
|
1537
|
-
Subtract<18, 96>;
|
|
1538
|
-
//=> -78
|
|
1539
|
-
|
|
1540
|
-
Subtract<PositiveInfinity, 9999>;
|
|
1541
|
-
//=> PositiveInfinity
|
|
1542
|
-
|
|
1543
|
-
Subtract<PositiveInfinity, PositiveInfinity>;
|
|
1544
|
-
//=> number
|
|
1545
|
-
```
|
|
1546
|
-
|
|
1547
|
-
@category Numeric
|
|
1548
|
-
*/
|
|
1549
|
-
// TODO: Support big integer.
|
|
1550
|
-
type Subtract<A extends number, B extends number> =
|
|
1551
|
-
// Handle cases when A or B is the actual "number" type
|
|
1552
|
-
number extends A | B ? number
|
|
1553
|
-
// Handle cases when A and B are both +/- infinity
|
|
1554
|
-
: A extends B & (PositiveInfinity | NegativeInfinity) ? number
|
|
1555
|
-
// Handle cases when A is - infinity or B is + infinity
|
|
1556
|
-
: A extends NegativeInfinity ? NegativeInfinity : B extends PositiveInfinity ? NegativeInfinity
|
|
1557
|
-
// Handle cases when A is + infinity or B is - infinity
|
|
1558
|
-
: A extends PositiveInfinity ? PositiveInfinity : B extends NegativeInfinity ? PositiveInfinity
|
|
1559
|
-
// Handle case when numbers are equal to each other
|
|
1560
|
-
: A extends B ? 0
|
|
1561
|
-
// Handle cases when A or B is 0
|
|
1562
|
-
: A extends 0 ? ReverseSign<B> : B extends 0 ? A
|
|
1563
|
-
// Handle remaining regular cases
|
|
1564
|
-
: SubtractPostChecks<A, B>;
|
|
1565
|
-
|
|
1566
|
-
/**
|
|
1567
|
-
Subtracts two numbers A and B, such that they are not equal and neither of them are 0, +/- infinity or the `number` type
|
|
1568
|
-
*/
|
|
1569
|
-
type SubtractPostChecks<A extends number, B extends number, AreNegative = [IsNegative<A>, IsNegative<B>]> =
|
|
1570
|
-
AreNegative extends [false, false]
|
|
1571
|
-
? SubtractPositives<A, B>
|
|
1572
|
-
: AreNegative extends [true, true]
|
|
1573
|
-
// When both numbers are negative we subtract the absolute values and then reverse the sign
|
|
1574
|
-
? ReverseSign<SubtractPositives<NumberAbsolute<A>, NumberAbsolute<B>>>
|
|
1575
|
-
// When the signs are different we can add the absolute values and then reverse the sign if A < B
|
|
1576
|
-
: [...BuildTuple<NumberAbsolute<A>>, ...BuildTuple<NumberAbsolute<B>>] extends infer R extends unknown[]
|
|
1577
|
-
? LessThan<A, B> extends true ? ReverseSign<R['length']> : R['length']
|
|
1578
|
-
: never;
|
|
1579
|
-
|
|
1580
|
-
/**
|
|
1581
|
-
Subtracts two positive numbers.
|
|
1582
|
-
*/
|
|
1583
|
-
type SubtractPositives<A extends number, B extends number> =
|
|
1584
|
-
LessThan<A, B> extends true
|
|
1585
|
-
// When A < B we can reverse the result of B - A
|
|
1586
|
-
? ReverseSign<SubtractIfAGreaterThanB<B, A>>
|
|
1587
|
-
: SubtractIfAGreaterThanB<A, B>;
|
|
1588
|
-
|
|
1589
|
-
/**
|
|
1590
|
-
Subtracts two positive numbers A and B such that A > B.
|
|
1591
|
-
*/
|
|
1592
|
-
type SubtractIfAGreaterThanB<A extends number, B extends number> =
|
|
1593
|
-
// This is where we always want to end up and do the actual subtraction
|
|
1594
|
-
BuildTuple<A> extends [...BuildTuple<B>, ...infer R]
|
|
1595
|
-
? R['length']
|
|
1596
|
-
: never;
|
|
1597
|
-
|
|
1598
|
-
/**
|
|
1599
|
-
Paths options.
|
|
1600
|
-
|
|
1601
|
-
@see {@link Paths}
|
|
1602
|
-
*/
|
|
1603
|
-
type PathsOptions = {
|
|
1604
|
-
/**
|
|
1605
|
-
The maximum depth to recurse when searching for paths. Range: 0 ~ 10.
|
|
1606
|
-
|
|
1607
|
-
@default 5
|
|
1608
|
-
*/
|
|
1609
|
-
maxRecursionDepth?: number;
|
|
1610
|
-
|
|
1611
|
-
/**
|
|
1612
|
-
Use bracket notation for array indices and numeric object keys.
|
|
1613
|
-
|
|
1614
|
-
@default false
|
|
1615
|
-
|
|
1616
|
-
@example
|
|
1617
|
-
```
|
|
1618
|
-
type ArrayExample = {
|
|
1619
|
-
array: ['foo'];
|
|
1620
|
-
};
|
|
1621
|
-
|
|
1622
|
-
type A = Paths<ArrayExample, {bracketNotation: false}>;
|
|
1623
|
-
//=> 'array' | 'array.0'
|
|
1624
|
-
|
|
1625
|
-
type B = Paths<ArrayExample, {bracketNotation: true}>;
|
|
1626
|
-
//=> 'array' | 'array[0]'
|
|
1627
|
-
```
|
|
1628
|
-
|
|
1629
|
-
@example
|
|
1630
|
-
```
|
|
1631
|
-
type NumberKeyExample = {
|
|
1632
|
-
1: ['foo'];
|
|
1633
|
-
};
|
|
1634
|
-
|
|
1635
|
-
type A = Paths<NumberKeyExample, {bracketNotation: false}>;
|
|
1636
|
-
//=> 1 | '1' | '1.0'
|
|
1637
|
-
|
|
1638
|
-
type B = Paths<NumberKeyExample, {bracketNotation: true}>;
|
|
1639
|
-
//=> '[1]' | '[1][0]'
|
|
1640
|
-
```
|
|
1641
|
-
*/
|
|
1642
|
-
bracketNotation?: boolean;
|
|
1643
|
-
|
|
1644
|
-
/**
|
|
1645
|
-
Only include leaf paths in the output.
|
|
1646
|
-
|
|
1647
|
-
@default false
|
|
1648
|
-
|
|
1649
|
-
@example
|
|
1650
|
-
```
|
|
1651
|
-
type Post = {
|
|
1652
|
-
id: number;
|
|
1653
|
-
author: {
|
|
1654
|
-
id: number;
|
|
1655
|
-
name: {
|
|
1656
|
-
first: string;
|
|
1657
|
-
last: string;
|
|
1658
|
-
};
|
|
1659
|
-
};
|
|
1660
|
-
};
|
|
1661
|
-
|
|
1662
|
-
type AllPaths = Paths<Post, {leavesOnly: false}>;
|
|
1663
|
-
//=> 'id' | 'author' | 'author.id' | 'author.name' | 'author.name.first' | 'author.name.last'
|
|
1664
|
-
|
|
1665
|
-
type LeafPaths = Paths<Post, {leavesOnly: true}>;
|
|
1666
|
-
//=> 'id' | 'author.id' | 'author.name.first' | 'author.name.last'
|
|
1667
|
-
```
|
|
1668
|
-
|
|
1669
|
-
@example
|
|
1670
|
-
```
|
|
1671
|
-
type ArrayExample = {
|
|
1672
|
-
array: Array<{foo: string}>;
|
|
1673
|
-
tuple: [string, {bar: string}];
|
|
1674
|
-
};
|
|
1675
|
-
|
|
1676
|
-
type AllPaths = Paths<ArrayExample, {leavesOnly: false}>;
|
|
1677
|
-
//=> 'array' | `array.${number}` | `array.${number}.foo` | 'tuple' | 'tuple.0' | 'tuple.1' | 'tuple.1.bar'
|
|
1678
|
-
|
|
1679
|
-
type LeafPaths = Paths<ArrayExample, {leavesOnly: true}>;
|
|
1680
|
-
//=> `array.${number}.foo` | 'tuple.0' | 'tuple.1.bar'
|
|
1681
|
-
```
|
|
1682
|
-
*/
|
|
1683
|
-
leavesOnly?: boolean;
|
|
1684
|
-
|
|
1685
|
-
/**
|
|
1686
|
-
Only include paths at the specified depth. By default all paths up to {@link PathsOptions.maxRecursionDepth | `maxRecursionDepth`} are included.
|
|
1687
|
-
|
|
1688
|
-
Note: Depth starts at `0` for root properties.
|
|
1689
|
-
|
|
1690
|
-
@default number
|
|
1691
|
-
|
|
1692
|
-
@example
|
|
1693
|
-
```
|
|
1694
|
-
type Post = {
|
|
1695
|
-
id: number;
|
|
1696
|
-
author: {
|
|
1697
|
-
id: number;
|
|
1698
|
-
name: {
|
|
1699
|
-
first: string;
|
|
1700
|
-
last: string;
|
|
1701
|
-
};
|
|
1702
|
-
};
|
|
1703
|
-
};
|
|
1704
|
-
|
|
1705
|
-
type DepthZero = Paths<Post, {depth: 0}>;
|
|
1706
|
-
//=> 'id' | 'author'
|
|
1707
|
-
|
|
1708
|
-
type DepthOne = Paths<Post, {depth: 1}>;
|
|
1709
|
-
//=> 'author.id' | 'author.name'
|
|
1710
|
-
|
|
1711
|
-
type DepthTwo = Paths<Post, {depth: 2}>;
|
|
1712
|
-
//=> 'author.name.first' | 'author.name.last'
|
|
1713
|
-
|
|
1714
|
-
type LeavesAtDepthOne = Paths<Post, {leavesOnly: true; depth: 1}>;
|
|
1715
|
-
//=> 'author.id'
|
|
1716
|
-
```
|
|
1717
|
-
*/
|
|
1718
|
-
depth?: number;
|
|
1719
|
-
};
|
|
1720
|
-
|
|
1721
|
-
type DefaultPathsOptions = {
|
|
1722
|
-
maxRecursionDepth: 5;
|
|
1723
|
-
bracketNotation: false;
|
|
1724
|
-
leavesOnly: false;
|
|
1725
|
-
depth: number;
|
|
1726
|
-
};
|
|
1727
|
-
|
|
1728
|
-
/**
|
|
1729
|
-
Generate a union of all possible paths to properties in the given object.
|
|
1730
|
-
|
|
1731
|
-
It also works with arrays.
|
|
1732
|
-
|
|
1733
|
-
Use-case: You want a type-safe way to access deeply nested properties in an object.
|
|
1734
|
-
|
|
1735
|
-
@example
|
|
1736
|
-
```
|
|
1737
|
-
import type {Paths} from 'type-fest';
|
|
1738
|
-
|
|
1739
|
-
type Project = {
|
|
1740
|
-
filename: string;
|
|
1741
|
-
listA: string[];
|
|
1742
|
-
listB: [{filename: string}];
|
|
1743
|
-
folder: {
|
|
1744
|
-
subfolder: {
|
|
1745
|
-
filename: string;
|
|
1746
|
-
};
|
|
1747
|
-
};
|
|
1748
|
-
};
|
|
1749
|
-
|
|
1750
|
-
type ProjectPaths = Paths<Project>;
|
|
1751
|
-
//=> 'filename' | 'listA' | 'listB' | 'folder' | `listA.${number}` | 'listB.0' | 'listB.0.filename' | 'folder.subfolder' | 'folder.subfolder.filename'
|
|
1752
|
-
|
|
1753
|
-
declare function open<Path extends ProjectPaths>(path: Path): void;
|
|
1754
|
-
|
|
1755
|
-
open('filename'); // Pass
|
|
1756
|
-
open('folder.subfolder'); // Pass
|
|
1757
|
-
open('folder.subfolder.filename'); // Pass
|
|
1758
|
-
open('foo'); // TypeError
|
|
1759
|
-
|
|
1760
|
-
// Also works with arrays
|
|
1761
|
-
open('listA.1'); // Pass
|
|
1762
|
-
open('listB.0'); // Pass
|
|
1763
|
-
open('listB.1'); // TypeError. Because listB only has one element.
|
|
1764
|
-
```
|
|
1765
|
-
|
|
1766
|
-
@category Object
|
|
1767
|
-
@category Array
|
|
1768
|
-
*/
|
|
1769
|
-
type Paths<T, Options extends PathsOptions = {}> = _Paths<T, ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, Options>>;
|
|
1770
|
-
|
|
1771
|
-
type _Paths<T, Options extends Required<PathsOptions>> =
|
|
1772
|
-
T extends NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
|
|
1773
|
-
? never
|
|
1774
|
-
: IsAny<T> extends true
|
|
1775
|
-
? never
|
|
1776
|
-
: T extends UnknownArray
|
|
1777
|
-
? number extends T['length']
|
|
1778
|
-
// We need to handle the fixed and non-fixed index part of the array separately.
|
|
1779
|
-
? InternalPaths<StaticPartOfArray<T>, Options> | InternalPaths<Array<VariablePartOfArray<T>[number]>, Options>
|
|
1780
|
-
: InternalPaths<T, Options>
|
|
1781
|
-
: T extends object
|
|
1782
|
-
? InternalPaths<T, Options>
|
|
1783
|
-
: never;
|
|
1784
|
-
|
|
1785
|
-
type InternalPaths<T, Options extends Required<PathsOptions>> =
|
|
1786
|
-
Options['maxRecursionDepth'] extends infer MaxDepth extends number
|
|
1787
|
-
? Required<T> extends infer T
|
|
1788
|
-
? T extends readonly []
|
|
1789
|
-
? never
|
|
1790
|
-
: IsNever<keyof T> extends true // Check for empty object
|
|
1791
|
-
? never
|
|
1792
|
-
: {
|
|
1793
|
-
[Key in keyof T]:
|
|
1794
|
-
Key extends string | number // Limit `Key` to string or number.
|
|
1795
|
-
? (
|
|
1796
|
-
Options['bracketNotation'] extends true
|
|
1797
|
-
? IsNumberLike<Key> extends true
|
|
1798
|
-
? `[${Key}]`
|
|
1799
|
-
: (Key | ToString<Key>)
|
|
1800
|
-
: Options['bracketNotation'] extends false
|
|
1801
|
-
// If `Key` is a number, return `Key | `${Key}``, because both `array[0]` and `array['0']` work.
|
|
1802
|
-
? (Key | ToString<Key>)
|
|
1803
|
-
: never
|
|
1804
|
-
) extends infer TranformedKey extends string | number ?
|
|
1805
|
-
// 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
|
|
1806
|
-
// 2. If style is 'a.0.b', transform 'Key' to `${Key}` | Key
|
|
1807
|
-
| ((Options['leavesOnly'] extends true
|
|
1808
|
-
? MaxDepth extends 0
|
|
1809
|
-
? TranformedKey
|
|
1810
|
-
: T[Key] extends infer Value
|
|
1811
|
-
? (Value extends readonly [] | NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
|
|
1812
|
-
? TranformedKey
|
|
1813
|
-
: IsNever<keyof Value> extends true // Check for empty object
|
|
1814
|
-
? TranformedKey
|
|
1815
|
-
: never)
|
|
1816
|
-
: never
|
|
1817
|
-
: TranformedKey
|
|
1818
|
-
) extends infer _TransformedKey
|
|
1819
|
-
// If `depth` is provided, the condition becomes truthy only when it reaches `0`.
|
|
1820
|
-
// Otherwise, since `depth` defaults to `number`, the condition is always truthy, returning paths at all depths.
|
|
1821
|
-
? 0 extends Options['depth']
|
|
1822
|
-
? _TransformedKey
|
|
1823
|
-
: never
|
|
1824
|
-
: never)
|
|
1825
|
-
| (
|
|
1826
|
-
// Recursively generate paths for the current key
|
|
1827
|
-
GreaterThan<MaxDepth, 0> extends true // Limit the depth to prevent infinite recursion
|
|
1828
|
-
? _Paths<T[Key],
|
|
1829
|
-
{
|
|
1830
|
-
bracketNotation: Options['bracketNotation'];
|
|
1831
|
-
maxRecursionDepth: Subtract<MaxDepth, 1>;
|
|
1832
|
-
leavesOnly: Options['leavesOnly'];
|
|
1833
|
-
depth: Subtract<Options['depth'], 1>;
|
|
1834
|
-
}> extends infer SubPath
|
|
1835
|
-
? SubPath extends string | number
|
|
1836
|
-
? (
|
|
1837
|
-
Options['bracketNotation'] extends true
|
|
1838
|
-
? SubPath extends `[${any}]` | `[${any}]${string}`
|
|
1839
|
-
? `${TranformedKey}${SubPath}` // If next node is number key like `[3]`, no need to add `.` before it.
|
|
1840
|
-
: `${TranformedKey}.${SubPath}`
|
|
1841
|
-
: never
|
|
1842
|
-
) | (
|
|
1843
|
-
Options['bracketNotation'] extends false
|
|
1844
|
-
? `${TranformedKey}.${SubPath}`
|
|
1845
|
-
: never
|
|
1846
|
-
)
|
|
1847
|
-
: never
|
|
1848
|
-
: never
|
|
1849
|
-
: never
|
|
1850
|
-
)
|
|
1851
|
-
: never
|
|
1852
|
-
: never
|
|
1853
|
-
}[keyof T & (T extends UnknownArray ? number : unknown)]
|
|
1854
|
-
: never
|
|
1855
|
-
: never;
|
|
1856
|
-
|
|
1857
|
-
/**
|
|
1858
|
-
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
|
|
1859
|
-
|
|
1860
|
-
Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
|
|
1861
|
-
|
|
1862
|
-
This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
|
|
1863
|
-
|
|
1864
|
-
@example
|
|
1865
|
-
```
|
|
1866
|
-
import type {LiteralUnion} from 'type-fest';
|
|
1867
|
-
|
|
1868
|
-
// Before
|
|
1869
|
-
|
|
1870
|
-
type Pet = 'dog' | 'cat' | string;
|
|
1871
|
-
|
|
1872
|
-
const pet: Pet = '';
|
|
1873
|
-
// Start typing in your TypeScript-enabled IDE.
|
|
1874
|
-
// You **will not** get auto-completion for `dog` and `cat` literals.
|
|
1875
|
-
|
|
1876
|
-
// After
|
|
1877
|
-
|
|
1878
|
-
type Pet2 = LiteralUnion<'dog' | 'cat', string>;
|
|
1879
|
-
|
|
1880
|
-
const pet: Pet2 = '';
|
|
1881
|
-
// You **will** get auto-completion for `dog` and `cat` literals.
|
|
1882
|
-
```
|
|
1883
|
-
|
|
1884
|
-
@category Type
|
|
1885
|
-
*/
|
|
1886
|
-
type LiteralUnion<
|
|
1887
|
-
LiteralType,
|
|
1888
|
-
BaseType extends Primitive,
|
|
1889
|
-
> = LiteralType | (BaseType & Record<never, never>);
|
|
1890
|
-
|
|
1891
|
-
declare namespace PackageJson$1 {
|
|
1892
|
-
/**
|
|
1893
|
-
A person who has been involved in creating or maintaining the package.
|
|
1894
|
-
*/
|
|
1895
|
-
export type Person =
|
|
1896
|
-
| string
|
|
1897
|
-
| {
|
|
1898
|
-
name: string;
|
|
1899
|
-
url?: string;
|
|
1900
|
-
email?: string;
|
|
1901
|
-
};
|
|
1902
|
-
|
|
1903
|
-
export type BugsLocation =
|
|
1904
|
-
| string
|
|
1905
|
-
| {
|
|
1906
|
-
/**
|
|
1907
|
-
The URL to the package's issue tracker.
|
|
1908
|
-
*/
|
|
1909
|
-
url?: string;
|
|
1910
|
-
|
|
1911
|
-
/**
|
|
1912
|
-
The email address to which issues should be reported.
|
|
1913
|
-
*/
|
|
1914
|
-
email?: string;
|
|
1915
|
-
};
|
|
1916
|
-
|
|
1917
|
-
export type DirectoryLocations = {
|
|
1918
|
-
[directoryType: string]: JsonValue | undefined;
|
|
1919
|
-
|
|
1920
|
-
/**
|
|
1921
|
-
Location for executable scripts. Sugar to generate entries in the `bin` property by walking the folder.
|
|
1922
|
-
*/
|
|
1923
|
-
bin?: string;
|
|
1924
|
-
|
|
1925
|
-
/**
|
|
1926
|
-
Location for Markdown files.
|
|
1927
|
-
*/
|
|
1928
|
-
doc?: string;
|
|
1929
|
-
|
|
1930
|
-
/**
|
|
1931
|
-
Location for example scripts.
|
|
1932
|
-
*/
|
|
1933
|
-
example?: string;
|
|
1934
|
-
|
|
1935
|
-
/**
|
|
1936
|
-
Location for the bulk of the library.
|
|
1937
|
-
*/
|
|
1938
|
-
lib?: string;
|
|
1939
|
-
|
|
1940
|
-
/**
|
|
1941
|
-
Location for man pages. Sugar to generate a `man` array by walking the folder.
|
|
1942
|
-
*/
|
|
1943
|
-
man?: string;
|
|
1944
|
-
|
|
1945
|
-
/**
|
|
1946
|
-
Location for test files.
|
|
1947
|
-
*/
|
|
1948
|
-
test?: string;
|
|
1949
|
-
};
|
|
1950
|
-
|
|
1951
|
-
export type Scripts = {
|
|
1952
|
-
/**
|
|
1953
|
-
Run **before** the package is published (Also run on local `npm install` without any arguments).
|
|
1954
|
-
*/
|
|
1955
|
-
prepublish?: string;
|
|
1956
|
-
|
|
1957
|
-
/**
|
|
1958
|
-
Run both **before** the package is packed and published, and on local `npm install` without any arguments. This is run **after** `prepublish`, but **before** `prepublishOnly`.
|
|
1959
|
-
*/
|
|
1960
|
-
prepare?: string;
|
|
1961
|
-
|
|
1962
|
-
/**
|
|
1963
|
-
Run **before** the package is prepared and packed, **only** on `npm publish`.
|
|
1964
|
-
*/
|
|
1965
|
-
prepublishOnly?: string;
|
|
1966
|
-
|
|
1967
|
-
/**
|
|
1968
|
-
Run **before** a tarball is packed (on `npm pack`, `npm publish`, and when installing git dependencies).
|
|
1969
|
-
*/
|
|
1970
|
-
prepack?: string;
|
|
1971
|
-
|
|
1972
|
-
/**
|
|
1973
|
-
Run **after** the tarball has been generated and moved to its final destination.
|
|
1974
|
-
*/
|
|
1975
|
-
postpack?: string;
|
|
1976
|
-
|
|
1977
|
-
/**
|
|
1978
|
-
Run **after** the package is published.
|
|
1979
|
-
*/
|
|
1980
|
-
publish?: string;
|
|
1981
|
-
|
|
1982
|
-
/**
|
|
1983
|
-
Run **after** the package is published.
|
|
1984
|
-
*/
|
|
1985
|
-
postpublish?: string;
|
|
1986
|
-
|
|
1987
|
-
/**
|
|
1988
|
-
Run **before** the package is installed.
|
|
1989
|
-
*/
|
|
1990
|
-
preinstall?: string;
|
|
1991
|
-
|
|
1992
|
-
/**
|
|
1993
|
-
Run **after** the package is installed.
|
|
1994
|
-
*/
|
|
1995
|
-
install?: string;
|
|
1996
|
-
|
|
1997
|
-
/**
|
|
1998
|
-
Run **after** the package is installed and after `install`.
|
|
1999
|
-
*/
|
|
2000
|
-
postinstall?: string;
|
|
2001
|
-
|
|
2002
|
-
/**
|
|
2003
|
-
Run **before** the package is uninstalled and before `uninstall`.
|
|
2004
|
-
*/
|
|
2005
|
-
preuninstall?: string;
|
|
2006
|
-
|
|
2007
|
-
/**
|
|
2008
|
-
Run **before** the package is uninstalled.
|
|
2009
|
-
*/
|
|
2010
|
-
uninstall?: string;
|
|
2011
|
-
|
|
2012
|
-
/**
|
|
2013
|
-
Run **after** the package is uninstalled.
|
|
2014
|
-
*/
|
|
2015
|
-
postuninstall?: string;
|
|
2016
|
-
|
|
2017
|
-
/**
|
|
2018
|
-
Run **before** bump the package version and before `version`.
|
|
2019
|
-
*/
|
|
2020
|
-
preversion?: string;
|
|
2021
|
-
|
|
2022
|
-
/**
|
|
2023
|
-
Run **before** bump the package version.
|
|
2024
|
-
*/
|
|
2025
|
-
version?: string;
|
|
2026
|
-
|
|
2027
|
-
/**
|
|
2028
|
-
Run **after** bump the package version.
|
|
2029
|
-
*/
|
|
2030
|
-
postversion?: string;
|
|
2031
|
-
|
|
2032
|
-
/**
|
|
2033
|
-
Run with the `npm test` command, before `test`.
|
|
2034
|
-
*/
|
|
2035
|
-
pretest?: string;
|
|
2036
|
-
|
|
2037
|
-
/**
|
|
2038
|
-
Run with the `npm test` command.
|
|
2039
|
-
*/
|
|
2040
|
-
test?: string;
|
|
2041
|
-
|
|
2042
|
-
/**
|
|
2043
|
-
Run with the `npm test` command, after `test`.
|
|
2044
|
-
*/
|
|
2045
|
-
posttest?: string;
|
|
2046
|
-
|
|
2047
|
-
/**
|
|
2048
|
-
Run with the `npm stop` command, before `stop`.
|
|
2049
|
-
*/
|
|
2050
|
-
prestop?: string;
|
|
2051
|
-
|
|
2052
|
-
/**
|
|
2053
|
-
Run with the `npm stop` command.
|
|
2054
|
-
*/
|
|
2055
|
-
stop?: string;
|
|
2056
|
-
|
|
2057
|
-
/**
|
|
2058
|
-
Run with the `npm stop` command, after `stop`.
|
|
2059
|
-
*/
|
|
2060
|
-
poststop?: string;
|
|
2061
|
-
|
|
2062
|
-
/**
|
|
2063
|
-
Run with the `npm start` command, before `start`.
|
|
2064
|
-
*/
|
|
2065
|
-
prestart?: string;
|
|
2066
|
-
|
|
2067
|
-
/**
|
|
2068
|
-
Run with the `npm start` command.
|
|
2069
|
-
*/
|
|
2070
|
-
start?: string;
|
|
2071
|
-
|
|
2072
|
-
/**
|
|
2073
|
-
Run with the `npm start` command, after `start`.
|
|
2074
|
-
*/
|
|
2075
|
-
poststart?: string;
|
|
2076
|
-
|
|
2077
|
-
/**
|
|
2078
|
-
Run with the `npm restart` command, before `restart`. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided.
|
|
2079
|
-
*/
|
|
2080
|
-
prerestart?: string;
|
|
2081
|
-
|
|
2082
|
-
/**
|
|
2083
|
-
Run with the `npm restart` command. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided.
|
|
2084
|
-
*/
|
|
2085
|
-
restart?: string;
|
|
2086
|
-
|
|
2087
|
-
/**
|
|
2088
|
-
Run with the `npm restart` command, after `restart`. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided.
|
|
2089
|
-
*/
|
|
2090
|
-
postrestart?: string;
|
|
2091
|
-
} & Partial<Record<string, string>>;
|
|
2092
|
-
|
|
2093
|
-
/**
|
|
2094
|
-
Dependencies of the package. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or Git URL.
|
|
2095
|
-
*/
|
|
2096
|
-
export type Dependency = Partial<Record<string, string>>;
|
|
2097
|
-
|
|
2098
|
-
/**
|
|
2099
|
-
A mapping of conditions and the paths to which they resolve.
|
|
2100
|
-
*/
|
|
2101
|
-
type ExportConditions = {
|
|
2102
|
-
[condition: string]: Exports;
|
|
2103
|
-
};
|
|
2104
|
-
|
|
2105
|
-
/**
|
|
2106
|
-
Entry points of a module, optionally with conditions and subpath exports.
|
|
2107
|
-
*/
|
|
2108
|
-
export type Exports =
|
|
2109
|
-
| null
|
|
2110
|
-
| string
|
|
2111
|
-
| Array<string | ExportConditions>
|
|
2112
|
-
| ExportConditions;
|
|
2113
|
-
|
|
2114
|
-
/**
|
|
2115
|
-
Import map entries of a module, optionally with conditions and subpath imports.
|
|
2116
|
-
*/
|
|
2117
|
-
export type Imports = {
|
|
2118
|
-
[key: `#${string}`]: Exports;
|
|
2119
|
-
};
|
|
2120
|
-
|
|
2121
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
2122
|
-
export interface NonStandardEntryPoints {
|
|
2123
|
-
/**
|
|
2124
|
-
An ECMAScript module ID that is the primary entry point to the program.
|
|
2125
|
-
*/
|
|
2126
|
-
module?: string;
|
|
2127
|
-
|
|
2128
|
-
/**
|
|
2129
|
-
A module ID with untranspiled code that is the primary entry point to the program.
|
|
2130
|
-
*/
|
|
2131
|
-
esnext?:
|
|
2132
|
-
| string
|
|
2133
|
-
| {
|
|
2134
|
-
[moduleName: string]: string | undefined;
|
|
2135
|
-
main?: string;
|
|
2136
|
-
browser?: string;
|
|
2137
|
-
};
|
|
2138
|
-
|
|
2139
|
-
/**
|
|
2140
|
-
A hint to JavaScript bundlers or component tools when packaging modules for client side use.
|
|
2141
|
-
*/
|
|
2142
|
-
browser?:
|
|
2143
|
-
| string
|
|
2144
|
-
| Partial<Record<string, string | false>>;
|
|
2145
|
-
|
|
2146
|
-
/**
|
|
2147
|
-
Denote which files in your project are "pure" and therefore safe for Webpack to prune if unused.
|
|
2148
|
-
|
|
2149
|
-
[Read more.](https://webpack.js.org/guides/tree-shaking/)
|
|
2150
|
-
*/
|
|
2151
|
-
sideEffects?: boolean | string[];
|
|
2152
|
-
}
|
|
2153
|
-
|
|
2154
|
-
export type TypeScriptConfiguration = {
|
|
2155
|
-
/**
|
|
2156
|
-
Location of the bundled TypeScript declaration file.
|
|
2157
|
-
*/
|
|
2158
|
-
types?: string;
|
|
2159
|
-
|
|
2160
|
-
/**
|
|
2161
|
-
Version selection map of TypeScript.
|
|
2162
|
-
*/
|
|
2163
|
-
typesVersions?: Partial<Record<string, Partial<Record<string, string[]>>>>;
|
|
2164
|
-
|
|
2165
|
-
/**
|
|
2166
|
-
Location of the bundled TypeScript declaration file. Alias of `types`.
|
|
2167
|
-
*/
|
|
2168
|
-
typings?: string;
|
|
2169
|
-
};
|
|
2170
|
-
|
|
2171
|
-
/**
|
|
2172
|
-
An alternative configuration for workspaces.
|
|
2173
|
-
*/
|
|
2174
|
-
export type WorkspaceConfig = {
|
|
2175
|
-
/**
|
|
2176
|
-
An array of workspace pattern strings which contain the workspace packages.
|
|
2177
|
-
*/
|
|
2178
|
-
packages?: WorkspacePattern[];
|
|
2179
|
-
|
|
2180
|
-
/**
|
|
2181
|
-
Designed to solve the problem of packages which break when their `node_modules` are moved to the root workspace directory - a process known as hoisting. For these packages, both within your workspace, and also some that have been installed via `node_modules`, it is important to have a mechanism for preventing the default Yarn workspace behavior. By adding workspace pattern strings here, Yarn will resume non-workspace behavior for any package which matches the defined patterns.
|
|
2182
|
-
|
|
2183
|
-
[Supported](https://classic.yarnpkg.com/blog/2018/02/15/nohoist/) by Yarn.
|
|
2184
|
-
[Not supported](https://github.com/npm/rfcs/issues/287) by npm.
|
|
2185
|
-
*/
|
|
2186
|
-
nohoist?: WorkspacePattern[];
|
|
2187
|
-
};
|
|
2188
|
-
|
|
2189
|
-
/**
|
|
2190
|
-
A workspace pattern points to a directory or group of directories which contain packages that should be included in the workspace installation process.
|
|
2191
|
-
|
|
2192
|
-
The patterns are handled with [minimatch](https://github.com/isaacs/minimatch).
|
|
2193
|
-
|
|
2194
|
-
@example
|
|
2195
|
-
`docs` → Include the docs directory and install its dependencies.
|
|
2196
|
-
`packages/*` → Include all nested directories within the packages directory, like `packages/cli` and `packages/core`.
|
|
2197
|
-
*/
|
|
2198
|
-
type WorkspacePattern = string;
|
|
2199
|
-
|
|
2200
|
-
export type YarnConfiguration = {
|
|
2201
|
-
/**
|
|
2202
|
-
If your package only allows one version of a given dependency, and you’d like to enforce the same behavior as `yarn install --flat` on the command-line, set this to `true`.
|
|
2203
|
-
|
|
2204
|
-
Note that if your `package.json` contains `"flat": true` and other packages depend on yours (e.g. you are building a library rather than an app), those other packages will also need `"flat": true` in their `package.json` or be installed with `yarn install --flat` on the command-line.
|
|
2205
|
-
*/
|
|
2206
|
-
flat?: boolean;
|
|
2207
|
-
|
|
2208
|
-
/**
|
|
2209
|
-
Selective version resolutions. Allows the definition of custom package versions inside dependencies without manual edits in the `yarn.lock` file.
|
|
2210
|
-
*/
|
|
2211
|
-
resolutions?: Dependency;
|
|
2212
|
-
};
|
|
2213
|
-
|
|
2214
|
-
export type JSPMConfiguration = {
|
|
2215
|
-
/**
|
|
2216
|
-
JSPM configuration.
|
|
2217
|
-
*/
|
|
2218
|
-
jspm?: PackageJson$1;
|
|
2219
|
-
};
|
|
2220
|
-
|
|
2221
|
-
/**
|
|
2222
|
-
Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Containing standard npm properties.
|
|
2223
|
-
*/
|
|
2224
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
2225
|
-
export interface PackageJsonStandard {
|
|
2226
|
-
/**
|
|
2227
|
-
The name of the package.
|
|
2228
|
-
*/
|
|
2229
|
-
name?: string;
|
|
2230
|
-
|
|
2231
|
-
/**
|
|
2232
|
-
Package version, parseable by [`node-semver`](https://github.com/npm/node-semver).
|
|
2233
|
-
*/
|
|
2234
|
-
version?: string;
|
|
2235
|
-
|
|
2236
|
-
/**
|
|
2237
|
-
Package description, listed in `npm search`.
|
|
2238
|
-
*/
|
|
2239
|
-
description?: string;
|
|
2240
|
-
|
|
2241
|
-
/**
|
|
2242
|
-
Keywords associated with package, listed in `npm search`.
|
|
2243
|
-
*/
|
|
2244
|
-
keywords?: string[];
|
|
2245
|
-
|
|
2246
|
-
/**
|
|
2247
|
-
The URL to the package's homepage.
|
|
2248
|
-
*/
|
|
2249
|
-
homepage?: LiteralUnion<'.', string>;
|
|
2250
|
-
|
|
2251
|
-
/**
|
|
2252
|
-
The URL to the package's issue tracker and/or the email address to which issues should be reported.
|
|
2253
|
-
*/
|
|
2254
|
-
bugs?: BugsLocation;
|
|
2255
|
-
|
|
2256
|
-
/**
|
|
2257
|
-
The license for the package.
|
|
2258
|
-
*/
|
|
2259
|
-
license?: string;
|
|
2260
|
-
|
|
2261
|
-
/**
|
|
2262
|
-
The licenses for the package.
|
|
2263
|
-
*/
|
|
2264
|
-
licenses?: Array<{
|
|
2265
|
-
type?: string;
|
|
2266
|
-
url?: string;
|
|
2267
|
-
}>;
|
|
2268
|
-
|
|
2269
|
-
author?: Person;
|
|
2270
|
-
|
|
2271
|
-
/**
|
|
2272
|
-
A list of people who contributed to the package.
|
|
2273
|
-
*/
|
|
2274
|
-
contributors?: Person[];
|
|
2275
|
-
|
|
2276
|
-
/**
|
|
2277
|
-
A list of people who maintain the package.
|
|
2278
|
-
*/
|
|
2279
|
-
maintainers?: Person[];
|
|
2280
|
-
|
|
2281
|
-
/**
|
|
2282
|
-
The files included in the package.
|
|
2283
|
-
*/
|
|
2284
|
-
files?: string[];
|
|
2285
|
-
|
|
2286
|
-
/**
|
|
2287
|
-
Resolution algorithm for importing ".js" files from the package's scope.
|
|
2288
|
-
|
|
2289
|
-
[Read more.](https://nodejs.org/api/esm.html#esm_package_json_type_field)
|
|
2290
|
-
*/
|
|
2291
|
-
type?: 'module' | 'commonjs';
|
|
2292
|
-
|
|
2293
|
-
/**
|
|
2294
|
-
The module ID that is the primary entry point to the program.
|
|
2295
|
-
*/
|
|
2296
|
-
main?: string;
|
|
2297
|
-
|
|
2298
|
-
/**
|
|
2299
|
-
Subpath exports to define entry points of the package.
|
|
2300
|
-
|
|
2301
|
-
[Read more.](https://nodejs.org/api/packages.html#subpath-exports)
|
|
2302
|
-
*/
|
|
2303
|
-
exports?: Exports;
|
|
2304
|
-
|
|
2305
|
-
/**
|
|
2306
|
-
Subpath imports to define internal package import maps that only apply to import specifiers from within the package itself.
|
|
2307
|
-
|
|
2308
|
-
[Read more.](https://nodejs.org/api/packages.html#subpath-imports)
|
|
2309
|
-
*/
|
|
2310
|
-
imports?: Imports;
|
|
2311
|
-
|
|
2312
|
-
/**
|
|
2313
|
-
The executable files that should be installed into the `PATH`.
|
|
2314
|
-
*/
|
|
2315
|
-
bin?:
|
|
2316
|
-
| string
|
|
2317
|
-
| Partial<Record<string, string>>;
|
|
2318
|
-
|
|
2319
|
-
/**
|
|
2320
|
-
Filenames to put in place for the `man` program to find.
|
|
2321
|
-
*/
|
|
2322
|
-
man?: string | string[];
|
|
2323
|
-
|
|
2324
|
-
/**
|
|
2325
|
-
Indicates the structure of the package.
|
|
2326
|
-
*/
|
|
2327
|
-
directories?: DirectoryLocations;
|
|
2328
|
-
|
|
2329
|
-
/**
|
|
2330
|
-
Location for the code repository.
|
|
2331
|
-
*/
|
|
2332
|
-
repository?:
|
|
2333
|
-
| string
|
|
2334
|
-
| {
|
|
2335
|
-
type: string;
|
|
2336
|
-
url: string;
|
|
2337
|
-
|
|
2338
|
-
/**
|
|
2339
|
-
Relative path to package.json if it is placed in non-root directory (for example if it is part of a monorepo).
|
|
2340
|
-
|
|
2341
|
-
[Read more.](https://github.com/npm/rfcs/blob/latest/implemented/0010-monorepo-subdirectory-declaration.md)
|
|
2342
|
-
*/
|
|
2343
|
-
directory?: string;
|
|
2344
|
-
};
|
|
2345
|
-
|
|
2346
|
-
/**
|
|
2347
|
-
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.
|
|
2348
|
-
*/
|
|
2349
|
-
scripts?: Scripts;
|
|
2350
|
-
|
|
2351
|
-
/**
|
|
2352
|
-
Is used to set configuration parameters used in package scripts that persist across upgrades.
|
|
2353
|
-
*/
|
|
2354
|
-
config?: JsonObject;
|
|
2355
|
-
|
|
2356
|
-
/**
|
|
2357
|
-
The dependencies of the package.
|
|
2358
|
-
*/
|
|
2359
|
-
dependencies?: Dependency;
|
|
2360
|
-
|
|
2361
|
-
/**
|
|
2362
|
-
Additional tooling dependencies that are not required for the package to work. Usually test, build, or documentation tooling.
|
|
2363
|
-
*/
|
|
2364
|
-
devDependencies?: Dependency;
|
|
2365
|
-
|
|
2366
|
-
/**
|
|
2367
|
-
Dependencies that are skipped if they fail to install.
|
|
2368
|
-
*/
|
|
2369
|
-
optionalDependencies?: Dependency;
|
|
2370
|
-
|
|
2371
|
-
/**
|
|
2372
|
-
Dependencies that will usually be required by the package user directly or via another dependency.
|
|
2373
|
-
*/
|
|
2374
|
-
peerDependencies?: Dependency;
|
|
2375
|
-
|
|
2376
|
-
/**
|
|
2377
|
-
Indicate peer dependencies that are optional.
|
|
2378
|
-
*/
|
|
2379
|
-
peerDependenciesMeta?: Partial<Record<string, {optional: true}>>;
|
|
2380
|
-
|
|
2381
|
-
/**
|
|
2382
|
-
Package names that are bundled when the package is published.
|
|
2383
|
-
*/
|
|
2384
|
-
bundledDependencies?: string[];
|
|
2385
|
-
|
|
2386
|
-
/**
|
|
2387
|
-
Alias of `bundledDependencies`.
|
|
2388
|
-
*/
|
|
2389
|
-
bundleDependencies?: string[];
|
|
2390
|
-
|
|
2391
|
-
/**
|
|
2392
|
-
Engines that this package runs on.
|
|
2393
|
-
*/
|
|
2394
|
-
engines?: {
|
|
2395
|
-
[EngineName in 'npm' | 'node' | string]?: string;
|
|
2396
|
-
};
|
|
2397
|
-
|
|
2398
|
-
/**
|
|
2399
|
-
@deprecated
|
|
2400
|
-
*/
|
|
2401
|
-
engineStrict?: boolean;
|
|
2402
|
-
|
|
2403
|
-
/**
|
|
2404
|
-
Operating systems the module runs on.
|
|
2405
|
-
*/
|
|
2406
|
-
os?: Array<LiteralUnion<
|
|
2407
|
-
| 'aix'
|
|
2408
|
-
| 'darwin'
|
|
2409
|
-
| 'freebsd'
|
|
2410
|
-
| 'linux'
|
|
2411
|
-
| 'openbsd'
|
|
2412
|
-
| 'sunos'
|
|
2413
|
-
| 'win32'
|
|
2414
|
-
| '!aix'
|
|
2415
|
-
| '!darwin'
|
|
2416
|
-
| '!freebsd'
|
|
2417
|
-
| '!linux'
|
|
2418
|
-
| '!openbsd'
|
|
2419
|
-
| '!sunos'
|
|
2420
|
-
| '!win32',
|
|
2421
|
-
string
|
|
2422
|
-
>>;
|
|
2423
|
-
|
|
2424
|
-
/**
|
|
2425
|
-
CPU architectures the module runs on.
|
|
2426
|
-
*/
|
|
2427
|
-
cpu?: Array<LiteralUnion<
|
|
2428
|
-
| 'arm'
|
|
2429
|
-
| 'arm64'
|
|
2430
|
-
| 'ia32'
|
|
2431
|
-
| 'mips'
|
|
2432
|
-
| 'mipsel'
|
|
2433
|
-
| 'ppc'
|
|
2434
|
-
| 'ppc64'
|
|
2435
|
-
| 's390'
|
|
2436
|
-
| 's390x'
|
|
2437
|
-
| 'x32'
|
|
2438
|
-
| 'x64'
|
|
2439
|
-
| '!arm'
|
|
2440
|
-
| '!arm64'
|
|
2441
|
-
| '!ia32'
|
|
2442
|
-
| '!mips'
|
|
2443
|
-
| '!mipsel'
|
|
2444
|
-
| '!ppc'
|
|
2445
|
-
| '!ppc64'
|
|
2446
|
-
| '!s390'
|
|
2447
|
-
| '!s390x'
|
|
2448
|
-
| '!x32'
|
|
2449
|
-
| '!x64',
|
|
2450
|
-
string
|
|
2451
|
-
>>;
|
|
2452
|
-
|
|
2453
|
-
/**
|
|
2454
|
-
If set to `true`, a warning will be shown if package is installed locally. Useful if the package is primarily a command-line application that should be installed globally.
|
|
2455
|
-
|
|
2456
|
-
@deprecated
|
|
2457
|
-
*/
|
|
2458
|
-
preferGlobal?: boolean;
|
|
2459
|
-
|
|
2460
|
-
/**
|
|
2461
|
-
If set to `true`, then npm will refuse to publish it.
|
|
2462
|
-
*/
|
|
2463
|
-
private?: boolean;
|
|
2464
|
-
|
|
2465
|
-
/**
|
|
2466
|
-
A set of config values that will be used at publish-time. It's especially handy to set the tag, registry or access, to ensure that a given package is not tagged with 'latest', published to the global public registry or that a scoped module is private by default.
|
|
2467
|
-
*/
|
|
2468
|
-
publishConfig?: PublishConfig;
|
|
2469
|
-
|
|
2470
|
-
/**
|
|
2471
|
-
Describes and notifies consumers of a package's monetary support information.
|
|
2472
|
-
|
|
2473
|
-
[Read more.](https://github.com/npm/rfcs/blob/main/implemented/0017-add-funding-support.md)
|
|
2474
|
-
*/
|
|
2475
|
-
funding?: string | {
|
|
2476
|
-
/**
|
|
2477
|
-
The type of funding.
|
|
2478
|
-
*/
|
|
2479
|
-
type?: LiteralUnion<
|
|
2480
|
-
| 'github'
|
|
2481
|
-
| 'opencollective'
|
|
2482
|
-
| 'patreon'
|
|
2483
|
-
| 'individual'
|
|
2484
|
-
| 'foundation'
|
|
2485
|
-
| 'corporation',
|
|
2486
|
-
string
|
|
2487
|
-
>;
|
|
2488
|
-
|
|
2489
|
-
/**
|
|
2490
|
-
The URL to the funding page.
|
|
2491
|
-
*/
|
|
2492
|
-
url: string;
|
|
2493
|
-
};
|
|
2494
|
-
|
|
2495
|
-
/**
|
|
2496
|
-
Used to configure [npm workspaces](https://docs.npmjs.com/cli/using-npm/workspaces) / [Yarn workspaces](https://classic.yarnpkg.com/docs/workspaces/).
|
|
2497
|
-
|
|
2498
|
-
Workspaces allow you to manage multiple packages within the same repository in such a way that you only need to run your install command once in order to install all of them in a single pass.
|
|
2499
|
-
|
|
2500
|
-
Please note that the top-level `private` property of `package.json` **must** be set to `true` in order to use workspaces.
|
|
2501
|
-
*/
|
|
2502
|
-
workspaces?: WorkspacePattern[] | WorkspaceConfig;
|
|
2503
|
-
}
|
|
2504
|
-
|
|
2505
|
-
/**
|
|
2506
|
-
Type for [`package.json` file used by the Node.js runtime](https://nodejs.org/api/packages.html#nodejs-packagejson-field-definitions).
|
|
2507
|
-
*/
|
|
2508
|
-
export type NodeJsStandard = {
|
|
2509
|
-
/**
|
|
2510
|
-
Defines which package manager is expected to be used when working on the current project. It can set to any of the [supported package managers](https://nodejs.org/api/corepack.html#supported-package-managers), and will ensure that your teams use the exact same package manager versions without having to install anything else than Node.js.
|
|
2511
|
-
|
|
2512
|
-
__This field is currently experimental and needs to be opted-in; check the [Corepack](https://nodejs.org/api/corepack.html) page for details about the procedure.__
|
|
2513
|
-
|
|
2514
|
-
@example
|
|
2515
|
-
```json
|
|
2516
|
-
{
|
|
2517
|
-
"packageManager": "<package manager name>@<version>"
|
|
2518
|
-
}
|
|
2519
|
-
```
|
|
2520
|
-
*/
|
|
2521
|
-
packageManager?: string;
|
|
2522
|
-
};
|
|
2523
|
-
|
|
2524
|
-
export type PublishConfig = {
|
|
2525
|
-
/**
|
|
2526
|
-
Additional, less common properties from the [npm docs on `publishConfig`](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#publishconfig).
|
|
2527
|
-
*/
|
|
2528
|
-
[additionalProperties: string]: JsonValue | undefined;
|
|
2529
|
-
|
|
2530
|
-
/**
|
|
2531
|
-
When publishing scoped packages, the access level defaults to restricted. If you want your scoped package to be publicly viewable (and installable) set `--access=public`. The only valid values for access are public and restricted. Unscoped packages always have an access level of public.
|
|
2532
|
-
*/
|
|
2533
|
-
access?: 'public' | 'restricted';
|
|
2534
|
-
|
|
2535
|
-
/**
|
|
2536
|
-
The base URL of the npm registry.
|
|
2537
|
-
|
|
2538
|
-
Default: `'https://registry.npmjs.org/'`
|
|
2539
|
-
*/
|
|
2540
|
-
registry?: string;
|
|
2541
|
-
|
|
2542
|
-
/**
|
|
2543
|
-
The tag to publish the package under.
|
|
2544
|
-
|
|
2545
|
-
Default: `'latest'`
|
|
2546
|
-
*/
|
|
2547
|
-
tag?: string;
|
|
2548
|
-
};
|
|
2549
|
-
}
|
|
2550
|
-
|
|
2551
|
-
/**
|
|
2552
|
-
Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Also includes types for fields used by other popular projects, like TypeScript and Yarn.
|
|
2553
|
-
|
|
2554
|
-
@category File
|
|
2555
|
-
*/
|
|
2556
|
-
type PackageJson$1 =
|
|
2557
|
-
JsonObject &
|
|
2558
|
-
PackageJson$1.NodeJsStandard &
|
|
2559
|
-
PackageJson$1.PackageJsonStandard &
|
|
2560
|
-
PackageJson$1.NonStandardEntryPoints &
|
|
2561
|
-
PackageJson$1.TypeScriptConfiguration &
|
|
2562
|
-
PackageJson$1.YarnConfiguration &
|
|
2563
|
-
PackageJson$1.JSPMConfiguration;
|
|
2564
|
-
|
|
2565
|
-
interface InstallPackageOptions {
|
|
2566
|
-
cwd?: string;
|
|
2567
|
-
dev?: boolean;
|
|
2568
|
-
silent?: boolean;
|
|
2569
|
-
packageManager?: string;
|
|
2570
|
-
preferOffline?: boolean;
|
|
2571
|
-
additionalArgs?: string[] | ((agent: string, detectedAgent: string) => string[] | undefined);
|
|
2572
|
-
}
|
|
2573
|
-
|
|
2574
|
-
type Prettify<T> = {
|
|
2575
|
-
[K in keyof T]: T[K];
|
|
2576
|
-
} & {};
|
|
2577
|
-
type PartialDeep<T> = T extends object ? {
|
|
2578
|
-
[P in keyof T]?: PartialDeep<T[P]>;
|
|
2579
|
-
} : T;
|
|
2580
|
-
|
|
2581
|
-
/**
|
|
2582
|
-
* Union type representing the possible statuses of a prompt.
|
|
2583
|
-
*
|
|
2584
|
-
* - `'loading'`: The prompt is currently loading.
|
|
2585
|
-
* - `'idle'`: The prompt is loaded and currently waiting for the user to
|
|
2586
|
-
* submit an answer.
|
|
2587
|
-
* - `'done'`: The user has submitted an answer and the prompt is finished.
|
|
2588
|
-
* - `string`: Any other string: The prompt is in a custom state.
|
|
2589
|
-
*/
|
|
2590
|
-
type Status = 'loading' | 'idle' | 'done' | (string & {});
|
|
2591
|
-
type DefaultTheme = {
|
|
2592
|
-
/**
|
|
2593
|
-
* Prefix to prepend to the message. If a function is provided, it will be
|
|
2594
|
-
* called with the current status of the prompt, and the return value will be
|
|
2595
|
-
* used as the prefix.
|
|
2596
|
-
*
|
|
2597
|
-
* @remarks
|
|
2598
|
-
* If `status === 'loading'`, this property is ignored and the spinner (styled
|
|
2599
|
-
* by the `spinner` property) will be displayed instead.
|
|
2600
|
-
*
|
|
2601
|
-
* @defaultValue
|
|
2602
|
-
* ```ts
|
|
2603
|
-
* // import colors from 'yoctocolors-cjs';
|
|
2604
|
-
* (status) => status === 'done' ? colors.green('✔') : colors.blue('?')
|
|
2605
|
-
* ```
|
|
2606
|
-
*/
|
|
2607
|
-
prefix: string | Prettify<Omit<Record<Status, string>, 'loading'>>;
|
|
2608
|
-
/**
|
|
2609
|
-
* Configuration for the spinner that is displayed when the prompt is in the
|
|
2610
|
-
* `'loading'` state.
|
|
2611
|
-
*
|
|
2612
|
-
* We recommend the use of {@link https://github.com/sindresorhus/cli-spinners|cli-spinners} for a list of available spinners.
|
|
2613
|
-
*/
|
|
2614
|
-
spinner: {
|
|
2615
|
-
/**
|
|
2616
|
-
* The time interval between frames, in milliseconds.
|
|
2617
|
-
*
|
|
2618
|
-
* @defaultValue
|
|
2619
|
-
* ```ts
|
|
2620
|
-
* 80
|
|
2621
|
-
* ```
|
|
2622
|
-
*/
|
|
2623
|
-
interval: number;
|
|
2624
|
-
/**
|
|
2625
|
-
* A list of frames to show for the spinner.
|
|
2626
|
-
*
|
|
2627
|
-
* @defaultValue
|
|
2628
|
-
* ```ts
|
|
2629
|
-
* ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
|
|
2630
|
-
* ```
|
|
2631
|
-
*/
|
|
2632
|
-
frames: string[];
|
|
2633
|
-
};
|
|
2634
|
-
/**
|
|
2635
|
-
* Object containing functions to style different parts of the prompt.
|
|
2636
|
-
*/
|
|
2637
|
-
style: {
|
|
2638
|
-
/**
|
|
2639
|
-
* Style to apply to the user's answer once it has been submitted.
|
|
2640
|
-
*
|
|
2641
|
-
* @param text - The user's answer.
|
|
2642
|
-
* @returns The styled answer.
|
|
2643
|
-
*
|
|
2644
|
-
* @defaultValue
|
|
2645
|
-
* ```ts
|
|
2646
|
-
* // import colors from 'yoctocolors-cjs';
|
|
2647
|
-
* (text) => colors.cyan(text)
|
|
2648
|
-
* ```
|
|
2649
|
-
*/
|
|
2650
|
-
answer: (text: string) => string;
|
|
2651
|
-
/**
|
|
2652
|
-
* Style to apply to the message displayed to the user.
|
|
2653
|
-
*
|
|
2654
|
-
* @param text - The message to style.
|
|
2655
|
-
* @param status - The current status of the prompt.
|
|
2656
|
-
* @returns The styled message.
|
|
2657
|
-
*
|
|
2658
|
-
* @defaultValue
|
|
2659
|
-
* ```ts
|
|
2660
|
-
* // import colors from 'yoctocolors-cjs';
|
|
2661
|
-
* (text, status) => colors.bold(text)
|
|
2662
|
-
* ```
|
|
2663
|
-
*/
|
|
2664
|
-
message: (text: string, status: Status) => string;
|
|
2665
|
-
/**
|
|
2666
|
-
* Style to apply to error messages.
|
|
2667
|
-
*
|
|
2668
|
-
* @param text - The error message.
|
|
2669
|
-
* @returns The styled error message.
|
|
2670
|
-
*
|
|
2671
|
-
* @defaultValue
|
|
2672
|
-
* ```ts
|
|
2673
|
-
* // import colors from 'yoctocolors-cjs';
|
|
2674
|
-
* (text) => colors.red(`> ${text}`)
|
|
2675
|
-
* ```
|
|
2676
|
-
*/
|
|
2677
|
-
error: (text: string) => string;
|
|
2678
|
-
/**
|
|
2679
|
-
* Style to apply to the default answer when one is provided.
|
|
2680
|
-
*
|
|
2681
|
-
* @param text - The default answer.
|
|
2682
|
-
* @returns The styled default answer.
|
|
2683
|
-
*
|
|
2684
|
-
* @defaultValue
|
|
2685
|
-
* ```ts
|
|
2686
|
-
* // import colors from 'yoctocolors-cjs';
|
|
2687
|
-
* (text) => colors.dim(`(${text})`)
|
|
2688
|
-
* ```
|
|
2689
|
-
*/
|
|
2690
|
-
defaultAnswer: (text: string) => string;
|
|
2691
|
-
/**
|
|
2692
|
-
* Style to apply to help text.
|
|
2693
|
-
*
|
|
2694
|
-
* @param text - The help text.
|
|
2695
|
-
* @returns The styled help text.
|
|
2696
|
-
*
|
|
2697
|
-
* @defaultValue
|
|
2698
|
-
* ```ts
|
|
2699
|
-
* // import colors from 'yoctocolors-cjs';
|
|
2700
|
-
* (text) => colors.dim(text)
|
|
2701
|
-
* ```
|
|
2702
|
-
*/
|
|
2703
|
-
help: (text: string) => string;
|
|
2704
|
-
/**
|
|
2705
|
-
* Style to apply to highlighted text.
|
|
2706
|
-
*
|
|
2707
|
-
* @param text - The text to highlight.
|
|
2708
|
-
* @returns The highlighted text.
|
|
2709
|
-
*
|
|
2710
|
-
* @defaultValue
|
|
2711
|
-
* ```ts
|
|
2712
|
-
* // import colors from 'yoctocolors-cjs';
|
|
2713
|
-
* (text) => colors.cyan(text)
|
|
2714
|
-
* ```
|
|
2715
|
-
*/
|
|
2716
|
-
highlight: (text: string) => string;
|
|
2717
|
-
/**
|
|
2718
|
-
* Style to apply to keyboard keys referred to in help texts.
|
|
2719
|
-
*
|
|
2720
|
-
* @param text - The key to style.
|
|
2721
|
-
* @returns The styled key.
|
|
2722
|
-
*
|
|
2723
|
-
* @defaultValue
|
|
2724
|
-
* ```ts
|
|
2725
|
-
* // import colors from 'yoctocolors-cjs';
|
|
2726
|
-
* (text) => colors.cyan(colors.bold(`<${text}>`))
|
|
2727
|
-
* ```
|
|
2728
|
-
*/
|
|
2729
|
-
key: (text: string) => string;
|
|
2730
|
-
};
|
|
2731
|
-
};
|
|
2732
|
-
type Theme<Extension extends object = object> = Prettify<Extension & DefaultTheme>;
|
|
2733
|
-
|
|
2734
|
-
type NormalizedPackageJson = Package & PackageJson;
|
|
2735
|
-
type PackageJson = PackageJson$1;
|
|
2736
|
-
type Cache<T = any> = Map<string, T>;
|
|
2737
|
-
type EnsurePackagesOptions = {
|
|
2738
|
-
confirm?: {
|
|
2739
|
-
default?: boolean;
|
|
2740
|
-
message: string | ((packages: string[]) => string);
|
|
2741
|
-
theme?: PartialDeep<Theme>;
|
|
2742
|
-
transformer?: (value: boolean) => string;
|
|
2743
|
-
};
|
|
2744
|
-
cwd?: URL | string;
|
|
2745
|
-
deps?: boolean;
|
|
2746
|
-
devDeps?: boolean;
|
|
2747
|
-
installPackage?: Omit<InstallPackageOptions, "cwd" | "dev">;
|
|
2748
|
-
logger?: {
|
|
2749
|
-
warn: (message: string) => void;
|
|
2750
|
-
};
|
|
2751
|
-
peerDeps?: boolean;
|
|
2752
|
-
throwOnWarn?: boolean;
|
|
2753
|
-
};
|
|
2754
|
-
|
|
2755
|
-
type ReadOptions = {
|
|
2756
|
-
cache?: FindPackageJsonCache | boolean;
|
|
2757
|
-
ignoreWarnings?: (RegExp | string)[];
|
|
2758
|
-
strict?: boolean;
|
|
2759
|
-
};
|
|
2760
|
-
type FindPackageJsonCache = Cache<NormalizedReadResult>;
|
|
2761
|
-
type NormalizedReadResult = {
|
|
2762
|
-
packageJson: NormalizedPackageJson;
|
|
2763
|
-
path: string;
|
|
2764
|
-
};
|
|
2765
|
-
declare const findPackageJson: (cwd?: URL | string, options?: ReadOptions) => Promise<NormalizedReadResult>;
|
|
2766
|
-
declare const findPackageJsonSync: (cwd?: URL | string, options?: ReadOptions) => NormalizedReadResult;
|
|
2767
|
-
declare const writePackageJson: <T = PackageJson>(data: T, options?: WriteJsonOptions & {
|
|
2768
|
-
cwd?: URL | string;
|
|
2769
|
-
}) => Promise<void>;
|
|
2770
|
-
declare const writePackageJsonSync: <T = PackageJson>(data: T, options?: WriteJsonOptions & {
|
|
2771
|
-
cwd?: URL | string;
|
|
2772
|
-
}) => void;
|
|
2773
|
-
declare const parsePackageJson: (packageFile: JsonObject | string, options?: {
|
|
2774
|
-
ignoreWarnings?: (RegExp | string)[];
|
|
2775
|
-
strict?: boolean;
|
|
2776
|
-
}) => NormalizedPackageJson;
|
|
2777
|
-
declare const getPackageJsonProperty: <T = unknown>(packageJson: NormalizedPackageJson, property: Paths<NormalizedPackageJson>, defaultValue?: T) => T;
|
|
2778
|
-
declare const hasPackageJsonProperty: (packageJson: NormalizedPackageJson, property: Paths<NormalizedPackageJson>) => boolean;
|
|
2779
|
-
declare const hasPackageJsonAnyDependency: (packageJson: NormalizedPackageJson, arguments_: string[], options?: {
|
|
2780
|
-
peerDeps?: boolean;
|
|
2781
|
-
}) => boolean;
|
|
2782
|
-
declare const ensurePackages: (packageJson: NormalizedPackageJson, packages: string[], installKey?: "dependencies" | "devDependencies", options?: EnsurePackagesOptions) => Promise<void>;
|
|
2783
|
-
|
|
2784
|
-
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 };
|