@visulima/tsconfig 1.1.16 → 1.1.17

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/dist/index.d.cts CHANGED
@@ -7,6 +7,175 @@ declare global {
7
7
  }
8
8
  }
9
9
 
10
+ /**
11
+ Extract all optional keys from the given type.
12
+
13
+ This is useful when you want to create a new type that contains different type values for the optional keys only.
14
+
15
+ @example
16
+ ```
17
+ import type {OptionalKeysOf, Except} from 'type-fest';
18
+
19
+ interface User {
20
+ name: string;
21
+ surname: string;
22
+
23
+ luckyNumber?: number;
24
+ }
25
+
26
+ const REMOVE_FIELD = Symbol('remove field symbol');
27
+ type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
28
+ [Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
29
+ };
30
+
31
+ const update1: UpdateOperation<User> = {
32
+ name: 'Alice'
33
+ };
34
+
35
+ const update2: UpdateOperation<User> = {
36
+ name: 'Bob',
37
+ luckyNumber: REMOVE_FIELD
38
+ };
39
+ ```
40
+
41
+ @category Utilities
42
+ */
43
+ type OptionalKeysOf<BaseType extends object> =
44
+ BaseType extends unknown // For distributing `BaseType`
45
+ ? (keyof {
46
+ [Key in keyof BaseType as BaseType extends Record<Key, BaseType[Key]> ? never : Key]: never
47
+ }) & (keyof BaseType) // Intersect with `keyof BaseType` to ensure result of `OptionalKeysOf<BaseType>` is always assignable to `keyof BaseType`
48
+ : never; // Should never happen
49
+
50
+ /**
51
+ Extract all required keys from the given type.
52
+
53
+ 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...
54
+
55
+ @example
56
+ ```
57
+ import type {RequiredKeysOf} from 'type-fest';
58
+
59
+ declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
60
+
61
+ interface User {
62
+ name: string;
63
+ surname: string;
64
+
65
+ luckyNumber?: number;
66
+ }
67
+
68
+ const validator1 = createValidation<User>('name', value => value.length < 25);
69
+ const validator2 = createValidation<User>('surname', value => value.length < 25);
70
+ ```
71
+
72
+ @category Utilities
73
+ */
74
+ type RequiredKeysOf<BaseType extends object> =
75
+ BaseType extends unknown // For distributing `BaseType`
76
+ ? Exclude<keyof BaseType, OptionalKeysOf<BaseType>>
77
+ : never; // Should never happen
78
+
79
+ /**
80
+ Returns a boolean for whether the given type is `never`.
81
+
82
+ @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
83
+ @link https://stackoverflow.com/a/53984913/10292952
84
+ @link https://www.zhenghao.io/posts/ts-never
85
+
86
+ Useful in type utilities, such as checking if something does not occur.
87
+
88
+ @example
89
+ ```
90
+ import type {IsNever, And} from 'type-fest';
91
+
92
+ // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
93
+ type AreStringsEqual<A extends string, B extends string> =
94
+ And<
95
+ IsNever<Exclude<A, B>> extends true ? true : false,
96
+ IsNever<Exclude<B, A>> extends true ? true : false
97
+ >;
98
+
99
+ type EndIfEqual<I extends string, O extends string> =
100
+ AreStringsEqual<I, O> extends true
101
+ ? never
102
+ : void;
103
+
104
+ function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
105
+ if (input === output) {
106
+ process.exit(0);
107
+ }
108
+ }
109
+
110
+ endIfEqual('abc', 'abc');
111
+ //=> never
112
+
113
+ endIfEqual('abc', '123');
114
+ //=> void
115
+ ```
116
+
117
+ @category Type Guard
118
+ @category Utilities
119
+ */
120
+ type IsNever<T> = [T] extends [never] ? true : false;
121
+
122
+ /**
123
+ An if-else-like type that resolves depending on whether the given type is `never`.
124
+
125
+ @see {@link IsNever}
126
+
127
+ @example
128
+ ```
129
+ import type {IfNever} from 'type-fest';
130
+
131
+ type ShouldBeTrue = IfNever<never>;
132
+ //=> true
133
+
134
+ type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
135
+ //=> 'bar'
136
+ ```
137
+
138
+ @category Type Guard
139
+ @category Utilities
140
+ */
141
+ type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
142
+ IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
143
+ );
144
+
145
+ // Can eventually be replaced with the built-in once this library supports
146
+ // TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
147
+ type NoInfer<T> = T extends infer U ? U : never;
148
+
149
+ /**
150
+ Returns a boolean for whether the given type is `any`.
151
+
152
+ @link https://stackoverflow.com/a/49928360/1490091
153
+
154
+ Useful in type utilities, such as disallowing `any`s to be passed to a function.
155
+
156
+ @example
157
+ ```
158
+ import type {IsAny} from 'type-fest';
159
+
160
+ const typedObject = {a: 1, b: 2} as const;
161
+ const anyObject: any = {a: 1, b: 2};
162
+
163
+ function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
164
+ return obj[key];
165
+ }
166
+
167
+ const typedA = get(typedObject, 'a');
168
+ //=> 1
169
+
170
+ const anyA = get(anyObject, 'a');
171
+ //=> any
172
+ ```
173
+
174
+ @category Type Guard
175
+ @category Utilities
176
+ */
177
+ type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
178
+
10
179
  /**
11
180
  Returns a boolean for whether the two given types are equal.
12
181
 
@@ -39,6 +208,350 @@ type IsEqual<A, B> =
39
208
  ? true
40
209
  : false;
41
210
 
211
+ /**
212
+ 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.
213
+
214
+ @example
215
+ ```
216
+ import type {Simplify} from 'type-fest';
217
+
218
+ type PositionProps = {
219
+ top: number;
220
+ left: number;
221
+ };
222
+
223
+ type SizeProps = {
224
+ width: number;
225
+ height: number;
226
+ };
227
+
228
+ // In your editor, hovering over `Props` will show a flattened object with all the properties.
229
+ type Props = Simplify<PositionProps & SizeProps>;
230
+ ```
231
+
232
+ 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.
233
+
234
+ 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`.
235
+
236
+ @example
237
+ ```
238
+ import type {Simplify} from 'type-fest';
239
+
240
+ interface SomeInterface {
241
+ foo: number;
242
+ bar?: string;
243
+ baz: number | undefined;
244
+ }
245
+
246
+ type SomeType = {
247
+ foo: number;
248
+ bar?: string;
249
+ baz: number | undefined;
250
+ };
251
+
252
+ const literal = {foo: 123, bar: 'hello', baz: 456};
253
+ const someType: SomeType = literal;
254
+ const someInterface: SomeInterface = literal;
255
+
256
+ function fn(object: Record<string, unknown>): void {}
257
+
258
+ fn(literal); // Good: literal object type is sealed
259
+ fn(someType); // Good: type is sealed
260
+ fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
261
+ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
262
+ ```
263
+
264
+ @link https://github.com/microsoft/TypeScript/issues/15300
265
+ @see SimplifyDeep
266
+ @category Object
267
+ */
268
+ type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
269
+
270
+ /**
271
+ Omit any index signatures from the given object type, leaving only explicitly defined properties.
272
+
273
+ This is the counterpart of `PickIndexSignature`.
274
+
275
+ Use-cases:
276
+ - Remove overly permissive signatures from third-party types.
277
+
278
+ This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
279
+
280
+ 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>`.
281
+
282
+ (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
283
+
284
+ ```
285
+ const indexed: Record<string, unknown> = {}; // Allowed
286
+
287
+ const keyed: Record<'foo', unknown> = {}; // Error
288
+ // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
289
+ ```
290
+
291
+ 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:
292
+
293
+ ```
294
+ type Indexed = {} extends Record<string, unknown>
295
+ ? '✅ `{}` is assignable to `Record<string, unknown>`'
296
+ : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
297
+ // => '✅ `{}` is assignable to `Record<string, unknown>`'
298
+
299
+ type Keyed = {} extends Record<'foo' | 'bar', unknown>
300
+ ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
301
+ : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
302
+ // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
303
+ ```
304
+
305
+ 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`...
306
+
307
+ ```
308
+ import type {OmitIndexSignature} from 'type-fest';
309
+
310
+ type OmitIndexSignature<ObjectType> = {
311
+ [KeyType in keyof ObjectType // Map each key of `ObjectType`...
312
+ ]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
313
+ };
314
+ ```
315
+
316
+ ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
317
+
318
+ ```
319
+ import type {OmitIndexSignature} from 'type-fest';
320
+
321
+ type OmitIndexSignature<ObjectType> = {
322
+ [KeyType in keyof ObjectType
323
+ // Is `{}` assignable to `Record<KeyType, unknown>`?
324
+ as {} extends Record<KeyType, unknown>
325
+ ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
326
+ : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
327
+ ]: ObjectType[KeyType];
328
+ };
329
+ ```
330
+
331
+ 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.
332
+
333
+ @example
334
+ ```
335
+ import type {OmitIndexSignature} from 'type-fest';
336
+
337
+ interface Example {
338
+ // These index signatures will be removed.
339
+ [x: string]: any
340
+ [x: number]: any
341
+ [x: symbol]: any
342
+ [x: `head-${string}`]: string
343
+ [x: `${string}-tail`]: string
344
+ [x: `head-${string}-tail`]: string
345
+ [x: `${bigint}`]: string
346
+ [x: `embedded-${number}`]: string
347
+
348
+ // These explicitly defined keys will remain.
349
+ foo: 'bar';
350
+ qux?: 'baz';
351
+ }
352
+
353
+ type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
354
+ // => { foo: 'bar'; qux?: 'baz' | undefined; }
355
+ ```
356
+
357
+ @see PickIndexSignature
358
+ @category Object
359
+ */
360
+ type OmitIndexSignature<ObjectType> = {
361
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
362
+ ? never
363
+ : KeyType]: ObjectType[KeyType];
364
+ };
365
+
366
+ /**
367
+ Pick only index signatures from the given object type, leaving out all explicitly defined properties.
368
+
369
+ This is the counterpart of `OmitIndexSignature`.
370
+
371
+ @example
372
+ ```
373
+ import type {PickIndexSignature} from 'type-fest';
374
+
375
+ declare const symbolKey: unique symbol;
376
+
377
+ type Example = {
378
+ // These index signatures will remain.
379
+ [x: string]: unknown;
380
+ [x: number]: unknown;
381
+ [x: symbol]: unknown;
382
+ [x: `head-${string}`]: string;
383
+ [x: `${string}-tail`]: string;
384
+ [x: `head-${string}-tail`]: string;
385
+ [x: `${bigint}`]: string;
386
+ [x: `embedded-${number}`]: string;
387
+
388
+ // These explicitly defined keys will be removed.
389
+ ['kebab-case-key']: string;
390
+ [symbolKey]: string;
391
+ foo: 'bar';
392
+ qux?: 'baz';
393
+ };
394
+
395
+ type ExampleIndexSignature = PickIndexSignature<Example>;
396
+ // {
397
+ // [x: string]: unknown;
398
+ // [x: number]: unknown;
399
+ // [x: symbol]: unknown;
400
+ // [x: `head-${string}`]: string;
401
+ // [x: `${string}-tail`]: string;
402
+ // [x: `head-${string}-tail`]: string;
403
+ // [x: `${bigint}`]: string;
404
+ // [x: `embedded-${number}`]: string;
405
+ // }
406
+ ```
407
+
408
+ @see OmitIndexSignature
409
+ @category Object
410
+ */
411
+ type PickIndexSignature<ObjectType> = {
412
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
413
+ ? KeyType
414
+ : never]: ObjectType[KeyType];
415
+ };
416
+
417
+ // Merges two objects without worrying about index signatures.
418
+ type SimpleMerge<Destination, Source> = {
419
+ [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
420
+ } & Source;
421
+
422
+ /**
423
+ Merge two types into a new type. Keys of the second type overrides keys of the first type.
424
+
425
+ @example
426
+ ```
427
+ import type {Merge} from 'type-fest';
428
+
429
+ interface Foo {
430
+ [x: string]: unknown;
431
+ [x: number]: unknown;
432
+ foo: string;
433
+ bar: symbol;
434
+ }
435
+
436
+ type Bar = {
437
+ [x: number]: number;
438
+ [x: symbol]: unknown;
439
+ bar: Date;
440
+ baz: boolean;
441
+ };
442
+
443
+ export type FooBar = Merge<Foo, Bar>;
444
+ // => {
445
+ // [x: string]: unknown;
446
+ // [x: number]: number;
447
+ // [x: symbol]: unknown;
448
+ // foo: string;
449
+ // bar: Date;
450
+ // baz: boolean;
451
+ // }
452
+ ```
453
+
454
+ @category Object
455
+ */
456
+ type Merge<Destination, Source> =
457
+ Simplify<
458
+ SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
459
+ & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
460
+ >;
461
+
462
+ /**
463
+ An if-else-like type that resolves depending on whether the given type is `any`.
464
+
465
+ @see {@link IsAny}
466
+
467
+ @example
468
+ ```
469
+ import type {IfAny} from 'type-fest';
470
+
471
+ type ShouldBeTrue = IfAny<any>;
472
+ //=> true
473
+
474
+ type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
475
+ //=> 'bar'
476
+ ```
477
+
478
+ @category Type Guard
479
+ @category Utilities
480
+ */
481
+ type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
482
+ IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
483
+ );
484
+
485
+ /**
486
+ Merges user specified options with default options.
487
+
488
+ @example
489
+ ```
490
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
491
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
492
+ type SpecifiedOptions = {leavesOnly: true};
493
+
494
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
495
+ //=> {maxRecursionDepth: 10; leavesOnly: true}
496
+ ```
497
+
498
+ @example
499
+ ```
500
+ // Complains if default values are not provided for optional options
501
+
502
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
503
+ type DefaultPathsOptions = {maxRecursionDepth: 10};
504
+ type SpecifiedOptions = {};
505
+
506
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
507
+ // ~~~~~~~~~~~~~~~~~~~
508
+ // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
509
+ ```
510
+
511
+ @example
512
+ ```
513
+ // Complains if an option's default type does not conform to the expected type
514
+
515
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
516
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
517
+ type SpecifiedOptions = {};
518
+
519
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
520
+ // ~~~~~~~~~~~~~~~~~~~
521
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
522
+ ```
523
+
524
+ @example
525
+ ```
526
+ // Complains if an option's specified type does not conform to the expected type
527
+
528
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
529
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
530
+ type SpecifiedOptions = {leavesOnly: 'yes'};
531
+
532
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
533
+ // ~~~~~~~~~~~~~~~~
534
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
535
+ ```
536
+ */
537
+ type ApplyDefaultOptions<
538
+ Options extends object,
539
+ Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
540
+ SpecifiedOptions extends Options,
541
+ > =
542
+ IfAny<SpecifiedOptions, Defaults,
543
+ IfNever<SpecifiedOptions, Defaults,
544
+ Simplify<Merge<Defaults, {
545
+ [Key in keyof SpecifiedOptions
546
+ as Key extends OptionalKeysOf<Options>
547
+ ? Extract<SpecifiedOptions[Key], undefined> extends never
548
+ ? Key
549
+ : never
550
+ : Key
551
+ ]: SpecifiedOptions[Key]
552
+ }> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
553
+ >>;
554
+
42
555
  /**
43
556
  Filter out keys from an object.
44
557
 
@@ -79,6 +592,10 @@ type ExceptOptions = {
79
592
  requireExactProps?: boolean;
80
593
  };
81
594
 
595
+ type DefaultExceptOptions = {
596
+ requireExactProps: false;
597
+ };
598
+
82
599
  /**
83
600
  Create a type from an object type without certain keys.
84
601
 
@@ -132,7 +649,10 @@ type PostPayload = Except<UserData, 'email'>;
132
649
 
133
650
  @category Object
134
651
  */
135
- type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {requireExactProps: false}> = {
652
+ type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {}> =
653
+ _Except<ObjectType, KeysType, ApplyDefaultOptions<ExceptOptions, DefaultExceptOptions, Options>>;
654
+
655
+ type _Except<ObjectType, KeysType extends keyof ObjectType, Options extends Required<ExceptOptions>> = {
136
656
  [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
137
657
  } & (Options['requireExactProps'] extends true
138
658
  ? Partial<Record<KeysType, never>>