type-fest 4.19.0 → 4.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "4.19.0",
3
+ "version": "4.20.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -22,10 +22,12 @@ LiteralCheck<1, string>
22
22
  */
23
23
  type LiteralCheck<T, LiteralType extends Primitive> = (
24
24
  IsNever<T> extends false // Must be wider than `never`
25
- ? [T] extends [LiteralType] // Must be narrower than `LiteralType`
26
- ? [LiteralType] extends [T] // Cannot be wider than `LiteralType`
27
- ? false
28
- : true
25
+ ? [T] extends [LiteralType & infer U] // Remove any branding
26
+ ? [U] extends [LiteralType] // Must be narrower than `LiteralType`
27
+ ? [LiteralType] extends [U] // Cannot be wider than `LiteralType`
28
+ ? false
29
+ : true
30
+ : false
29
31
  : false
30
32
  : false
31
33
  );
@@ -12,6 +12,9 @@ import type {
12
12
  import type {UnknownRecord} from './unknown-record';
13
13
  import type {EnforceOptional} from './enforce-optional';
14
14
  import type {SimplifyDeep} from './simplify-deep';
15
+ import type {UnknownArray} from './unknown-array';
16
+
17
+ type SimplifyDeepExcludeArray<T> = SimplifyDeep<T, UnknownArray>;
15
18
 
16
19
  /**
17
20
  Try to merge two record properties or return the source property value, preserving `undefined` properties values in both cases.
@@ -247,7 +250,7 @@ type MergeDeepArrayRecursive<
247
250
  : DoMergeArrayOrTuple<Destination, Source, Options>
248
251
  : Destination[number] extends UnknownRecord
249
252
  ? Source[number] extends UnknownRecord
250
- ? Array<SimplifyDeep<MergeDeepRecord<Destination[number], Source[number], Options>>>
253
+ ? Array<SimplifyDeepExcludeArray<MergeDeepRecord<Destination[number], Source[number], Options>>>
251
254
  : DoMergeArrayOrTuple<Destination, Source, Options>
252
255
  : DoMergeArrayOrTuple<Destination, Source, Options>;
253
256
 
@@ -290,7 +293,7 @@ type MergeDeepOrReturn<
290
293
  Destination,
291
294
  Source,
292
295
  Options extends MergeDeepInternalOptions,
293
- > = SimplifyDeep<[undefined] extends [Destination | Source]
296
+ > = SimplifyDeepExcludeArray<[undefined] extends [Destination | Source]
294
297
  ? DefaultType
295
298
  : Destination extends UnknownRecord
296
299
  ? Source extends UnknownRecord
@@ -353,7 +356,7 @@ type DefaultMergeDeepOptions<Options extends MergeDeepOptions> = Merge<{
353
356
  /**
354
357
  This utility selects the correct entry point with the corresponding default options. This avoids re-merging the options at each iteration.
355
358
  */
356
- type MergeDeepWithDefaultOptions<Destination, Source, Options extends MergeDeepOptions> = SimplifyDeep<
359
+ type MergeDeepWithDefaultOptions<Destination, Source, Options extends MergeDeepOptions> = SimplifyDeepExcludeArray<
357
360
  [undefined] extends [Destination | Source]
358
361
  ? never
359
362
  : Destination extends UnknownRecord
@@ -477,7 +480,7 @@ function mergeDeep<Destination, Source, Options extends MergeDeepOptions = {}>(
477
480
  @category Utilities
478
481
  */
479
482
  export type MergeDeep<Destination, Source, Options extends MergeDeepOptions = {}> = MergeDeepWithDefaultOptions<
480
- SimplifyDeep<Destination>,
481
- SimplifyDeep<Source>,
483
+ SimplifyDeepExcludeArray<Destination>,
484
+ SimplifyDeepExcludeArray<Source>,
482
485
  Options
483
486
  >;
@@ -75,8 +75,8 @@ export type OmitDeep<T, PathUnion extends LiteralUnion<Paths<T>, string>> =
75
75
  SimplifyDeep<
76
76
  SharedUnionFieldsDeep<
77
77
  {[P in PathUnion]: OmitDeepWithOnePath<T, P>}[PathUnion]
78
- >
79
- >;
78
+ >,
79
+ UnknownArray>;
80
80
 
81
81
  /**
82
82
  Omit one path from the given object/array.
@@ -1,28 +1,35 @@
1
1
  import type {ConditionalSimplifyDeep} from './conditional-simplify';
2
+ import type {NonRecursiveType} from './internal';
2
3
 
3
4
  /**
4
5
  Deeply simplifies an object type.
5
6
 
7
+ You can exclude certain types from being simplified by providing them in the second generic `ExcludeType`.
8
+
6
9
  Useful to flatten the type output to improve type hints shown in editors.
7
10
 
8
11
  @example
9
12
  ```
10
13
  import type {SimplifyDeep} from 'type-fest';
11
14
 
15
+ type PositionX = {
16
+ left: number;
17
+ right: number;
18
+ };
19
+
20
+ type PositionY = {
21
+ top: number;
22
+ bottom: number;
23
+ };
24
+
12
25
  type Properties1 = {
13
26
  height: number;
14
- position: {
15
- top: number;
16
- bottom: number;
17
- };
27
+ position: PositionY;
18
28
  };
19
29
 
20
30
  type Properties2 = {
21
31
  width: number;
22
- position: {
23
- left: number;
24
- right: number;
25
- };
32
+ position: PositionX;
26
33
  };
27
34
 
28
35
  type Properties = Properties1 & Properties2;
@@ -31,7 +38,58 @@ type Properties = Properties1 & Properties2;
31
38
  // type Properties = Properties1 & Properties2;
32
39
 
33
40
  type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2>;
34
- // But if wrapped in SimplifyDeep, hovering over `Props` will show a flattened object with all the properties:
41
+ // But if wrapped in SimplifyDeep, hovering over `SimplifyDeepProperties` will show a flattened object with all the properties:
42
+ //
43
+ // SimplifyDeepProperties = {
44
+ // height: number;
45
+ // width: number;
46
+ // position: {
47
+ // top: number;
48
+ // bottom: number;
49
+ // left: number;
50
+ // right: number;
51
+ // };
52
+ // };
53
+ ```
54
+
55
+ @example
56
+ ```
57
+ import type {SimplifyDeep} from 'type-fest';
58
+
59
+ // A complex type that you don't want or need to simplify
60
+ type ComplexType = {
61
+ a: string;
62
+ b: 'b';
63
+ c: number;
64
+ ...
65
+ };
66
+
67
+ type PositionX = {
68
+ left: number;
69
+ right: number;
70
+ };
71
+
72
+ type PositionY = {
73
+ top: number;
74
+ bottom: number;
75
+ };
76
+
77
+ // You want to simplify all other types
78
+ type Properties1 = {
79
+ height: number;
80
+ position: PositionY;
81
+ foo: ComplexType;
82
+ };
83
+
84
+ type Properties2 = {
85
+ width: number;
86
+ position: PositionX;
87
+ foo: ComplexType;
88
+ };
89
+
90
+ type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2, ComplexType>;
91
+ // If wrapped in `SimplifyDeep` and set `ComplexType` to exclude, hovering over `SimplifyDeepProperties` will
92
+ // show a flattened object with all the properties except `ComplexType`:
35
93
  //
36
94
  // SimplifyDeepProperties = {
37
95
  // height: number;
@@ -42,10 +100,16 @@ type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2>;
42
100
  // left: number;
43
101
  // right: number;
44
102
  // };
103
+ // foo: ComplexType;
45
104
  // };
46
105
  ```
47
106
 
48
107
  @see Simplify
49
108
  @category Object
50
109
  */
51
- export type SimplifyDeep<Type> = ConditionalSimplifyDeep<Type, Function | Iterable<unknown>, object>;
110
+ export type SimplifyDeep<Type, ExcludeType = never> =
111
+ ConditionalSimplifyDeep<
112
+ Type,
113
+ ExcludeType | NonRecursiveType | Set<unknown> | Map<unknown, unknown>,
114
+ object
115
+ >;