type-fest 4.18.3 → 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/index.d.ts +1 -0
- package/package.json +1 -1
- package/readme.md +1 -0
- package/source/is-literal.d.ts +6 -4
- package/source/merge-deep.d.ts +8 -9
- package/source/omit-deep.d.ts +3 -3
- package/source/simplify-deep.d.ts +115 -0
- package/source/simplify.d.ts +1 -1
package/index.d.ts
CHANGED
|
@@ -61,6 +61,7 @@ export type {SetReturnType} from './source/set-return-type';
|
|
|
61
61
|
export type {SetParameterType} from './source/set-parameter-type';
|
|
62
62
|
export type {Asyncify} from './source/asyncify';
|
|
63
63
|
export type {Simplify} from './source/simplify';
|
|
64
|
+
export type {SimplifyDeep} from './source/simplify-deep';
|
|
64
65
|
export type {Jsonify} from './source/jsonify';
|
|
65
66
|
export type {Jsonifiable} from './source/jsonifiable';
|
|
66
67
|
export type {Schema} from './source/schema';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -173,6 +173,7 @@ Click the type names for complete docs.
|
|
|
173
173
|
- [`SetReturnType`](source/set-return-type.d.ts) - Create a function type with a return type of your choice and the same parameters as the given function type.
|
|
174
174
|
- [`SetParameterType`](source/set-parameter-type.d.ts) - Create a function that replaces some parameters with the given parameters.
|
|
175
175
|
- [`Simplify`](source/simplify.d.ts) - 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.
|
|
176
|
+
- [`SimplifyDeep`](source/simplify-deep.d.ts) - Deeply simplifies an object type.
|
|
176
177
|
- [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
|
|
177
178
|
- [`StringKeyOf`](source/string-key-of.d.ts) - Get keys of the given type as strings.
|
|
178
179
|
- [`Schema`](source/schema.d.ts) - Create a deep version of another object type where property values are recursively replaced into a given value type.
|
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
|
@@ -11,11 +11,10 @@ import type {
|
|
|
11
11
|
} from './internal';
|
|
12
12
|
import type {UnknownRecord} from './unknown-record';
|
|
13
13
|
import type {EnforceOptional} from './enforce-optional';
|
|
14
|
+
import type {SimplifyDeep} from './simplify-deep';
|
|
15
|
+
import type {UnknownArray} from './unknown-array';
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
Deeply simplifies an object excluding iterables and functions. Used internally to improve the UX and accept both interfaces and type aliases as inputs.
|
|
17
|
-
*/
|
|
18
|
-
export type SimplifyDeep<Type> = ConditionalSimplifyDeep<Type, Function | Iterable<unknown>, object>;
|
|
17
|
+
type SimplifyDeepExcludeArray<T> = SimplifyDeep<T, UnknownArray>;
|
|
19
18
|
|
|
20
19
|
/**
|
|
21
20
|
Try to merge two record properties or return the source property value, preserving `undefined` properties values in both cases.
|
|
@@ -251,7 +250,7 @@ type MergeDeepArrayRecursive<
|
|
|
251
250
|
: DoMergeArrayOrTuple<Destination, Source, Options>
|
|
252
251
|
: Destination[number] extends UnknownRecord
|
|
253
252
|
? Source[number] extends UnknownRecord
|
|
254
|
-
? Array<
|
|
253
|
+
? Array<SimplifyDeepExcludeArray<MergeDeepRecord<Destination[number], Source[number], Options>>>
|
|
255
254
|
: DoMergeArrayOrTuple<Destination, Source, Options>
|
|
256
255
|
: DoMergeArrayOrTuple<Destination, Source, Options>;
|
|
257
256
|
|
|
@@ -294,7 +293,7 @@ type MergeDeepOrReturn<
|
|
|
294
293
|
Destination,
|
|
295
294
|
Source,
|
|
296
295
|
Options extends MergeDeepInternalOptions,
|
|
297
|
-
> =
|
|
296
|
+
> = SimplifyDeepExcludeArray<[undefined] extends [Destination | Source]
|
|
298
297
|
? DefaultType
|
|
299
298
|
: Destination extends UnknownRecord
|
|
300
299
|
? Source extends UnknownRecord
|
|
@@ -357,7 +356,7 @@ type DefaultMergeDeepOptions<Options extends MergeDeepOptions> = Merge<{
|
|
|
357
356
|
/**
|
|
358
357
|
This utility selects the correct entry point with the corresponding default options. This avoids re-merging the options at each iteration.
|
|
359
358
|
*/
|
|
360
|
-
type MergeDeepWithDefaultOptions<Destination, Source, Options extends MergeDeepOptions> =
|
|
359
|
+
type MergeDeepWithDefaultOptions<Destination, Source, Options extends MergeDeepOptions> = SimplifyDeepExcludeArray<
|
|
361
360
|
[undefined] extends [Destination | Source]
|
|
362
361
|
? never
|
|
363
362
|
: Destination extends UnknownRecord
|
|
@@ -481,7 +480,7 @@ function mergeDeep<Destination, Source, Options extends MergeDeepOptions = {}>(
|
|
|
481
480
|
@category Utilities
|
|
482
481
|
*/
|
|
483
482
|
export type MergeDeep<Destination, Source, Options extends MergeDeepOptions = {}> = MergeDeepWithDefaultOptions<
|
|
484
|
-
|
|
485
|
-
|
|
483
|
+
SimplifyDeepExcludeArray<Destination>,
|
|
484
|
+
SimplifyDeepExcludeArray<Source>,
|
|
486
485
|
Options
|
|
487
486
|
>;
|
package/source/omit-deep.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ import type {ExactKey, IsArrayReadonly, NonRecursiveType, SetArrayAccess, ToStri
|
|
|
3
3
|
import type {IsEqual} from './is-equal';
|
|
4
4
|
import type {IsNever} from './is-never';
|
|
5
5
|
import type {LiteralUnion} from './literal-union';
|
|
6
|
-
import type {SimplifyDeep} from './merge-deep';
|
|
7
6
|
import type {Paths} from './paths';
|
|
8
7
|
import type {SharedUnionFieldsDeep} from './shared-union-fields-deep';
|
|
8
|
+
import type {SimplifyDeep} from './simplify-deep';
|
|
9
9
|
import type {UnknownArray} from './unknown-array';
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -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.
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type {ConditionalSimplifyDeep} from './conditional-simplify';
|
|
2
|
+
import type {NonRecursiveType} from './internal';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Deeply simplifies an object type.
|
|
6
|
+
|
|
7
|
+
You can exclude certain types from being simplified by providing them in the second generic `ExcludeType`.
|
|
8
|
+
|
|
9
|
+
Useful to flatten the type output to improve type hints shown in editors.
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
import type {SimplifyDeep} from 'type-fest';
|
|
14
|
+
|
|
15
|
+
type PositionX = {
|
|
16
|
+
left: number;
|
|
17
|
+
right: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type PositionY = {
|
|
21
|
+
top: number;
|
|
22
|
+
bottom: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type Properties1 = {
|
|
26
|
+
height: number;
|
|
27
|
+
position: PositionY;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type Properties2 = {
|
|
31
|
+
width: number;
|
|
32
|
+
position: PositionX;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type Properties = Properties1 & Properties2;
|
|
36
|
+
// In your editor, hovering over `Props` will show the following:
|
|
37
|
+
//
|
|
38
|
+
// type Properties = Properties1 & Properties2;
|
|
39
|
+
|
|
40
|
+
type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2>;
|
|
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`:
|
|
93
|
+
//
|
|
94
|
+
// SimplifyDeepProperties = {
|
|
95
|
+
// height: number;
|
|
96
|
+
// width: number;
|
|
97
|
+
// position: {
|
|
98
|
+
// top: number;
|
|
99
|
+
// bottom: number;
|
|
100
|
+
// left: number;
|
|
101
|
+
// right: number;
|
|
102
|
+
// };
|
|
103
|
+
// foo: ComplexType;
|
|
104
|
+
// };
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
@see Simplify
|
|
108
|
+
@category Object
|
|
109
|
+
*/
|
|
110
|
+
export type SimplifyDeep<Type, ExcludeType = never> =
|
|
111
|
+
ConditionalSimplifyDeep<
|
|
112
|
+
Type,
|
|
113
|
+
ExcludeType | NonRecursiveType | Set<unknown> | Map<unknown, unknown>,
|
|
114
|
+
object
|
|
115
|
+
>;
|
package/source/simplify.d.ts
CHANGED
|
@@ -52,7 +52,7 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
|
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
55
|
-
|
|
55
|
+
@see SimplifyDeep
|
|
56
56
|
@category Object
|
|
57
57
|
*/
|
|
58
58
|
export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|