type-fest 4.19.0 → 4.20.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.
- package/package.json +2 -1
- package/source/is-literal.d.ts +6 -4
- package/source/merge-deep.d.ts +8 -5
- package/source/omit-deep.d.ts +2 -2
- package/source/paths.d.ts +14 -8
- package/source/schema.d.ts +12 -12
- package/source/simplify-deep.d.ts +74 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-fest",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.20.1",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
},
|
|
48
48
|
"xo": {
|
|
49
49
|
"rules": {
|
|
50
|
+
"@typescript-eslint/no-extraneous-class": "off",
|
|
50
51
|
"@typescript-eslint/ban-ts-comment": "off",
|
|
51
52
|
"@typescript-eslint/ban-types": "off",
|
|
52
53
|
"@typescript-eslint/naming-convention": "off",
|
package/source/is-literal.d.ts
CHANGED
|
@@ -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] //
|
|
26
|
-
? [
|
|
27
|
-
?
|
|
28
|
-
|
|
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
|
);
|
package/source/merge-deep.d.ts
CHANGED
|
@@ -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<
|
|
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
|
-
> =
|
|
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> =
|
|
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
|
-
|
|
481
|
-
|
|
483
|
+
SimplifyDeepExcludeArray<Destination>,
|
|
484
|
+
SimplifyDeepExcludeArray<Source>,
|
|
482
485
|
Options
|
|
483
486
|
>;
|
package/source/omit-deep.d.ts
CHANGED
|
@@ -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.
|
package/source/paths.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ import type {EmptyObject} from './empty-object';
|
|
|
3
3
|
import type {IsAny} from './is-any';
|
|
4
4
|
import type {IsNever} from './is-never';
|
|
5
5
|
import type {UnknownArray} from './unknown-array';
|
|
6
|
+
import type {Sum} from './sum';
|
|
7
|
+
import type {LessThan} from './less-than';
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
Generate a union of all possible paths to properties in the given object.
|
|
@@ -45,7 +47,9 @@ open('listB.1'); // TypeError. Because listB only has one element.
|
|
|
45
47
|
@category Object
|
|
46
48
|
@category Array
|
|
47
49
|
*/
|
|
48
|
-
export type Paths<T> =
|
|
50
|
+
export type Paths<T> = Paths_<T>;
|
|
51
|
+
|
|
52
|
+
type Paths_<T, Depth extends number = 0> =
|
|
49
53
|
T extends NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
|
|
50
54
|
? never
|
|
51
55
|
: IsAny<T> extends true
|
|
@@ -53,14 +57,14 @@ export type Paths<T> =
|
|
|
53
57
|
: T extends UnknownArray
|
|
54
58
|
? number extends T['length']
|
|
55
59
|
// We need to handle the fixed and non-fixed index part of the array separately.
|
|
56
|
-
? InternalPaths<StaticPartOfArray<T
|
|
57
|
-
| InternalPaths<Array<VariablePartOfArray<T>[number]
|
|
58
|
-
: InternalPaths<T>
|
|
60
|
+
? InternalPaths<StaticPartOfArray<T>, Depth>
|
|
61
|
+
| InternalPaths<Array<VariablePartOfArray<T>[number]>, Depth>
|
|
62
|
+
: InternalPaths<T, Depth>
|
|
59
63
|
: T extends object
|
|
60
|
-
? InternalPaths<T>
|
|
64
|
+
? InternalPaths<T, Depth>
|
|
61
65
|
: never;
|
|
62
66
|
|
|
63
|
-
export type InternalPaths<_T, T = Required<_T>> =
|
|
67
|
+
export type InternalPaths<_T, Depth extends number = 0, T = Required<_T>> =
|
|
64
68
|
T extends EmptyObject | readonly []
|
|
65
69
|
? never
|
|
66
70
|
: {
|
|
@@ -71,8 +75,10 @@ export type InternalPaths<_T, T = Required<_T>> =
|
|
|
71
75
|
| Key
|
|
72
76
|
| ToString<Key>
|
|
73
77
|
| (
|
|
74
|
-
|
|
75
|
-
?
|
|
78
|
+
LessThan<Depth, 15> extends true // Limit the depth to prevent infinite recursion
|
|
79
|
+
? IsNever<Paths_<T[Key], Sum<Depth, 1>>> extends false
|
|
80
|
+
? `${Key}.${Paths_<T[Key], Sum<Depth, 1>>}`
|
|
81
|
+
: never
|
|
76
82
|
: never
|
|
77
83
|
)
|
|
78
84
|
: never
|
package/source/schema.d.ts
CHANGED
|
@@ -47,25 +47,25 @@ export type Schema<ObjectType, ValueType> = ObjectType extends string
|
|
|
47
47
|
? ValueType
|
|
48
48
|
: ObjectType extends ReadonlySet<unknown>
|
|
49
49
|
? ValueType
|
|
50
|
-
: ObjectType extends
|
|
51
|
-
? ValueType
|
|
52
|
-
: ObjectType extends unknown[]
|
|
50
|
+
: ObjectType extends Array<infer U>
|
|
51
|
+
? Array<Schema<U, ValueType>>
|
|
52
|
+
: ObjectType extends (...arguments_: unknown[]) => unknown
|
|
53
53
|
? ValueType
|
|
54
|
-
: ObjectType extends
|
|
54
|
+
: ObjectType extends Date
|
|
55
55
|
? ValueType
|
|
56
|
-
: ObjectType extends
|
|
56
|
+
: ObjectType extends Function
|
|
57
57
|
? ValueType
|
|
58
|
-
: ObjectType extends
|
|
58
|
+
: ObjectType extends RegExp
|
|
59
59
|
? ValueType
|
|
60
|
-
: ObjectType extends
|
|
61
|
-
? ValueType
|
|
62
|
-
:
|
|
63
|
-
? SchemaObject<ObjectType, ValueType>
|
|
64
|
-
: ValueType;
|
|
60
|
+
: ObjectType extends object
|
|
61
|
+
? SchemaObject<ObjectType, ValueType>
|
|
62
|
+
: ValueType;
|
|
65
63
|
|
|
66
64
|
/**
|
|
67
65
|
Same as `Schema`, but accepts only `object`s as inputs. Internal helper for `Schema`.
|
|
68
66
|
*/
|
|
69
67
|
type SchemaObject<ObjectType extends object, K> = {
|
|
70
|
-
[KeyType in keyof ObjectType]:
|
|
68
|
+
[KeyType in keyof ObjectType]: ObjectType[KeyType] extends readonly unknown[] | unknown[]
|
|
69
|
+
? Schema<ObjectType[KeyType], K>
|
|
70
|
+
: Schema<ObjectType[KeyType], K> | K;
|
|
71
71
|
};
|
|
@@ -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 `
|
|
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
|
|
110
|
+
export type SimplifyDeep<Type, ExcludeType = never> =
|
|
111
|
+
ConditionalSimplifyDeep<
|
|
112
|
+
Type,
|
|
113
|
+
ExcludeType | NonRecursiveType | Set<unknown> | Map<unknown, unknown>,
|
|
114
|
+
object
|
|
115
|
+
>;
|