@visulima/package 3.5.12 → 3.6.1

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.
@@ -47,42 +47,83 @@ Matches any valid JSON value.
47
47
  */
48
48
  type JsonValue = JsonPrimitive | JsonObject | JsonArray;
49
49
 
50
- declare global {
51
- // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
52
- interface SymbolConstructor {
53
- readonly observable: symbol;
54
- }
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];
55
66
  }
56
67
 
57
- declare const emptyObjectSymbol: unique symbol;
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;
58
79
 
59
80
  /**
60
- Represents a strictly empty plain object, the `{}` value.
81
+ Returns a boolean for whether the given key is an optional key of type.
61
82
 
62
- When you annotate something as the type `{}`, it can be anything except `null` and `undefined`. This means that you cannot use `{}` to represent an empty plain object ([read more](https://stackoverflow.com/questions/47339869/typescript-empty-object-and-any-difference/52193484#52193484)).
83
+ This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
63
84
 
64
85
  @example
65
86
  ```
66
- import type {EmptyObject} from 'type-fest';
87
+ import type {IsOptionalKeyOf} from 'type-fest';
67
88
 
68
- // The following illustrates the problem with `{}`.
69
- const foo1: {} = {}; // Pass
70
- const foo2: {} = []; // Pass
71
- const foo3: {} = 42; // Pass
72
- const foo4: {} = {a: 1}; // Pass
89
+ interface User {
90
+ name: string;
91
+ surname: string;
73
92
 
74
- // With `EmptyObject` only the first case is valid.
75
- const bar1: EmptyObject = {}; // Pass
76
- const bar2: EmptyObject = 42; // Fail
77
- const bar3: EmptyObject = []; // Fail
78
- const bar4: EmptyObject = {a: 1}; // Fail
79
- ```
93
+ luckyNumber?: number;
94
+ }
95
+
96
+ interface Admin {
97
+ name: string;
98
+ surname?: string;
99
+ }
80
100
 
81
- Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<never, never>` do not work. See {@link https://github.com/sindresorhus/type-fest/issues/395 #395}.
101
+ type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
102
+ //=> true
82
103
 
83
- @category Object
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
84
119
  */
85
- type EmptyObject = {[emptyObjectSymbol]?: never};
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;
86
127
 
87
128
  /**
88
129
  Extract all optional keys from the given type.
@@ -117,11 +158,14 @@ const update2: UpdateOperation<User> = {
117
158
 
118
159
  @category Utilities
119
160
  */
120
- type OptionalKeysOf<BaseType extends object> =
121
- BaseType extends unknown // For distributing `BaseType`
122
- ? (keyof {
123
- [Key in keyof BaseType as BaseType extends Record<Key, BaseType[Key]> ? never : Key]: never
124
- }) & (keyof BaseType) // Intersect with `keyof BaseType` to ensure result of `OptionalKeysOf<BaseType>` is always assignable to `keyof BaseType`
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`
125
169
  : never; // Should never happen
126
170
 
127
171
  /**
@@ -148,9 +192,9 @@ const validator2 = createValidation<User>('surname', value => value.length < 25)
148
192
 
149
193
  @category Utilities
150
194
  */
151
- type RequiredKeysOf<BaseType extends object> =
152
- BaseType extends unknown // For distributing `BaseType`
153
- ? Exclude<keyof BaseType, OptionalKeysOf<BaseType>>
195
+ type RequiredKeysOf<Type extends object> =
196
+ Type extends unknown // For distributing `Type`
197
+ ? Exclude<keyof Type, OptionalKeysOf<Type>>
154
198
  : never; // Should never happen
155
199
 
156
200
  /**
@@ -197,27 +241,68 @@ endIfEqual('abc', '123');
197
241
  type IsNever<T> = [T] extends [never] ? true : false;
198
242
 
199
243
  /**
200
- An if-else-like type that resolves depending on whether the given type is `never`.
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'>`.
201
248
 
202
- @see {@link IsNever}
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'`.
203
252
 
204
253
  @example
205
254
  ```
206
- import type {IfNever} from 'type-fest';
255
+ import {If} from 'type-fest';
207
256
 
208
- type ShouldBeTrue = IfNever<never>;
209
- //=> true
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';
210
287
 
211
- type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
212
- //=> 'bar'
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'
213
295
  ```
214
296
 
215
297
  @category Type Guard
216
298
  @category Utilities
217
299
  */
218
- type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
219
- IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
220
- );
300
+ type If<Type extends boolean, IfBranch, ElseBranch> =
301
+ IsNever<Type> extends true
302
+ ? ElseBranch
303
+ : Type extends true
304
+ ? IfBranch
305
+ : ElseBranch;
221
306
 
222
307
  /**
223
308
  Represents an array with `unknown` value.
@@ -245,6 +330,89 @@ type C = IsArray<string>;
245
330
  */
246
331
  type UnknownArray = readonly unknown[];
247
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
+
248
416
  /**
249
417
  Returns the static, fixed-length portion of the given array, excluding variable-length parts.
250
418
 
@@ -281,39 +449,66 @@ type VariablePartOfArray<T extends UnknownArray> =
281
449
  : []
282
450
  : never;
283
451
 
284
- // Can eventually be replaced with the built-in once this library supports
285
- // TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
286
- type NoInfer<T> = T extends infer U ? U : never;
287
-
288
452
  /**
289
- Returns a boolean for whether the given type is `any`.
290
-
291
- @link https://stackoverflow.com/a/49928360/1490091
292
-
293
- Useful in type utilities, such as disallowing `any`s to be passed to a function.
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.
294
454
 
295
455
  @example
296
456
  ```
297
- import type {IsAny} from 'type-fest';
457
+ type A = CollapseRestElement<[string, string, ...number[]]>;
458
+ //=> [string, string, number]
298
459
 
299
- const typedObject = {a: 1, b: 2} as const;
300
- const anyObject: any = {a: 1, b: 2};
460
+ type B = CollapseRestElement<[...string[], number, number]>;
461
+ //=> [string, number, number]
301
462
 
302
- function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
303
- return obj[key];
304
- }
463
+ type C = CollapseRestElement<[string, string, ...Array<number | bigint>]>;
464
+ //=> [string, string, number | bigint]
305
465
 
306
- const typedA = get(typedObject, 'a');
307
- //=> 1
466
+ type D = CollapseRestElement<[string, number]>;
467
+ //=> [string, number]
468
+ ```
308
469
 
309
- const anyA = get(anyObject, 'a');
310
- //=> any
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
311
473
  ```
474
+ // `exactOptionalPropertyTypes` enabled
475
+ type A = CollapseRestElement<[string?, string?, ...number[]]>;
476
+ //=> [string, string, number]
312
477
 
313
- @category Type Guard
314
- @category Utilities
478
+ // `exactOptionalPropertyTypes` disabled
479
+ type B = CollapseRestElement<[string?, string?, ...number[]]>;
480
+ //=> [string | undefined, string | undefined, number]
481
+ ```
315
482
  */
316
- type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
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
317
512
 
318
513
  type Numeric = number | bigint;
319
514
 
@@ -329,7 +524,7 @@ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277)
329
524
  @category Numeric
330
525
  */
331
526
  // See https://github.com/microsoft/TypeScript/issues/31752
332
- // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
527
+ // eslint-disable-next-line no-loss-of-precision
333
528
  type PositiveInfinity = 1e999;
334
529
 
335
530
  /**
@@ -342,7 +537,7 @@ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277)
342
537
  @category Numeric
343
538
  */
344
539
  // See https://github.com/microsoft/TypeScript/issues/31752
345
- // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
540
+ // eslint-disable-next-line no-loss-of-precision
346
541
  type NegativeInfinity = -1e999;
347
542
 
348
543
  /**
@@ -407,737 +602,918 @@ type IsEqual<A, B> =
407
602
  : false;
408
603
 
409
604
  /**
410
- Returns a boolean for whether two given types are both true.
411
-
412
- Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
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.
413
606
 
414
607
  @example
415
608
  ```
416
- import type {And} from 'type-fest';
609
+ import type {Simplify} from 'type-fest';
417
610
 
418
- And<true, true>;
419
- //=> true
611
+ type PositionProps = {
612
+ top: number;
613
+ left: number;
614
+ };
420
615
 
421
- And<true, false>;
422
- //=> false
423
- ```
616
+ type SizeProps = {
617
+ width: number;
618
+ height: number;
619
+ };
424
620
 
425
- @see {@link Or}
426
- */
427
- type And<A extends boolean, B extends boolean> = [A, B][number] extends true
428
- ? true
429
- : true extends [IsEqual<A, false>, IsEqual<B, false>][number]
430
- ? false
431
- : never;
621
+ // In your editor, hovering over `Props` will show a flattened object with all the properties.
622
+ type Props = Simplify<PositionProps & SizeProps>;
623
+ ```
432
624
 
433
- /**
434
- Returns a boolean for whether either of two given types are true.
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.
435
626
 
436
- Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
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`.
437
628
 
438
629
  @example
439
630
  ```
440
- import type {Or} from 'type-fest';
631
+ import type {Simplify} from 'type-fest';
441
632
 
442
- Or<true, false>;
443
- //=> true
633
+ interface SomeInterface {
634
+ foo: number;
635
+ bar?: string;
636
+ baz: number | undefined;
637
+ }
444
638
 
445
- Or<false, false>;
446
- //=> false
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`
447
655
  ```
448
656
 
449
- @see {@link And}
657
+ @link https://github.com/microsoft/TypeScript/issues/15300
658
+ @see SimplifyDeep
659
+ @category Object
450
660
  */
451
- type Or<A extends boolean, B extends boolean> = [A, B][number] extends false
452
- ? false
453
- : true extends [IsEqual<A, true>, IsEqual<B, true>][number]
454
- ? true
455
- : never;
661
+ type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
456
662
 
457
663
  /**
458
- Returns a boolean for whether a given number is greater than another number.
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.)
459
676
 
460
- @example
461
677
  ```
462
- import type {GreaterThan} from 'type-fest';
678
+ const indexed: Record<string, unknown> = {}; // Allowed
463
679
 
464
- GreaterThan<1, -5>;
465
- //=> true
680
+ const keyed: Record<'foo', unknown> = {}; // Error
681
+ // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
682
+ ```
466
683
 
467
- GreaterThan<1, 1>;
468
- //=> false
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:
469
685
 
470
- GreaterThan<1, 5>;
471
- //=> false
472
686
  ```
473
- */
474
- type GreaterThan<A extends number, B extends number> =
475
- A extends number // For distributing `A`
476
- ? B extends number // For distributing `B`
477
- ? number extends A | B
478
- ? never
479
- : [
480
- IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
481
- IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
482
- ] extends infer R extends [boolean, boolean, boolean, boolean]
483
- ? Or<
484
- And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
485
- And<IsEqual<R[3], true>, IsEqual<R[1], false>>
486
- > extends true
487
- ? true
488
- : Or<
489
- And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
490
- And<IsEqual<R[2], true>, IsEqual<R[0], false>>
491
- > extends true
492
- ? false
493
- : true extends R[number]
494
- ? false
495
- : [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean]
496
- ? [true, false] extends R
497
- ? false
498
- : [false, true] extends R
499
- ? true
500
- : [false, false] extends R
501
- ? PositiveNumericStringGt<`${A}`, `${B}`>
502
- : PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
503
- : never
504
- : never
505
- : never // Should never happen
506
- : never; // Should never happen
507
-
508
- /**
509
- Returns a boolean for whether a given number is greater than or equal to another number.
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>`'
510
691
 
511
- @example
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>`"
512
696
  ```
513
- import type {GreaterThanOrEqual} from 'type-fest';
514
697
 
515
- GreaterThanOrEqual<1, -5>;
516
- //=> true
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`...
517
699
 
518
- GreaterThanOrEqual<1, 1>;
519
- //=> true
700
+ ```
701
+ import type {OmitIndexSignature} from 'type-fest';
520
702
 
521
- GreaterThanOrEqual<1, 5>;
522
- //=> false
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
+ };
523
707
  ```
524
- */
525
- type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
526
- ? never
527
- : A extends B ? true : GreaterThan<A, B>;
528
708
 
529
- /**
530
- Returns a boolean for whether a given number is less than another number.
709
+ ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
531
710
 
532
- @example
533
711
  ```
534
- import type {LessThan} from 'type-fest';
712
+ import type {OmitIndexSignature} from 'type-fest';
535
713
 
536
- LessThan<1, -5>;
537
- //=> false
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
+ ```
538
723
 
539
- LessThan<1, 1>;
540
- //=> false
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.
541
725
 
542
- LessThan<1, 5>;
543
- //=> true
726
+ @example
544
727
  ```
545
- */
546
- type LessThan<A extends number, B extends number> = number extends A | B
547
- ? never
548
- : GreaterThanOrEqual<A, B> extends infer Result
549
- ? Result extends true
550
- ? false
551
- : true
552
- : never; // Should never happen
728
+ import type {OmitIndexSignature} from 'type-fest';
553
729
 
554
- // Should never happen
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
555
740
 
556
- /**
557
- Create a tuple type of the given length `<L>` and fill it with the given type `<Fill>`.
741
+ // These explicitly defined keys will remain.
742
+ foo: 'bar';
743
+ qux?: 'baz';
744
+ }
558
745
 
559
- If `<Fill>` is not provided, it will default to `unknown`.
746
+ type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
747
+ // => { foo: 'bar'; qux?: 'baz' | undefined; }
748
+ ```
560
749
 
561
- @link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
750
+ @see PickIndexSignature
751
+ @category Object
562
752
  */
563
- type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = number extends L
564
- ? Fill[]
565
- : L extends T['length']
566
- ? T
567
- : BuildTuple<L, Fill, [...T, Fill]>;
753
+ type OmitIndexSignature<ObjectType> = {
754
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
755
+ ? never
756
+ : KeyType]: ObjectType[KeyType];
757
+ };
568
758
 
569
759
  /**
570
- Return a string representation of the given string or number.
571
-
572
- Note: This type is not the return type of the `.toString()` function.
573
- */
574
- type ToString<T> = T extends string | number ? `${T}` : never;
760
+ Pick only index signatures from the given object type, leaving out all explicitly defined properties.
575
761
 
576
- /**
577
- Converts a numeric string to a number.
762
+ This is the counterpart of `OmitIndexSignature`.
578
763
 
579
764
  @example
580
765
  ```
581
- type PositiveInt = StringToNumber<'1234'>;
582
- //=> 1234
583
-
584
- type NegativeInt = StringToNumber<'-1234'>;
585
- //=> -1234
766
+ import type {PickIndexSignature} from 'type-fest';
586
767
 
587
- type PositiveFloat = StringToNumber<'1234.56'>;
588
- //=> 1234.56
768
+ declare const symbolKey: unique symbol;
589
769
 
590
- type NegativeFloat = StringToNumber<'-1234.56'>;
591
- //=> -1234.56
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;
592
780
 
593
- type PositiveInfinity = StringToNumber<'Infinity'>;
594
- //=> Infinity
781
+ // These explicitly defined keys will be removed.
782
+ ['kebab-case-key']: string;
783
+ [symbolKey]: string;
784
+ foo: 'bar';
785
+ qux?: 'baz';
786
+ };
595
787
 
596
- type NegativeInfinity = StringToNumber<'-Infinity'>;
597
- //=> -Infinity
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
+ // }
598
799
  ```
599
800
 
600
- @category String
601
- @category Numeric
602
- @category Template literal
801
+ @see OmitIndexSignature
802
+ @category Object
603
803
  */
604
- type StringToNumber<S extends string> = S extends `${infer N extends number}`
605
- ? N
606
- : S extends 'Infinity'
607
- ? PositiveInfinity
608
- : S extends '-Infinity'
609
- ? NegativeInfinity
610
- : never;
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;
611
814
 
612
815
  /**
613
- Returns an array of the characters of the string.
816
+ Merge two types into a new type. Keys of the second type overrides keys of the first type.
614
817
 
615
818
  @example
616
819
  ```
617
- StringToArray<'abcde'>;
618
- //=> ['a', 'b', 'c', 'd', 'e']
820
+ import type {Merge} from 'type-fest';
619
821
 
620
- StringToArray<string>;
621
- //=> never
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
+ // }
622
845
  ```
623
846
 
624
- @category String
847
+ @category Object
625
848
  */
626
- type StringToArray<S extends string, Result extends string[] = []> = string extends S
627
- ? never
628
- : S extends `${infer F}${infer R}`
629
- ? StringToArray<R, [...Result, F]>
630
- : Result;
849
+ type Merge<Destination, Source> =
850
+ Simplify<
851
+ SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
852
+ & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
853
+ >;
631
854
 
632
855
  /**
633
- Returns the length of the given string.
856
+ Merges user specified options with default options.
634
857
 
635
858
  @example
636
859
  ```
637
- StringLength<'abcde'>;
638
- //=> 5
860
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
861
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
862
+ type SpecifiedOptions = {leavesOnly: true};
639
863
 
640
- StringLength<string>;
641
- //=> never
864
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
865
+ //=> {maxRecursionDepth: 10; leavesOnly: true}
642
866
  ```
643
867
 
644
- @category String
645
- @category Template literal
646
- */
647
- type StringLength<S extends string> = string extends S
648
- ? never
649
- : StringToArray<S>['length'];
868
+ @example
869
+ ```
870
+ // Complains if default values are not provided for optional options
650
871
 
651
- /**
652
- 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.
872
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
873
+ type DefaultPathsOptions = {maxRecursionDepth: 10};
874
+ type SpecifiedOptions = {};
653
875
 
654
- @example
876
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
877
+ // ~~~~~~~~~~~~~~~~~~~
878
+ // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
655
879
  ```
656
- SameLengthPositiveNumericStringGt<'50', '10'>;
657
- //=> true
658
880
 
659
- SameLengthPositiveNumericStringGt<'10', '10'>;
660
- //=> false
881
+ @example
661
882
  ```
662
- */
663
- type SameLengthPositiveNumericStringGt<A extends string, B extends string> = A extends `${infer FirstA}${infer RestA}`
664
- ? B extends `${infer FirstB}${infer RestB}`
665
- ? FirstA extends FirstB
666
- ? SameLengthPositiveNumericStringGt<RestA, RestB>
667
- : PositiveNumericCharacterGt<FirstA, FirstB>
668
- : never
669
- : false;
883
+ // Complains if an option's default type does not conform to the expected type
670
884
 
671
- type NumericString = '0123456789';
885
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
886
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
887
+ type SpecifiedOptions = {};
672
888
 
673
- /**
674
- Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are both positive numeric strings.
889
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
890
+ // ~~~~~~~~~~~~~~~~~~~
891
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
892
+ ```
675
893
 
676
894
  @example
677
895
  ```
678
- PositiveNumericStringGt<'500', '1'>;
679
- //=> true
896
+ // Complains if an option's specified type does not conform to the expected type
680
897
 
681
- PositiveNumericStringGt<'1', '1'>;
682
- //=> false
898
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
899
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
900
+ type SpecifiedOptions = {leavesOnly: 'yes'};
683
901
 
684
- PositiveNumericStringGt<'1', '500'>;
685
- //=> false
902
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
903
+ // ~~~~~~~~~~~~~~~~
904
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
686
905
  ```
687
906
  */
688
- type PositiveNumericStringGt<A extends string, B extends string> = A extends B
689
- ? false
690
- : [BuildTuple<StringLength<A>, 0>, BuildTuple<StringLength<B>, 0>] extends infer R extends [readonly unknown[], readonly unknown[]]
691
- ? R[0] extends [...R[1], ...infer Remain extends readonly unknown[]]
692
- ? 0 extends Remain['length']
693
- ? SameLengthPositiveNumericStringGt<A, B>
694
- : true
695
- : false
696
- : never;
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>>>>;
697
919
 
698
920
  /**
699
- Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters.
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.
700
924
 
701
925
  @example
702
926
  ```
703
- PositiveNumericCharacterGt<'5', '1'>;
927
+ import type {Or} from 'type-fest';
928
+
929
+ type TT = Or<true, false>;
704
930
  //=> true
705
931
 
706
- PositiveNumericCharacterGt<'1', '1'>;
932
+ type TF = Or<true, false>;
933
+ //=> true
934
+
935
+ type FT = Or<false, true>;
936
+ //=> true
937
+
938
+ type FF = Or<false, false>;
707
939
  //=> false
708
940
  ```
709
- */
710
- type PositiveNumericCharacterGt<A extends string, B extends string> = NumericString extends `${infer HeadA}${A}${infer TailA}`
711
- ? NumericString extends `${infer HeadB}${B}${infer TailB}`
712
- ? HeadA extends `${HeadB}${infer _}${infer __}`
713
- ? true
714
- : false
715
- : never
716
- : never;
717
941
 
718
- /**
719
- Returns the absolute value of a given value.
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.
720
965
 
721
966
  @example
722
967
  ```
723
- NumberAbsolute<-1>;
724
- //=> 1
968
+ import type {Or} from 'type-fest';
725
969
 
726
- NumberAbsolute<1>;
727
- //=> 1
970
+ type A = Or<true, never>;
971
+ //=> true
728
972
 
729
- NumberAbsolute<NegativeInfinity>
730
- //=> PositiveInfinity
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
731
990
  ```
991
+
992
+ @see {@link And}
732
993
  */
733
- type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N;
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;
734
1002
 
735
1003
  /**
736
- Check whether the given type is a number or a number string.
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`).
737
1009
 
738
- Supports floating-point as a string.
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.
739
1053
 
740
1054
  @example
741
1055
  ```
742
- type A = IsNumberLike<'1'>;
743
- //=> true
1056
+ import type {AllExtend} from 'type-fest';
744
1057
 
745
- type B = IsNumberLike<'-1.1'>;
1058
+ type A = AllExtend<[1, 2, 3], number>;
746
1059
  //=> true
747
1060
 
748
- type C = IsNumberLike<1>;
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>;
749
1078
  //=> true
750
1079
 
751
- type D = IsNumberLike<'a'>;
1080
+ // `exactOptionalPropertyTypes` disabled
1081
+ type B = AllExtend<[1?, 2?, 3?], number>;
752
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
753
1093
  */
754
- type IsNumberLike<N> =
755
- N extends number ? true
756
- : N extends `${number}`
757
- ? true
758
- : N extends `${number}.${number}`
759
- ? true
760
- : false;
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>;
761
1109
 
762
1110
  /**
763
- Returns the number with reversed sign.
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.
764
1114
 
765
1115
  @example
766
1116
  ```
767
- ReverseSign<-1>;
768
- //=> 1
1117
+ import type {And} from 'type-fest';
769
1118
 
770
- ReverseSign<1>;
771
- //=> -1
1119
+ type TT = And<true, true>;
1120
+ //=> true
1121
+
1122
+ type TF = And<true, false>;
1123
+ //=> false
772
1124
 
773
- ReverseSign<NegativeInfinity>
774
- //=> PositiveInfinity
1125
+ type FT = And<false, true>;
1126
+ //=> false
775
1127
 
776
- ReverseSign<PositiveInfinity>
777
- //=> NegativeInfinity
1128
+ type FF = And<false, false>;
1129
+ //=> false
778
1130
  ```
779
- */
780
- type ReverseSign<N extends number> =
781
- // Handle edge cases
782
- N extends 0 ? 0 : N extends PositiveInfinity ? NegativeInfinity : N extends NegativeInfinity ? PositiveInfinity :
783
- // Handle negative numbers
784
- `${N}` extends `-${infer P extends number}` ? P
785
- // Handle positive numbers
786
- : `-${N}` extends `${infer R extends number}` ? R : never;
787
1131
 
788
- /**
789
- 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.
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`).
790
1134
 
791
1135
  @example
792
1136
  ```
793
- import type {Simplify} from 'type-fest';
1137
+ import type {And} from 'type-fest';
794
1138
 
795
- type PositionProps = {
796
- top: number;
797
- left: number;
798
- };
1139
+ type A = And<true, boolean>;
1140
+ //=> boolean
799
1141
 
800
- type SizeProps = {
801
- width: number;
802
- height: number;
803
- };
1142
+ type B = And<boolean, true>;
1143
+ //=> boolean
804
1144
 
805
- // In your editor, hovering over `Props` will show a flattened object with all the properties.
806
- type Props = Simplify<PositionProps & SizeProps>;
807
- ```
1145
+ type C = And<false, boolean>;
1146
+ //=> false
808
1147
 
809
- 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.
1148
+ type D = And<boolean, false>;
1149
+ //=> false
810
1150
 
811
- If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
1151
+ type E = And<boolean, boolean>;
1152
+ //=> boolean
1153
+ ```
812
1154
 
1155
+ Note: If either of the types is `never`, the result becomes `false`.
813
1156
  @example
814
1157
  ```
815
- import type {Simplify} from 'type-fest';
1158
+ import type {And} from 'type-fest';
816
1159
 
817
- interface SomeInterface {
818
- foo: number;
819
- bar?: string;
820
- baz: number | undefined;
821
- }
1160
+ type A = And<true, never>;
1161
+ //=> false
822
1162
 
823
- type SomeType = {
824
- foo: number;
825
- bar?: string;
826
- baz: number | undefined;
827
- };
1163
+ type B = And<never, true>;
1164
+ //=> false
828
1165
 
829
- const literal = {foo: 123, bar: 'hello', baz: 456};
830
- const someType: SomeType = literal;
831
- const someInterface: SomeInterface = literal;
1166
+ type C = And<false, never>;
1167
+ //=> false
832
1168
 
833
- function fn(object: Record<string, unknown>): void {}
1169
+ type D = And<never, false>;
1170
+ //=> false
834
1171
 
835
- fn(literal); // Good: literal object type is sealed
836
- fn(someType); // Good: type is sealed
837
- fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
838
- fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
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
839
1180
  ```
840
1181
 
841
- @link https://github.com/microsoft/TypeScript/issues/15300
842
- @see SimplifyDeep
843
- @category Object
1182
+ @see {@link Or}
844
1183
  */
845
- type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
1184
+ type And<A extends boolean, B extends boolean> = AllExtend<[A, B], true>;
846
1185
 
847
1186
  /**
848
- Omit any index signatures from the given object type, leaving only explicitly defined properties.
1187
+ Returns a boolean for whether a given number is greater than another number.
849
1188
 
850
- This is the counterpart of `PickIndexSignature`.
1189
+ @example
1190
+ ```
1191
+ import type {GreaterThan} from 'type-fest';
851
1192
 
852
- Use-cases:
853
- - Remove overly permissive signatures from third-party types.
1193
+ GreaterThan<1, -5>;
1194
+ //=> true
854
1195
 
855
- This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
1196
+ GreaterThan<1, 1>;
1197
+ //=> false
856
1198
 
857
- 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>`.
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
858
1236
 
859
- (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
1237
+ /**
1238
+ Returns a boolean for whether a given number is greater than or equal to another number.
860
1239
 
1240
+ @example
861
1241
  ```
862
- const indexed: Record<string, unknown> = {}; // Allowed
1242
+ import type {GreaterThanOrEqual} from 'type-fest';
863
1243
 
864
- const keyed: Record<'foo', unknown> = {}; // Error
865
- // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
866
- ```
1244
+ GreaterThanOrEqual<1, -5>;
1245
+ //=> true
867
1246
 
868
- 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:
1247
+ GreaterThanOrEqual<1, 1>;
1248
+ //=> true
869
1249
 
1250
+ GreaterThanOrEqual<1, 5>;
1251
+ //=> false
870
1252
  ```
871
- type Indexed = {} extends Record<string, unknown>
872
- ? '✅ `{}` is assignable to `Record<string, unknown>`'
873
- : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
874
- // => '✅ `{}` is assignable to `Record<string, unknown>`'
1253
+ */
1254
+ type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
1255
+ ? never
1256
+ : A extends B ? true : GreaterThan<A, B>;
875
1257
 
876
- type Keyed = {} extends Record<'foo' | 'bar', unknown>
877
- ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
878
- : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
879
- // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
1258
+ /**
1259
+ Returns a boolean for whether a given number is less than another number.
1260
+
1261
+ @example
880
1262
  ```
1263
+ import type {LessThan} from 'type-fest';
881
1264
 
882
- 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`...
1265
+ LessThan<1, -5>;
1266
+ //=> false
883
1267
 
884
- ```
885
- import type {OmitIndexSignature} from 'type-fest';
1268
+ LessThan<1, 1>;
1269
+ //=> false
886
1270
 
887
- type OmitIndexSignature<ObjectType> = {
888
- [KeyType in keyof ObjectType // Map each key of `ObjectType`...
889
- ]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
890
- };
1271
+ LessThan<1, 5>;
1272
+ //=> true
891
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
892
1282
 
893
- ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
1283
+ // Should never happen
894
1284
 
895
- ```
896
- import type {OmitIndexSignature} from 'type-fest';
1285
+ /**
1286
+ Create a tuple type of the given length `<L>` and fill it with the given type `<Fill>`.
897
1287
 
898
- type OmitIndexSignature<ObjectType> = {
899
- [KeyType in keyof ObjectType
900
- // Is `{}` assignable to `Record<KeyType, unknown>`?
901
- as {} extends Record<KeyType, unknown>
902
- ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
903
- : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
904
- ]: ObjectType[KeyType];
905
- };
906
- ```
1288
+ If `<Fill>` is not provided, it will default to `unknown`.
907
1289
 
908
- 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.
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.
909
1307
 
910
1308
  @example
911
1309
  ```
912
- import type {OmitIndexSignature} from 'type-fest';
1310
+ type PositiveInt = StringToNumber<'1234'>;
1311
+ //=> 1234
913
1312
 
914
- interface Example {
915
- // These index signatures will be removed.
916
- [x: string]: any
917
- [x: number]: any
918
- [x: symbol]: any
919
- [x: `head-${string}`]: string
920
- [x: `${string}-tail`]: string
921
- [x: `head-${string}-tail`]: string
922
- [x: `${bigint}`]: string
923
- [x: `embedded-${number}`]: string
1313
+ type NegativeInt = StringToNumber<'-1234'>;
1314
+ //=> -1234
924
1315
 
925
- // These explicitly defined keys will remain.
926
- foo: 'bar';
927
- qux?: 'baz';
928
- }
1316
+ type PositiveFloat = StringToNumber<'1234.56'>;
1317
+ //=> 1234.56
929
1318
 
930
- type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
931
- // => { foo: 'bar'; qux?: 'baz' | undefined; }
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
932
1327
  ```
933
1328
 
934
- @see PickIndexSignature
935
- @category Object
1329
+ @category String
1330
+ @category Numeric
1331
+ @category Template literal
936
1332
  */
937
- type OmitIndexSignature<ObjectType> = {
938
- [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
939
- ? never
940
- : KeyType]: ObjectType[KeyType];
941
- };
942
-
943
- /**
944
- Pick only index signatures from the given object type, leaving out all explicitly defined properties.
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;
945
1340
 
946
- This is the counterpart of `OmitIndexSignature`.
1341
+ /**
1342
+ Returns an array of the characters of the string.
947
1343
 
948
1344
  @example
949
1345
  ```
950
- import type {PickIndexSignature} from 'type-fest';
951
-
952
- declare const symbolKey: unique symbol;
953
-
954
- type Example = {
955
- // These index signatures will remain.
956
- [x: string]: unknown;
957
- [x: number]: unknown;
958
- [x: symbol]: unknown;
959
- [x: `head-${string}`]: string;
960
- [x: `${string}-tail`]: string;
961
- [x: `head-${string}-tail`]: string;
962
- [x: `${bigint}`]: string;
963
- [x: `embedded-${number}`]: string;
964
-
965
- // These explicitly defined keys will be removed.
966
- ['kebab-case-key']: string;
967
- [symbolKey]: string;
968
- foo: 'bar';
969
- qux?: 'baz';
970
- };
1346
+ StringToArray<'abcde'>;
1347
+ //=> ['a', 'b', 'c', 'd', 'e']
971
1348
 
972
- type ExampleIndexSignature = PickIndexSignature<Example>;
973
- // {
974
- // [x: string]: unknown;
975
- // [x: number]: unknown;
976
- // [x: symbol]: unknown;
977
- // [x: `head-${string}`]: string;
978
- // [x: `${string}-tail`]: string;
979
- // [x: `head-${string}-tail`]: string;
980
- // [x: `${bigint}`]: string;
981
- // [x: `embedded-${number}`]: string;
982
- // }
1349
+ StringToArray<string>;
1350
+ //=> never
983
1351
  ```
984
1352
 
985
- @see OmitIndexSignature
986
- @category Object
1353
+ @category String
987
1354
  */
988
- type PickIndexSignature<ObjectType> = {
989
- [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
990
- ? KeyType
991
- : never]: ObjectType[KeyType];
992
- };
993
-
994
- // Merges two objects without worrying about index signatures.
995
- type SimpleMerge<Destination, Source> = {
996
- [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
997
- } & Source;
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;
998
1360
 
999
1361
  /**
1000
- Merge two types into a new type. Keys of the second type overrides keys of the first type.
1362
+ Returns the length of the given string.
1001
1363
 
1002
1364
  @example
1003
1365
  ```
1004
- import type {Merge} from 'type-fest';
1005
-
1006
- interface Foo {
1007
- [x: string]: unknown;
1008
- [x: number]: unknown;
1009
- foo: string;
1010
- bar: symbol;
1011
- }
1012
-
1013
- type Bar = {
1014
- [x: number]: number;
1015
- [x: symbol]: unknown;
1016
- bar: Date;
1017
- baz: boolean;
1018
- };
1366
+ StringLength<'abcde'>;
1367
+ //=> 5
1019
1368
 
1020
- export type FooBar = Merge<Foo, Bar>;
1021
- // => {
1022
- // [x: string]: unknown;
1023
- // [x: number]: number;
1024
- // [x: symbol]: unknown;
1025
- // foo: string;
1026
- // bar: Date;
1027
- // baz: boolean;
1028
- // }
1369
+ StringLength<string>;
1370
+ //=> never
1029
1371
  ```
1030
1372
 
1031
- @category Object
1373
+ @category String
1374
+ @category Template literal
1032
1375
  */
1033
- type Merge<Destination, Source> =
1034
- Simplify<
1035
- SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
1036
- & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
1037
- >;
1376
+ type StringLength<S extends string> = string extends S
1377
+ ? never
1378
+ : StringToArray<S>['length'];
1038
1379
 
1039
1380
  /**
1040
- An if-else-like type that resolves depending on whether the given type is `any`.
1041
-
1042
- @see {@link IsAny}
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.
1043
1382
 
1044
1383
  @example
1045
1384
  ```
1046
- import type {IfAny} from 'type-fest';
1047
-
1048
- type ShouldBeTrue = IfAny<any>;
1385
+ SameLengthPositiveNumericStringGt<'50', '10'>;
1049
1386
  //=> true
1050
1387
 
1051
- type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
1052
- //=> 'bar'
1388
+ SameLengthPositiveNumericStringGt<'10', '10'>;
1389
+ //=> false
1053
1390
  ```
1054
-
1055
- @category Type Guard
1056
- @category Utilities
1057
1391
  */
1058
- type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
1059
- IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
1060
- );
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;
1061
1399
 
1062
- /**
1063
- Matches any primitive, `void`, `Date`, or `RegExp` value.
1064
- */
1065
- type BuiltIns = Primitive | void | Date | RegExp;
1400
+ type NumericString = '0123456789';
1066
1401
 
1067
1402
  /**
1068
- Matches non-recursive types.
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
+ ```
1069
1416
  */
1070
- type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown);
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;
1071
1426
 
1072
1427
  /**
1073
- Merges user specified options with default options.
1428
+ Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters.
1074
1429
 
1075
1430
  @example
1076
1431
  ```
1077
- type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
1078
- type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
1079
- type SpecifiedOptions = {leavesOnly: true};
1432
+ PositiveNumericCharacterGt<'5', '1'>;
1433
+ //=> true
1080
1434
 
1081
- type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
1082
- //=> {maxRecursionDepth: 10; leavesOnly: true}
1435
+ PositiveNumericCharacterGt<'1', '1'>;
1436
+ //=> false
1083
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.
1084
1449
 
1085
1450
  @example
1086
1451
  ```
1087
- // Complains if default values are not provided for optional options
1452
+ NumberAbsolute<-1>;
1453
+ //=> 1
1088
1454
 
1089
- type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
1090
- type DefaultPathsOptions = {maxRecursionDepth: 10};
1091
- type SpecifiedOptions = {};
1455
+ NumberAbsolute<1>;
1456
+ //=> 1
1092
1457
 
1093
- type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
1094
- // ~~~~~~~~~~~~~~~~~~~
1095
- // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
1458
+ NumberAbsolute<NegativeInfinity>
1459
+ //=> PositiveInfinity
1096
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.
1097
1468
 
1098
1469
  @example
1099
1470
  ```
1100
- // Complains if an option's default type does not conform to the expected type
1471
+ type A = IsNumberLike<'1'>;
1472
+ //=> true
1101
1473
 
1102
- type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
1103
- type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
1104
- type SpecifiedOptions = {};
1474
+ type B = IsNumberLike<'-1.1'>;
1475
+ //=> true
1105
1476
 
1106
- type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
1107
- // ~~~~~~~~~~~~~~~~~~~
1108
- // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
1109
- ```
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.
1110
1494
 
1111
1495
  @example
1112
1496
  ```
1113
- // Complains if an option's specified type does not conform to the expected type
1497
+ ReverseSign<-1>;
1498
+ //=> 1
1114
1499
 
1115
- type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
1116
- type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
1117
- type SpecifiedOptions = {leavesOnly: 'yes'};
1500
+ ReverseSign<1>;
1501
+ //=> -1
1118
1502
 
1119
- type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
1120
- // ~~~~~~~~~~~~~~~~
1121
- // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
1503
+ ReverseSign<NegativeInfinity>
1504
+ //=> PositiveInfinity
1505
+
1506
+ ReverseSign<PositiveInfinity>
1507
+ //=> NegativeInfinity
1122
1508
  ```
1123
1509
  */
1124
- type ApplyDefaultOptions<
1125
- Options extends object,
1126
- Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
1127
- SpecifiedOptions extends Options,
1128
- > =
1129
- IfAny<SpecifiedOptions, Defaults,
1130
- IfNever<SpecifiedOptions, Defaults,
1131
- Simplify<Merge<Defaults, {
1132
- [Key in keyof SpecifiedOptions
1133
- as Key extends OptionalKeysOf<Options>
1134
- ? Extract<SpecifiedOptions[Key], undefined> extends never
1135
- ? Key
1136
- : never
1137
- : Key
1138
- ]: SpecifiedOptions[Key]
1139
- }> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
1140
- >>;
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;
1141
1517
 
1142
1518
  /**
1143
1519
  Returns the difference between two numbers.
@@ -1226,9 +1602,9 @@ Paths options.
1226
1602
  */
1227
1603
  type PathsOptions = {
1228
1604
  /**
1229
- The maximum depth to recurse when searching for paths.
1605
+ The maximum depth to recurse when searching for paths. Range: 0 ~ 10.
1230
1606
 
1231
- @default 10
1607
+ @default 5
1232
1608
  */
1233
1609
  maxRecursionDepth?: number;
1234
1610
 
@@ -1343,7 +1719,7 @@ type PathsOptions = {
1343
1719
  };
1344
1720
 
1345
1721
  type DefaultPathsOptions = {
1346
- maxRecursionDepth: 10;
1722
+ maxRecursionDepth: 5;
1347
1723
  bracketNotation: false;
1348
1724
  leavesOnly: false;
1349
1725
  depth: number;
@@ -1400,8 +1776,7 @@ type _Paths<T, Options extends Required<PathsOptions>> =
1400
1776
  : T extends UnknownArray
1401
1777
  ? number extends T['length']
1402
1778
  // We need to handle the fixed and non-fixed index part of the array separately.
1403
- ? InternalPaths<StaticPartOfArray<T>, Options>
1404
- | InternalPaths<Array<VariablePartOfArray<T>[number]>, Options>
1779
+ ? InternalPaths<StaticPartOfArray<T>, Options> | InternalPaths<Array<VariablePartOfArray<T>[number]>, Options>
1405
1780
  : InternalPaths<T, Options>
1406
1781
  : T extends object
1407
1782
  ? InternalPaths<T, Options>
@@ -1410,30 +1785,34 @@ type _Paths<T, Options extends Required<PathsOptions>> =
1410
1785
  type InternalPaths<T, Options extends Required<PathsOptions>> =
1411
1786
  Options['maxRecursionDepth'] extends infer MaxDepth extends number
1412
1787
  ? Required<T> extends infer T
1413
- ? T extends EmptyObject | readonly []
1788
+ ? T extends readonly []
1414
1789
  ? never
1415
- : {
1416
- [Key in keyof T]:
1417
- Key extends string | number // Limit `Key` to string or number.
1418
- ? (
1419
- Options['bracketNotation'] extends true
1420
- ? IsNumberLike<Key> extends true
1421
- ? `[${Key}]`
1422
- : (Key | ToString<Key>)
1423
- : never
1424
- |
1425
- Options['bracketNotation'] extends false
1426
- // If `Key` is a number, return `Key | `${Key}``, because both `array[0]` and `array['0']` work.
1427
- ? (Key | ToString<Key>)
1428
- : never
1429
- ) extends infer TranformedKey extends string | number ?
1430
- // 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
1431
- // 2. If style is 'a.0.b', transform 'Key' to `${Key}` | Key
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
1432
1807
  | ((Options['leavesOnly'] extends true
1433
1808
  ? MaxDepth extends 0
1434
1809
  ? TranformedKey
1435
- : T[Key] extends EmptyObject | readonly [] | NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
1436
- ? 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)
1437
1816
  : never
1438
1817
  : TranformedKey
1439
1818
  ) extends infer _TransformedKey
@@ -1447,12 +1826,12 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
1447
1826
  // Recursively generate paths for the current key
1448
1827
  GreaterThan<MaxDepth, 0> extends true // Limit the depth to prevent infinite recursion
1449
1828
  ? _Paths<T[Key],
1450
- {
1451
- bracketNotation: Options['bracketNotation'];
1452
- maxRecursionDepth: Subtract<MaxDepth, 1>;
1453
- leavesOnly: Options['leavesOnly'];
1454
- depth: Subtract<Options['depth'], 1>;
1455
- }> extends infer SubPath
1829
+ {
1830
+ bracketNotation: Options['bracketNotation'];
1831
+ maxRecursionDepth: Subtract<MaxDepth, 1>;
1832
+ leavesOnly: Options['leavesOnly'];
1833
+ depth: Subtract<Options['depth'], 1>;
1834
+ }> extends infer SubPath
1456
1835
  ? SubPath extends string | number
1457
1836
  ? (
1458
1837
  Options['bracketNotation'] extends true
@@ -1469,9 +1848,9 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
1469
1848
  : never
1470
1849
  : never
1471
1850
  )
1851
+ : never
1472
1852
  : never
1473
- : never
1474
- }[keyof T & (T extends UnknownArray ? number : unknown)]
1853
+ }[keyof T & (T extends UnknownArray ? number : unknown)]
1475
1854
  : never
1476
1855
  : never;
1477
1856
 
@@ -1719,7 +2098,7 @@ declare namespace PackageJson$1 {
1719
2098
  /**
1720
2099
  A mapping of conditions and the paths to which they resolve.
1721
2100
  */
1722
- type ExportConditions = { // eslint-disable-line @typescript-eslint/consistent-indexed-object-style
2101
+ type ExportConditions = {
1723
2102
  [condition: string]: Exports;
1724
2103
  };
1725
2104
 
@@ -1727,15 +2106,15 @@ declare namespace PackageJson$1 {
1727
2106
  Entry points of a module, optionally with conditions and subpath exports.
1728
2107
  */
1729
2108
  export type Exports =
1730
- | null
1731
- | string
1732
- | Array<string | ExportConditions>
1733
- | ExportConditions;
2109
+ | null
2110
+ | string
2111
+ | Array<string | ExportConditions>
2112
+ | ExportConditions;
1734
2113
 
1735
2114
  /**
1736
2115
  Import map entries of a module, optionally with conditions and subpath imports.
1737
2116
  */
1738
- export type Imports = { // eslint-disable-line @typescript-eslint/consistent-indexed-object-style
2117
+ export type Imports = {
1739
2118
  [key: `#${string}`]: Exports;
1740
2119
  };
1741
2120
 
@@ -1750,19 +2129,19 @@ declare namespace PackageJson$1 {
1750
2129
  A module ID with untranspiled code that is the primary entry point to the program.
1751
2130
  */
1752
2131
  esnext?:
1753
- | string
1754
- | {
1755
- [moduleName: string]: string | undefined;
1756
- main?: string;
1757
- browser?: string;
1758
- };
2132
+ | string
2133
+ | {
2134
+ [moduleName: string]: string | undefined;
2135
+ main?: string;
2136
+ browser?: string;
2137
+ };
1759
2138
 
1760
2139
  /**
1761
2140
  A hint to JavaScript bundlers or component tools when packaging modules for client side use.
1762
2141
  */
1763
2142
  browser?:
1764
- | string
1765
- | Partial<Record<string, string | false>>;
2143
+ | string
2144
+ | Partial<Record<string, string | false>>;
1766
2145
 
1767
2146
  /**
1768
2147
  Denote which files in your project are "pure" and therefore safe for Webpack to prune if unused.
@@ -1934,8 +2313,8 @@ declare namespace PackageJson$1 {
1934
2313
  The executable files that should be installed into the `PATH`.
1935
2314
  */
1936
2315
  bin?:
1937
- | string
1938
- | Partial<Record<string, string>>;
2316
+ | string
2317
+ | Partial<Record<string, string>>;
1939
2318
 
1940
2319
  /**
1941
2320
  Filenames to put in place for the `man` program to find.
@@ -1951,18 +2330,18 @@ declare namespace PackageJson$1 {
1951
2330
  Location for the code repository.
1952
2331
  */
1953
2332
  repository?:
1954
- | string
1955
- | {
1956
- type: string;
1957
- url: string;
2333
+ | string
2334
+ | {
2335
+ type: string;
2336
+ url: string;
1958
2337
 
1959
- /**
2338
+ /**
1960
2339
  Relative path to package.json if it is placed in non-root directory (for example if it is part of a monorepo).
1961
2340
 
1962
2341
  [Read more.](https://github.com/npm/rfcs/blob/latest/implemented/0010-monorepo-subdirectory-declaration.md)
1963
2342
  */
1964
- directory?: string;
1965
- };
2343
+ directory?: string;
2344
+ };
1966
2345
 
1967
2346
  /**
1968
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.
@@ -2025,50 +2404,50 @@ declare namespace PackageJson$1 {
2025
2404
  Operating systems the module runs on.
2026
2405
  */
2027
2406
  os?: Array<LiteralUnion<
2028
- | 'aix'
2029
- | 'darwin'
2030
- | 'freebsd'
2031
- | 'linux'
2032
- | 'openbsd'
2033
- | 'sunos'
2034
- | 'win32'
2035
- | '!aix'
2036
- | '!darwin'
2037
- | '!freebsd'
2038
- | '!linux'
2039
- | '!openbsd'
2040
- | '!sunos'
2041
- | '!win32',
2042
- string
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
2043
2422
  >>;
2044
2423
 
2045
2424
  /**
2046
2425
  CPU architectures the module runs on.
2047
2426
  */
2048
2427
  cpu?: Array<LiteralUnion<
2049
- | 'arm'
2050
- | 'arm64'
2051
- | 'ia32'
2052
- | 'mips'
2053
- | 'mipsel'
2054
- | 'ppc'
2055
- | 'ppc64'
2056
- | 's390'
2057
- | 's390x'
2058
- | 'x32'
2059
- | 'x64'
2060
- | '!arm'
2061
- | '!arm64'
2062
- | '!ia32'
2063
- | '!mips'
2064
- | '!mipsel'
2065
- | '!ppc'
2066
- | '!ppc64'
2067
- | '!s390'
2068
- | '!s390x'
2069
- | '!x32'
2070
- | '!x64',
2071
- string
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
2072
2451
  >>;
2073
2452
 
2074
2453
  /**
@@ -2091,20 +2470,20 @@ declare namespace PackageJson$1 {
2091
2470
  /**
2092
2471
  Describes and notifies consumers of a package's monetary support information.
2093
2472
 
2094
- [Read more.](https://github.com/npm/rfcs/blob/latest/accepted/0017-add-funding-support.md)
2473
+ [Read more.](https://github.com/npm/rfcs/blob/main/implemented/0017-add-funding-support.md)
2095
2474
  */
2096
2475
  funding?: string | {
2097
2476
  /**
2098
2477
  The type of funding.
2099
2478
  */
2100
2479
  type?: LiteralUnion<
2101
- | 'github'
2102
- | 'opencollective'
2103
- | 'patreon'
2104
- | 'individual'
2105
- | 'foundation'
2106
- | 'corporation',
2107
- string
2480
+ | 'github'
2481
+ | 'opencollective'
2482
+ | 'patreon'
2483
+ | 'individual'
2484
+ | 'foundation'
2485
+ | 'corporation',
2486
+ string
2108
2487
  >;
2109
2488
 
2110
2489
  /**
@@ -2175,13 +2554,13 @@ Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-j
2175
2554
  @category File
2176
2555
  */
2177
2556
  type PackageJson$1 =
2178
- JsonObject &
2179
- PackageJson$1.NodeJsStandard &
2180
- PackageJson$1.PackageJsonStandard &
2181
- PackageJson$1.NonStandardEntryPoints &
2182
- PackageJson$1.TypeScriptConfiguration &
2183
- PackageJson$1.YarnConfiguration &
2184
- PackageJson$1.JSPMConfiguration;
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;
2185
2564
 
2186
2565
  interface InstallPackageOptions {
2187
2566
  cwd?: string;
@@ -2366,7 +2745,11 @@ type EnsurePackagesOptions = {
2366
2745
  deps?: boolean;
2367
2746
  devDeps?: boolean;
2368
2747
  installPackage?: Omit<InstallPackageOptions, "cwd" | "dev">;
2748
+ logger?: {
2749
+ warn: (message: string) => void;
2750
+ };
2369
2751
  peerDeps?: boolean;
2752
+ throwOnWarn?: boolean;
2370
2753
  };
2371
2754
 
2372
2755
  type ReadOptions = {