@visulima/package 3.5.4 → 3.5.5
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 +23 -0
- package/LICENSE.md +116 -0
- package/dist/index.d.cts +1 -2
- package/dist/index.d.mts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/package-json.cjs +831 -11
- package/dist/package-json.d.cts +1 -2
- package/dist/package-json.d.mts +1 -2
- package/dist/package-json.d.ts +1 -2
- package/dist/package-json.mjs +816 -1
- package/dist/packem_shared/{package-json-DvaC9k-3.d.cts → package-json-BCAHuiQ6.d.cts} +545 -58
- package/dist/packem_shared/{package-json-DvaC9k-3.d.mts → package-json-BCAHuiQ6.d.mts} +545 -58
- package/dist/packem_shared/{package-json-DvaC9k-3.d.ts → package-json-BCAHuiQ6.d.ts} +545 -58
- package/package.json +4 -5
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { WriteJsonOptions } from '@visulima/fs';
|
|
2
|
-
import { InstallPackageOptions } from '@antfu/install-pkg';
|
|
3
2
|
import { Package } from 'normalize-package-data';
|
|
4
3
|
|
|
5
4
|
/**
|
|
@@ -86,36 +85,139 @@ Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<n
|
|
|
86
85
|
type EmptyObject = {[emptyObjectSymbol]?: never};
|
|
87
86
|
|
|
88
87
|
/**
|
|
89
|
-
|
|
88
|
+
Extract all optional keys from the given type.
|
|
90
89
|
|
|
91
|
-
|
|
92
|
-
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
90
|
+
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
93
91
|
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
@example
|
|
93
|
+
```
|
|
94
|
+
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
95
|
+
|
|
96
|
+
interface User {
|
|
97
|
+
name: string;
|
|
98
|
+
surname: string;
|
|
99
|
+
|
|
100
|
+
luckyNumber?: number;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
104
|
+
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
105
|
+
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const update1: UpdateOperation<User> = {
|
|
109
|
+
name: 'Alice'
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const update2: UpdateOperation<User> = {
|
|
113
|
+
name: 'Bob',
|
|
114
|
+
luckyNumber: REMOVE_FIELD
|
|
115
|
+
};
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
@category Utilities
|
|
119
|
+
*/
|
|
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`
|
|
125
|
+
: never; // Should never happen
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
Extract all required keys from the given type.
|
|
129
|
+
|
|
130
|
+
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...
|
|
96
131
|
|
|
97
132
|
@example
|
|
98
133
|
```
|
|
99
|
-
import type {
|
|
134
|
+
import type {RequiredKeysOf} from 'type-fest';
|
|
100
135
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
136
|
+
declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
|
|
137
|
+
|
|
138
|
+
interface User {
|
|
139
|
+
name: string;
|
|
140
|
+
surname: string;
|
|
141
|
+
|
|
142
|
+
luckyNumber?: number;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
146
|
+
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
@category Utilities
|
|
150
|
+
*/
|
|
151
|
+
type RequiredKeysOf<BaseType extends object> =
|
|
152
|
+
BaseType extends unknown // For distributing `BaseType`
|
|
153
|
+
? Exclude<keyof BaseType, OptionalKeysOf<BaseType>>
|
|
154
|
+
: never; // Should never happen
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
Returns a boolean for whether the given type is `never`.
|
|
158
|
+
|
|
159
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
160
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
161
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
162
|
+
|
|
163
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
164
|
+
|
|
165
|
+
@example
|
|
166
|
+
```
|
|
167
|
+
import type {IsNever, And} from 'type-fest';
|
|
168
|
+
|
|
169
|
+
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
170
|
+
type AreStringsEqual<A extends string, B extends string> =
|
|
171
|
+
And<
|
|
172
|
+
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
173
|
+
IsNever<Exclude<B, A>> extends true ? true : false
|
|
174
|
+
>;
|
|
175
|
+
|
|
176
|
+
type EndIfEqual<I extends string, O extends string> =
|
|
177
|
+
AreStringsEqual<I, O> extends true
|
|
178
|
+
? never
|
|
179
|
+
: void;
|
|
180
|
+
|
|
181
|
+
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
182
|
+
if (input === output) {
|
|
183
|
+
process.exit(0);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
endIfEqual('abc', 'abc');
|
|
188
|
+
//=> never
|
|
189
|
+
|
|
190
|
+
endIfEqual('abc', '123');
|
|
191
|
+
//=> void
|
|
109
192
|
```
|
|
110
193
|
|
|
111
194
|
@category Type Guard
|
|
112
195
|
@category Utilities
|
|
113
196
|
*/
|
|
114
|
-
type
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
197
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
An if-else-like type that resolves depending on whether the given type is `never`.
|
|
201
|
+
|
|
202
|
+
@see {@link IsNever}
|
|
203
|
+
|
|
204
|
+
@example
|
|
205
|
+
```
|
|
206
|
+
import type {IfNever} from 'type-fest';
|
|
207
|
+
|
|
208
|
+
type ShouldBeTrue = IfNever<never>;
|
|
209
|
+
//=> true
|
|
210
|
+
|
|
211
|
+
type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
|
|
212
|
+
//=> 'bar'
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
@category Type Guard
|
|
216
|
+
@category Utilities
|
|
217
|
+
*/
|
|
218
|
+
type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
|
|
219
|
+
IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
|
|
220
|
+
);
|
|
119
221
|
|
|
120
222
|
/**
|
|
121
223
|
Represents an array with `unknown` value.
|
|
@@ -272,6 +374,38 @@ type ShouldBeTrue = IsNegative<-1>;
|
|
|
272
374
|
*/
|
|
273
375
|
type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false;
|
|
274
376
|
|
|
377
|
+
/**
|
|
378
|
+
Returns a boolean for whether the two given types are equal.
|
|
379
|
+
|
|
380
|
+
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
381
|
+
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
382
|
+
|
|
383
|
+
Use-cases:
|
|
384
|
+
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
385
|
+
|
|
386
|
+
@example
|
|
387
|
+
```
|
|
388
|
+
import type {IsEqual} from 'type-fest';
|
|
389
|
+
|
|
390
|
+
// This type returns a boolean for whether the given array includes the given item.
|
|
391
|
+
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
392
|
+
type Includes<Value extends readonly any[], Item> =
|
|
393
|
+
Value extends readonly [Value[0], ...infer rest]
|
|
394
|
+
? IsEqual<Value[0], Item> extends true
|
|
395
|
+
? true
|
|
396
|
+
: Includes<rest, Item>
|
|
397
|
+
: false;
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
@category Type Guard
|
|
401
|
+
@category Utilities
|
|
402
|
+
*/
|
|
403
|
+
type IsEqual<A, B> =
|
|
404
|
+
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
405
|
+
(<G>() => G extends B & G | G ? 1 : 2)
|
|
406
|
+
? true
|
|
407
|
+
: false;
|
|
408
|
+
|
|
275
409
|
/**
|
|
276
410
|
Returns a boolean for whether two given types are both true.
|
|
277
411
|
|
|
@@ -337,34 +471,39 @@ GreaterThan<1, 5>;
|
|
|
337
471
|
//=> false
|
|
338
472
|
```
|
|
339
473
|
*/
|
|
340
|
-
type GreaterThan<A extends number, B extends number> =
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
: [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean]
|
|
359
|
-
? [true, false] extends R
|
|
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
|
|
360
492
|
? false
|
|
361
|
-
:
|
|
362
|
-
?
|
|
363
|
-
: [
|
|
364
|
-
?
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
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
|
|
368
507
|
|
|
369
508
|
/**
|
|
370
509
|
Returns a boolean for whether a given number is greater than or equal to another number.
|
|
@@ -406,7 +545,11 @@ LessThan<1, 5>;
|
|
|
406
545
|
*/
|
|
407
546
|
type LessThan<A extends number, B extends number> = number extends A | B
|
|
408
547
|
? never
|
|
409
|
-
: GreaterThanOrEqual<A, B> extends
|
|
548
|
+
: GreaterThanOrEqual<A, B> extends infer Result
|
|
549
|
+
? Result extends true
|
|
550
|
+
? false
|
|
551
|
+
: true
|
|
552
|
+
: never; // Should never happen
|
|
410
553
|
|
|
411
554
|
// Should never happen
|
|
412
555
|
|
|
@@ -642,6 +785,280 @@ type ReverseSign<N extends number> =
|
|
|
642
785
|
// Handle positive numbers
|
|
643
786
|
: `-${N}` extends `${infer R extends number}` ? R : never;
|
|
644
787
|
|
|
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.
|
|
790
|
+
|
|
791
|
+
@example
|
|
792
|
+
```
|
|
793
|
+
import type {Simplify} from 'type-fest';
|
|
794
|
+
|
|
795
|
+
type PositionProps = {
|
|
796
|
+
top: number;
|
|
797
|
+
left: number;
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
type SizeProps = {
|
|
801
|
+
width: number;
|
|
802
|
+
height: number;
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
806
|
+
type Props = Simplify<PositionProps & SizeProps>;
|
|
807
|
+
```
|
|
808
|
+
|
|
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.
|
|
810
|
+
|
|
811
|
+
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
812
|
+
|
|
813
|
+
@example
|
|
814
|
+
```
|
|
815
|
+
import type {Simplify} from 'type-fest';
|
|
816
|
+
|
|
817
|
+
interface SomeInterface {
|
|
818
|
+
foo: number;
|
|
819
|
+
bar?: string;
|
|
820
|
+
baz: number | undefined;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
type SomeType = {
|
|
824
|
+
foo: number;
|
|
825
|
+
bar?: string;
|
|
826
|
+
baz: number | undefined;
|
|
827
|
+
};
|
|
828
|
+
|
|
829
|
+
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
830
|
+
const someType: SomeType = literal;
|
|
831
|
+
const someInterface: SomeInterface = literal;
|
|
832
|
+
|
|
833
|
+
function fn(object: Record<string, unknown>): void {}
|
|
834
|
+
|
|
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`
|
|
839
|
+
```
|
|
840
|
+
|
|
841
|
+
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
842
|
+
@see SimplifyDeep
|
|
843
|
+
@category Object
|
|
844
|
+
*/
|
|
845
|
+
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
849
|
+
|
|
850
|
+
This is the counterpart of `PickIndexSignature`.
|
|
851
|
+
|
|
852
|
+
Use-cases:
|
|
853
|
+
- Remove overly permissive signatures from third-party types.
|
|
854
|
+
|
|
855
|
+
This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
|
|
856
|
+
|
|
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>`.
|
|
858
|
+
|
|
859
|
+
(The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
|
|
860
|
+
|
|
861
|
+
```
|
|
862
|
+
const indexed: Record<string, unknown> = {}; // Allowed
|
|
863
|
+
|
|
864
|
+
const keyed: Record<'foo', unknown> = {}; // Error
|
|
865
|
+
// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
866
|
+
```
|
|
867
|
+
|
|
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:
|
|
869
|
+
|
|
870
|
+
```
|
|
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>`'
|
|
875
|
+
|
|
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>`"
|
|
880
|
+
```
|
|
881
|
+
|
|
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`...
|
|
883
|
+
|
|
884
|
+
```
|
|
885
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
886
|
+
|
|
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
|
+
};
|
|
891
|
+
```
|
|
892
|
+
|
|
893
|
+
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
894
|
+
|
|
895
|
+
```
|
|
896
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
897
|
+
|
|
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
|
+
```
|
|
907
|
+
|
|
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.
|
|
909
|
+
|
|
910
|
+
@example
|
|
911
|
+
```
|
|
912
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
913
|
+
|
|
914
|
+
interface Example {
|
|
915
|
+
// These index signatures will be removed.
|
|
916
|
+
[x: string]: any
|
|
917
|
+
[x: number]: any
|
|
918
|
+
[x: symbol]: any
|
|
919
|
+
[x: `head-${string}`]: string
|
|
920
|
+
[x: `${string}-tail`]: string
|
|
921
|
+
[x: `head-${string}-tail`]: string
|
|
922
|
+
[x: `${bigint}`]: string
|
|
923
|
+
[x: `embedded-${number}`]: string
|
|
924
|
+
|
|
925
|
+
// These explicitly defined keys will remain.
|
|
926
|
+
foo: 'bar';
|
|
927
|
+
qux?: 'baz';
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
931
|
+
// => { foo: 'bar'; qux?: 'baz' | undefined; }
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
@see PickIndexSignature
|
|
935
|
+
@category Object
|
|
936
|
+
*/
|
|
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.
|
|
945
|
+
|
|
946
|
+
This is the counterpart of `OmitIndexSignature`.
|
|
947
|
+
|
|
948
|
+
@example
|
|
949
|
+
```
|
|
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
|
+
};
|
|
971
|
+
|
|
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
|
+
// }
|
|
983
|
+
```
|
|
984
|
+
|
|
985
|
+
@see OmitIndexSignature
|
|
986
|
+
@category Object
|
|
987
|
+
*/
|
|
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;
|
|
998
|
+
|
|
999
|
+
/**
|
|
1000
|
+
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
1001
|
+
|
|
1002
|
+
@example
|
|
1003
|
+
```
|
|
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
|
+
};
|
|
1019
|
+
|
|
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
|
+
// }
|
|
1029
|
+
```
|
|
1030
|
+
|
|
1031
|
+
@category Object
|
|
1032
|
+
*/
|
|
1033
|
+
type Merge<Destination, Source> =
|
|
1034
|
+
Simplify<
|
|
1035
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
1036
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
1037
|
+
>;
|
|
1038
|
+
|
|
1039
|
+
/**
|
|
1040
|
+
An if-else-like type that resolves depending on whether the given type is `any`.
|
|
1041
|
+
|
|
1042
|
+
@see {@link IsAny}
|
|
1043
|
+
|
|
1044
|
+
@example
|
|
1045
|
+
```
|
|
1046
|
+
import type {IfAny} from 'type-fest';
|
|
1047
|
+
|
|
1048
|
+
type ShouldBeTrue = IfAny<any>;
|
|
1049
|
+
//=> true
|
|
1050
|
+
|
|
1051
|
+
type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
|
|
1052
|
+
//=> 'bar'
|
|
1053
|
+
```
|
|
1054
|
+
|
|
1055
|
+
@category Type Guard
|
|
1056
|
+
@category Utilities
|
|
1057
|
+
*/
|
|
1058
|
+
type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
|
|
1059
|
+
IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
|
|
1060
|
+
);
|
|
1061
|
+
|
|
645
1062
|
/**
|
|
646
1063
|
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
647
1064
|
*/
|
|
@@ -652,6 +1069,76 @@ Matches non-recursive types.
|
|
|
652
1069
|
*/
|
|
653
1070
|
type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown);
|
|
654
1071
|
|
|
1072
|
+
/**
|
|
1073
|
+
Merges user specified options with default options.
|
|
1074
|
+
|
|
1075
|
+
@example
|
|
1076
|
+
```
|
|
1077
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
1078
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
1079
|
+
type SpecifiedOptions = {leavesOnly: true};
|
|
1080
|
+
|
|
1081
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
1082
|
+
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
1083
|
+
```
|
|
1084
|
+
|
|
1085
|
+
@example
|
|
1086
|
+
```
|
|
1087
|
+
// Complains if default values are not provided for optional options
|
|
1088
|
+
|
|
1089
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
1090
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10};
|
|
1091
|
+
type SpecifiedOptions = {};
|
|
1092
|
+
|
|
1093
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
1094
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
1095
|
+
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
1096
|
+
```
|
|
1097
|
+
|
|
1098
|
+
@example
|
|
1099
|
+
```
|
|
1100
|
+
// Complains if an option's default type does not conform to the expected type
|
|
1101
|
+
|
|
1102
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
1103
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
1104
|
+
type SpecifiedOptions = {};
|
|
1105
|
+
|
|
1106
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
1107
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
1108
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
1109
|
+
```
|
|
1110
|
+
|
|
1111
|
+
@example
|
|
1112
|
+
```
|
|
1113
|
+
// Complains if an option's specified type does not conform to the expected type
|
|
1114
|
+
|
|
1115
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
1116
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
1117
|
+
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
1118
|
+
|
|
1119
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
1120
|
+
// ~~~~~~~~~~~~~~~~
|
|
1121
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
1122
|
+
```
|
|
1123
|
+
*/
|
|
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
|
+
>>;
|
|
1141
|
+
|
|
655
1142
|
/**
|
|
656
1143
|
Returns the difference between two numbers.
|
|
657
1144
|
|
|
@@ -903,16 +1390,7 @@ open('listB.1'); // TypeError. Because listB only has one element.
|
|
|
903
1390
|
@category Object
|
|
904
1391
|
@category Array
|
|
905
1392
|
*/
|
|
906
|
-
type Paths<T, Options extends PathsOptions = {}> = _Paths<T,
|
|
907
|
-
// Set default maxRecursionDepth to 10
|
|
908
|
-
maxRecursionDepth: Options['maxRecursionDepth'] extends number ? Options['maxRecursionDepth'] : DefaultPathsOptions['maxRecursionDepth'];
|
|
909
|
-
// Set default bracketNotation to false
|
|
910
|
-
bracketNotation: Options['bracketNotation'] extends boolean ? Options['bracketNotation'] : DefaultPathsOptions['bracketNotation'];
|
|
911
|
-
// Set default leavesOnly to false
|
|
912
|
-
leavesOnly: Options['leavesOnly'] extends boolean ? Options['leavesOnly'] : DefaultPathsOptions['leavesOnly'];
|
|
913
|
-
// Set default depth to number
|
|
914
|
-
depth: Options['depth'] extends number ? Options['depth'] : DefaultPathsOptions['depth'];
|
|
915
|
-
}>;
|
|
1393
|
+
type Paths<T, Options extends PathsOptions = {}> = _Paths<T, ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, Options>>;
|
|
916
1394
|
|
|
917
1395
|
type _Paths<T, Options extends Required<PathsOptions>> =
|
|
918
1396
|
T extends NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
|
|
@@ -1705,6 +2183,15 @@ PackageJson$1.TypeScriptConfiguration &
|
|
|
1705
2183
|
PackageJson$1.YarnConfiguration &
|
|
1706
2184
|
PackageJson$1.JSPMConfiguration;
|
|
1707
2185
|
|
|
2186
|
+
interface InstallPackageOptions {
|
|
2187
|
+
cwd?: string;
|
|
2188
|
+
dev?: boolean;
|
|
2189
|
+
silent?: boolean;
|
|
2190
|
+
packageManager?: string;
|
|
2191
|
+
preferOffline?: boolean;
|
|
2192
|
+
additionalArgs?: string[] | ((agent: string, detectedAgent: string) => string[] | undefined);
|
|
2193
|
+
}
|
|
2194
|
+
|
|
1708
2195
|
type Prettify<T> = {
|
|
1709
2196
|
[K in keyof T]: T[K];
|
|
1710
2197
|
} & {};
|