type-fest 5.4.4 → 5.6.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 +10 -0
- package/package.json +6 -4
- package/readme.md +83 -58
- package/source/absolute.d.ts +52 -0
- package/source/all-extend.d.ts +5 -6
- package/source/all-union-fields.d.ts +18 -18
- package/source/and-all.d.ts +76 -0
- package/source/and.d.ts +4 -3
- package/source/array-length.d.ts +36 -0
- package/source/array-splice.d.ts +26 -26
- package/source/camel-case.d.ts +38 -5
- package/source/camel-cased-properties-deep.d.ts +11 -4
- package/source/camel-cased-properties.d.ts +5 -1
- package/source/conditional-pick-deep.d.ts +5 -3
- package/source/conditional-pick.d.ts +6 -4
- package/source/delimiter-case.d.ts +1 -0
- package/source/delimiter-cased-properties-deep.d.ts +8 -1
- package/source/delimiter-cased-properties.d.ts +5 -1
- package/source/empty-object.d.ts +1 -1
- package/source/entries.d.ts +1 -1
- package/source/entry.d.ts +1 -1
- package/source/exclude-exactly.d.ts +57 -0
- package/source/get.d.ts +1 -1
- package/source/greater-than-or-equal.d.ts +34 -1
- package/source/greater-than.d.ts +37 -3
- package/source/has-optional-keys.d.ts +1 -1
- package/source/has-readonly-keys.d.ts +1 -1
- package/source/has-required-keys.d.ts +1 -1
- package/source/has-writable-keys.d.ts +1 -1
- package/source/int-closed-range.d.ts +1 -3
- package/source/int-range.d.ts +3 -5
- package/source/internal/array.d.ts +7 -14
- package/source/internal/keys.d.ts +9 -9
- package/source/internal/numeric.d.ts +13 -23
- package/source/internal/tuple.d.ts +3 -3
- package/source/internal/type.d.ts +1 -0
- package/source/is-equal.d.ts +0 -1
- package/source/is-integer.d.ts +8 -8
- package/source/is-literal.d.ts +5 -5
- package/source/is-union.d.ts +12 -12
- package/source/iterable-element.d.ts +5 -5
- package/source/jsonify.d.ts +4 -4
- package/source/kebab-case.d.ts +1 -0
- package/source/kebab-cased-properties-deep.d.ts +7 -0
- package/source/kebab-cased-properties.d.ts +5 -1
- package/source/keys-of-union.d.ts +2 -2
- package/source/less-than-or-equal.d.ts +40 -4
- package/source/less-than.d.ts +35 -3
- package/source/literal-to-primitive.d.ts +1 -1
- package/source/literal-union.d.ts +1 -1
- package/source/merge-exclusive.d.ts +3 -3
- package/source/merge.d.ts +25 -0
- package/source/multidimensional-array.d.ts +1 -1
- package/source/multidimensional-readonly-array.d.ts +1 -1
- package/source/non-nullable-deep.d.ts +102 -0
- package/source/numeric.d.ts +3 -3
- package/source/omit-deep.d.ts +22 -23
- package/source/optional.d.ts +31 -0
- package/source/or-all.d.ts +73 -0
- package/source/or.d.ts +4 -11
- package/source/package-json.d.ts +7 -7
- package/source/partial-deep.d.ts +3 -1
- package/source/pascal-case.d.ts +1 -0
- package/source/pascal-cased-properties-deep.d.ts +7 -0
- package/source/pascal-cased-properties.d.ts +5 -1
- package/source/pick-deep.d.ts +8 -21
- package/source/readonly-deep.d.ts +5 -3
- package/source/remove-prefix.d.ts +15 -15
- package/source/replace.d.ts +2 -2
- package/source/require-all-or-none.d.ts +1 -1
- package/source/require-at-least-one.d.ts +5 -7
- package/source/require-exactly-one.d.ts +3 -3
- package/source/require-one-or-none.d.ts +1 -1
- package/source/required-deep.d.ts +3 -1
- package/source/screaming-snake-case.d.ts +1 -0
- package/source/set-non-nullable-deep.d.ts +6 -3
- package/source/set-non-nullable.d.ts +4 -11
- package/source/set-optional.d.ts +6 -10
- package/source/set-parameter-type.d.ts +2 -2
- package/source/set-readonly.d.ts +4 -8
- package/source/set-required-deep.d.ts +1 -1
- package/source/set-required.d.ts +4 -8
- package/source/shared-union-fields-deep.d.ts +1 -1
- package/source/shared-union-fields.d.ts +9 -9
- package/source/snake-case.d.ts +1 -0
- package/source/snake-cased-properties-deep.d.ts +7 -0
- package/source/snake-cased-properties.d.ts +5 -1
- package/source/some-extend.d.ts +113 -0
- package/source/spread.d.ts +1 -5
- package/source/subtract.d.ts +4 -3
- package/source/sum.d.ts +5 -4
- package/source/tagged.d.ts +5 -7
- package/source/tsconfig-json.d.ts +39 -7
- package/source/union-length.d.ts +27 -0
- package/source/union-member.d.ts +65 -0
- package/source/union-to-intersection.d.ts +1 -1
- package/source/union-to-tuple.d.ts +10 -19
- package/source/words.d.ts +30 -4
- package/source/writable.d.ts +15 -19
- package/source/xor.d.ts +1 -1
package/source/omit-deep.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ import type {IsNever} from './is-never.d.ts';
|
|
|
5
5
|
import type {LiteralUnion} from './literal-union.d.ts';
|
|
6
6
|
import type {Paths} from './paths.d.ts';
|
|
7
7
|
import type {SimplifyDeep} from './simplify-deep.d.ts';
|
|
8
|
-
import type {Simplify} from './simplify.d.ts';
|
|
9
8
|
import type {UnionToTuple} from './union-to-tuple.d.ts';
|
|
10
9
|
import type {UnknownArray} from './unknown-array.d.ts';
|
|
11
10
|
|
|
@@ -18,7 +17,7 @@ It supports removing specific items from an array, replacing each removed item w
|
|
|
18
17
|
|
|
19
18
|
Use-case: Remove unneeded parts of complex objects.
|
|
20
19
|
|
|
21
|
-
Use [`Omit
|
|
20
|
+
Use [`Omit<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) if you only need one level deep.
|
|
22
21
|
|
|
23
22
|
@example
|
|
24
23
|
```
|
|
@@ -94,34 +93,34 @@ type OmitDeepHelper<T, PathTuple extends UnknownArray> =
|
|
|
94
93
|
Omit one path from the given object/array.
|
|
95
94
|
*/
|
|
96
95
|
type OmitDeepWithOnePath<T, Path extends string | number> =
|
|
97
|
-
T extends NonRecursiveType
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
96
|
+
T extends NonRecursiveType
|
|
97
|
+
? T
|
|
98
|
+
: T extends UnknownArray ? SetArrayAccess<OmitDeepArrayWithOnePath<T, Path>, IsArrayReadonly<T>>
|
|
99
|
+
: T extends object ? OmitDeepObjectWithOnePath<T, Path>
|
|
100
|
+
: T;
|
|
102
101
|
|
|
103
102
|
/**
|
|
104
103
|
Omit one path from the given object.
|
|
105
104
|
*/
|
|
106
105
|
type OmitDeepObjectWithOnePath<ObjectT extends object, P extends string | number> =
|
|
107
|
-
P extends `${infer RecordKeyInPath}.${infer SubPath}`
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
106
|
+
P extends `${infer RecordKeyInPath}.${infer SubPath}`
|
|
107
|
+
? {
|
|
108
|
+
[Key in keyof ObjectT]:
|
|
109
|
+
IsEqual<RecordKeyInPath, ToString<Key>> extends true
|
|
110
|
+
? ExactKey<ObjectT, Key> extends infer RealKey
|
|
111
|
+
? RealKey extends keyof ObjectT
|
|
112
|
+
? OmitDeepWithOnePath<ObjectT[RealKey], SubPath>
|
|
113
|
+
: ObjectT[Key]
|
|
114
114
|
: ObjectT[Key]
|
|
115
115
|
: ObjectT[Key]
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
: ObjectT;
|
|
116
|
+
}
|
|
117
|
+
: ExactKey<ObjectT, P> extends infer Key
|
|
118
|
+
? IsNever<Key> extends true
|
|
119
|
+
? ObjectT
|
|
120
|
+
: Key extends PropertyKey
|
|
121
|
+
? Omit<ObjectT, Key>
|
|
122
|
+
: ObjectT
|
|
123
|
+
: ObjectT;
|
|
125
124
|
|
|
126
125
|
/**
|
|
127
126
|
Omit one path from from the given array.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Create a type that represents either the value or `undefined`, while stripping `null` from the type.
|
|
3
|
+
|
|
4
|
+
Use-cases:
|
|
5
|
+
- Enforcing the practice of using `undefined` instead of `null` as the "absence of value" marker.
|
|
6
|
+
- Converting APIs that return `null` (DOM, JSON, legacy libraries) to use `undefined` consistently.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {Optional} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
// Adds `undefined` to the type
|
|
13
|
+
type MaybeNumber = Optional<number>;
|
|
14
|
+
//=> number | undefined
|
|
15
|
+
|
|
16
|
+
// Strips `null` from the type
|
|
17
|
+
type NullableString = Optional<string | null>;
|
|
18
|
+
//=> string | undefined
|
|
19
|
+
|
|
20
|
+
type Config = {
|
|
21
|
+
name: string;
|
|
22
|
+
description: Optional<string>;
|
|
23
|
+
//=> string | undefined
|
|
24
|
+
};
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
@category Utilities
|
|
28
|
+
*/
|
|
29
|
+
export type Optional<Value> = Exclude<Value, null> | undefined;
|
|
30
|
+
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type {SomeExtend} from './some-extend.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether any of the given elements is `true`.
|
|
5
|
+
|
|
6
|
+
Use-cases:
|
|
7
|
+
- Check if at least one condition in a list of booleans is met.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {OrAll} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type FFT = OrAll<[false, false, true]>;
|
|
14
|
+
//=> true
|
|
15
|
+
|
|
16
|
+
type FFF = OrAll<[false, false, false]>;
|
|
17
|
+
//=> false
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Note: When `boolean` is passed as an element, it is distributed into separate cases, and the final result is a union of those cases.
|
|
21
|
+
For example, `OrAll<[false, boolean]>` expands to `OrAll<[false, true]> | OrAll<[false, false]>`, which simplifies to `true | false` (i.e., `boolean`).
|
|
22
|
+
|
|
23
|
+
@example
|
|
24
|
+
```
|
|
25
|
+
import type {OrAll} from 'type-fest';
|
|
26
|
+
|
|
27
|
+
type A = OrAll<[false, boolean]>;
|
|
28
|
+
//=> boolean
|
|
29
|
+
|
|
30
|
+
type B = OrAll<[true, boolean]>;
|
|
31
|
+
//=> true
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Note: If `never` is passed as an element, it is treated as `false` and the result is computed accordingly.
|
|
35
|
+
|
|
36
|
+
@example
|
|
37
|
+
```
|
|
38
|
+
import type {OrAll} from 'type-fest';
|
|
39
|
+
|
|
40
|
+
type A = OrAll<[never, never, true]>;
|
|
41
|
+
//=> true
|
|
42
|
+
|
|
43
|
+
type B = OrAll<[never, never, false]>;
|
|
44
|
+
//=> false
|
|
45
|
+
|
|
46
|
+
type C = OrAll<[never, never, never]>;
|
|
47
|
+
//=> false
|
|
48
|
+
|
|
49
|
+
type D = OrAll<[never, never, boolean]>;
|
|
50
|
+
//=> boolean
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Note: If `any` is passed as an element, it is treated as `boolean` and the result is computed accordingly.
|
|
54
|
+
|
|
55
|
+
@example
|
|
56
|
+
```
|
|
57
|
+
import type {OrAll} from 'type-fest';
|
|
58
|
+
|
|
59
|
+
type A = OrAll<[false, any]>;
|
|
60
|
+
//=> boolean
|
|
61
|
+
|
|
62
|
+
type B = OrAll<[true, any]>;
|
|
63
|
+
//=> true
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Note: `OrAll<[]>` evaluates to `false` because there are no `true` elements in an empty tuple. See [Wikipedia: Clause (logic) > Empty clauses](https://en.wikipedia.org/wiki/Clause_(logic)#Empty_clauses:~:text=The%20truth%20evaluation%20of%20an%20empty%20disjunctive%20clause%20is%20always%20false.).
|
|
67
|
+
|
|
68
|
+
@see {@link Or}
|
|
69
|
+
@see {@link AndAll}
|
|
70
|
+
*/
|
|
71
|
+
export type OrAll<T extends readonly boolean[]> = SomeExtend<T, true>;
|
|
72
|
+
|
|
73
|
+
export {};
|
package/source/or.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {IsNever} from './is-never.d.ts';
|
|
1
|
+
import type {OrAll} from './or-all.d.ts';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
Returns a boolean for whether either of two given types is true
|
|
4
|
+
Returns a boolean for whether either of two given types is `true`.
|
|
6
5
|
|
|
7
6
|
Use-case: Constructing complex conditional types where at least one condition must be satisfied.
|
|
8
7
|
|
|
@@ -74,16 +73,10 @@ type G = Or<never, never>;
|
|
|
74
73
|
//=> false
|
|
75
74
|
```
|
|
76
75
|
|
|
76
|
+
@see {@link OrAll}
|
|
77
77
|
@see {@link And}
|
|
78
78
|
@see {@link Xor}
|
|
79
79
|
*/
|
|
80
|
-
export type Or<A extends boolean, B extends boolean> =
|
|
81
|
-
_Or<If<IsNever<A>, false, A>, If<IsNever<B>, false, B>>; // `never` is treated as `false`
|
|
82
|
-
|
|
83
|
-
export type _Or<A extends boolean, B extends boolean> = A extends true
|
|
84
|
-
? true
|
|
85
|
-
: B extends true
|
|
86
|
-
? true
|
|
87
|
-
: false;
|
|
80
|
+
export type Or<A extends boolean, B extends boolean> = OrAll<[A, B]>;
|
|
88
81
|
|
|
89
82
|
export {};
|
package/source/package-json.d.ts
CHANGED
|
@@ -699,12 +699,12 @@ Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-j
|
|
|
699
699
|
@category File
|
|
700
700
|
*/
|
|
701
701
|
export type PackageJson =
|
|
702
|
-
JsonObject
|
|
703
|
-
PackageJson.NodeJsStandard
|
|
704
|
-
PackageJson.PackageJsonStandard
|
|
705
|
-
PackageJson.NonStandardEntryPoints
|
|
706
|
-
PackageJson.TypeScriptConfiguration
|
|
707
|
-
PackageJson.YarnConfiguration
|
|
708
|
-
PackageJson.JSPMConfiguration;
|
|
702
|
+
JsonObject
|
|
703
|
+
& PackageJson.NodeJsStandard
|
|
704
|
+
& PackageJson.PackageJsonStandard
|
|
705
|
+
& PackageJson.NonStandardEntryPoints
|
|
706
|
+
& PackageJson.TypeScriptConfiguration
|
|
707
|
+
& PackageJson.YarnConfiguration
|
|
708
|
+
& PackageJson.JSPMConfiguration;
|
|
709
709
|
|
|
710
710
|
export {};
|
package/source/partial-deep.d.ts
CHANGED
|
@@ -44,12 +44,14 @@ type DefaultPartialDeepOptions = {
|
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
|
-
Create a
|
|
47
|
+
Create a deeply optional version of another type.
|
|
48
48
|
|
|
49
49
|
Use-cases:
|
|
50
50
|
- Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
|
|
51
51
|
- Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
|
|
52
52
|
|
|
53
|
+
Use [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) if you only need one level deep.
|
|
54
|
+
|
|
53
55
|
@example
|
|
54
56
|
```
|
|
55
57
|
import type {PartialDeep} from 'type-fest';
|
package/source/pascal-case.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {PascalCase} from 'type-fest';
|
|
|
12
12
|
|
|
13
13
|
const someVariable: PascalCase<'foo-bar'> = 'FooBar';
|
|
14
14
|
const preserveConsecutiveUppercase: PascalCase<'foo-BAR-baz', {preserveConsecutiveUppercase: true}> = 'FooBARBaz';
|
|
15
|
+
const splitOnPunctuation: PascalCase<'foo-bar>>baz', {splitOnPunctuation: true}> = 'FooBarBaz';
|
|
15
16
|
|
|
16
17
|
// Advanced
|
|
17
18
|
|
|
@@ -48,6 +48,13 @@ const preserveConsecutiveUppercase: PascalCasedPropertiesDeep<{fooBAR: {fooBARBi
|
|
|
48
48
|
}],
|
|
49
49
|
},
|
|
50
50
|
};
|
|
51
|
+
|
|
52
|
+
const splitOnPunctuation: PascalCasedPropertiesDeep<{'user@info': {'user::id': number; 'user::name': string}}, {splitOnPunctuation: true}> = {
|
|
53
|
+
UserInfo: {
|
|
54
|
+
UserId: 1,
|
|
55
|
+
UserName: 'Tom',
|
|
56
|
+
},
|
|
57
|
+
};
|
|
51
58
|
```
|
|
52
59
|
|
|
53
60
|
@category Change case
|
|
@@ -3,7 +3,7 @@ import type {ApplyDefaultOptions} from './internal/index.d.ts';
|
|
|
3
3
|
import type {PascalCase} from './pascal-case.d.ts';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
Convert object properties to pascal case
|
|
6
|
+
Convert top-level object properties to pascal case.
|
|
7
7
|
|
|
8
8
|
This can be useful when, for example, converting some API types from a different style.
|
|
9
9
|
|
|
@@ -27,6 +27,10 @@ const result: PascalCasedProperties<User> = {
|
|
|
27
27
|
const preserveConsecutiveUppercase: PascalCasedProperties<{fooBAR: string}, {preserveConsecutiveUppercase: true}> = {
|
|
28
28
|
FooBAR: 'string',
|
|
29
29
|
};
|
|
30
|
+
|
|
31
|
+
const splitOnPunctuation: PascalCasedProperties<{'foo::bar': string}, {splitOnPunctuation: true}> = {
|
|
32
|
+
FooBar: 'string',
|
|
33
|
+
};
|
|
30
34
|
```
|
|
31
35
|
|
|
32
36
|
@category Change case
|
package/source/pick-deep.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {Paths} from './paths.d.ts';
|
|
|
5
5
|
import type {Simplify} from './simplify.d.ts';
|
|
6
6
|
import type {UnionToIntersection} from './union-to-intersection.d.ts';
|
|
7
7
|
import type {UnknownArray} from './unknown-array.d.ts';
|
|
8
|
+
import type {SimplifyDeep} from './simplify-deep.d.ts';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
Pick properties from a deeply-nested object.
|
|
@@ -13,6 +14,8 @@ It supports recursing into arrays.
|
|
|
13
14
|
|
|
14
15
|
Use-case: Distill complex objects down to the components you need to target.
|
|
15
16
|
|
|
17
|
+
Use [`Pick<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys) if you only need one level deep.
|
|
18
|
+
|
|
16
19
|
@example
|
|
17
20
|
```
|
|
18
21
|
import type {PickDeep, PartialDeep} from 'type-fest';
|
|
@@ -36,24 +39,15 @@ type Configuration = {
|
|
|
36
39
|
};
|
|
37
40
|
|
|
38
41
|
type NameConfig = PickDeep<Configuration, 'userConfig.name'>;
|
|
39
|
-
|
|
40
|
-
// userConfig: {
|
|
41
|
-
// name: string;
|
|
42
|
-
// }
|
|
43
|
-
// };
|
|
42
|
+
//=> {userConfig: {name: string}}
|
|
44
43
|
|
|
45
44
|
// Supports optional properties
|
|
46
45
|
type User = PickDeep<PartialDeep<Configuration>, 'userConfig.name' | 'userConfig.age'>;
|
|
47
|
-
|
|
48
|
-
// userConfig?: {
|
|
49
|
-
// name?: string;
|
|
50
|
-
// age?: number;
|
|
51
|
-
// };
|
|
52
|
-
// };
|
|
46
|
+
//=> {userConfig?: {name?: string; age?: number}}
|
|
53
47
|
|
|
54
48
|
// Supports array
|
|
55
49
|
type AddressConfig = PickDeep<Configuration, 'userConfig.address.0'>;
|
|
56
|
-
|
|
50
|
+
//=> {
|
|
57
51
|
// userConfig: {
|
|
58
52
|
// address: [{
|
|
59
53
|
// city1: string;
|
|
@@ -64,14 +58,7 @@ type AddressConfig = PickDeep<Configuration, 'userConfig.address.0'>;
|
|
|
64
58
|
|
|
65
59
|
// Supports recurse into array
|
|
66
60
|
type Street = PickDeep<Configuration, 'userConfig.address.1.street2'>;
|
|
67
|
-
|
|
68
|
-
// userConfig: {
|
|
69
|
-
// address: [
|
|
70
|
-
// unknown,
|
|
71
|
-
// {street2: string}
|
|
72
|
-
// ];
|
|
73
|
-
// };
|
|
74
|
-
// }
|
|
61
|
+
//=> {userConfig: {address: [unknown, {street2: string}]}}
|
|
75
62
|
```
|
|
76
63
|
|
|
77
64
|
@category Object
|
|
@@ -86,7 +73,7 @@ export type PickDeep<T, PathUnion extends Paths<T>> =
|
|
|
86
73
|
}[PathUnion]
|
|
87
74
|
>
|
|
88
75
|
: T extends object
|
|
89
|
-
?
|
|
76
|
+
? SimplifyDeep<UnionToIntersection<{
|
|
90
77
|
[P in PathUnion]: InternalPickDeep<T, P>;
|
|
91
78
|
}[PathUnion]>>
|
|
92
79
|
: never;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type {BuiltIns, HasMultipleCallSignatures} from './internal/index.d.ts';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
|
|
4
|
+
Create a deeply immutable version of another type.
|
|
5
5
|
|
|
6
6
|
This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
|
|
7
7
|
|
|
8
8
|
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
|
|
9
9
|
|
|
10
|
+
Use [`Readonly<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) if you only need one level deep.
|
|
11
|
+
|
|
10
12
|
@example
|
|
11
13
|
```
|
|
12
14
|
import type {ReadonlyDeep} from 'type-fest';
|
|
@@ -83,8 +85,8 @@ export type ReadonlyDeep<T> = T extends BuiltIns
|
|
|
83
85
|
? ReadonlyMapDeep<KeyType, ValueType>
|
|
84
86
|
: T extends Readonly<ReadonlySet<infer ItemType>>
|
|
85
87
|
? ReadonlySetDeep<ItemType>
|
|
86
|
-
|
|
87
|
-
T extends readonly [] | readonly [...never[]]
|
|
88
|
+
// Identify tuples to avoid converting them to arrays inadvertently; special case `readonly [...never[]]`, as it emerges undesirably from recursive invocations of ReadonlyDeep below.
|
|
89
|
+
: T extends readonly [] | readonly [...never[]]
|
|
88
90
|
? readonly []
|
|
89
91
|
: T extends readonly [infer U, ...infer V]
|
|
90
92
|
? readonly [ReadonlyDeep<U>, ...ReadonlyDeep<V>]
|
|
@@ -110,23 +110,23 @@ type D = RemovePrefix<`handle${Capitalize<string>}`, 'handle'>;
|
|
|
110
110
|
@category Template literal
|
|
111
111
|
*/
|
|
112
112
|
export type RemovePrefix<S extends string, Prefix extends string, Options extends RemovePrefixOptions = {}> =
|
|
113
|
-
IfNotAnyOrNever<
|
|
114
|
-
S,
|
|
115
113
|
IfNotAnyOrNever<
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
114
|
+
S,
|
|
115
|
+
IfNotAnyOrNever<
|
|
116
|
+
Prefix,
|
|
117
|
+
_RemovePrefix<S, Prefix, ApplyDefaultOptions<RemovePrefixOptions, DefaultRemovePrefixOptions, Options>>,
|
|
118
|
+
string,
|
|
119
|
+
S
|
|
120
|
+
>
|
|
121
|
+
>;
|
|
122
122
|
|
|
123
123
|
type _RemovePrefix<S extends string, Prefix extends string, Options extends Required<RemovePrefixOptions>> =
|
|
124
|
-
Prefix extends string // For distributing `Prefix`
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
124
|
+
Prefix extends string // For distributing `Prefix`
|
|
125
|
+
? S extends `${Prefix}${infer Rest}`
|
|
126
|
+
? Or<IsStringLiteral<Prefix>, Not<Options['strict']>> extends true
|
|
127
|
+
? Rest
|
|
128
|
+
: string // Fallback to `string` when `Prefix` is non-literal and `strict` is disabled
|
|
129
|
+
: S // Return back `S` when `Prefix` is not present at the start of `S`
|
|
130
|
+
: never;
|
|
131
131
|
|
|
132
132
|
export {};
|
package/source/replace.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ declare function replace<
|
|
|
27
27
|
>(
|
|
28
28
|
input: Input,
|
|
29
29
|
search: Search,
|
|
30
|
-
replacement: Replacement
|
|
30
|
+
replacement: Replacement,
|
|
31
31
|
): Replace<Input, Search, Replacement>;
|
|
32
32
|
|
|
33
33
|
declare function replaceAll<
|
|
@@ -37,7 +37,7 @@ declare function replaceAll<
|
|
|
37
37
|
>(
|
|
38
38
|
input: Input,
|
|
39
39
|
search: Search,
|
|
40
|
-
replacement: Replacement
|
|
40
|
+
replacement: Replacement,
|
|
41
41
|
): Replace<Input, Search, Replacement, {all: true}>;
|
|
42
42
|
|
|
43
43
|
// The return type is the exact string literal, not just `string`.
|
|
@@ -9,7 +9,7 @@ Requires all of the keys in the given object.
|
|
|
9
9
|
type RequireAll<ObjectType, KeysType extends keyof ObjectType> = Required<Pick<ObjectType, KeysType>>;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
Create a type that requires all of the given keys or none of the given keys
|
|
12
|
+
Create a type that requires all of the given keys or none of the given keys, while keeping the remaining keys as is.
|
|
13
13
|
|
|
14
14
|
Use-cases:
|
|
15
15
|
- Creating interfaces for components with mutually-inclusive keys.
|
|
@@ -5,7 +5,7 @@ import type {IsAny} from './is-any.d.ts';
|
|
|
5
5
|
import type {IsNever} from './is-never.d.ts';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
Create a type that requires at least one of the given keys
|
|
8
|
+
Create a type that requires at least one of the given keys, while keeping the remaining keys as is.
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```
|
|
@@ -40,11 +40,9 @@ type _RequireAtLeastOne<
|
|
|
40
40
|
KeysType extends keyof ObjectType,
|
|
41
41
|
> = {
|
|
42
42
|
// For each `Key` in `KeysType` make a mapped type:
|
|
43
|
-
[Key in KeysType]-?: Required<Pick<ObjectType, Key>>
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// 3. Add the remaining keys not in `KeysType`
|
|
48
|
-
Except<ObjectType, KeysType>;
|
|
43
|
+
[Key in KeysType]-?: Required<Pick<ObjectType, Key>> // 1. Make `Key`'s type required
|
|
44
|
+
& Partial<Pick<ObjectType, Exclude<KeysType, Key>>>; // 2. Make all other keys in `KeysType` optional
|
|
45
|
+
}[KeysType]
|
|
46
|
+
& Except<ObjectType, KeysType>; // 3. Add the remaining keys not in `KeysType`
|
|
49
47
|
|
|
50
48
|
export {};
|
|
@@ -4,7 +4,7 @@ import type {IsAny} from './is-any.d.ts';
|
|
|
4
4
|
import type {IsNever} from './is-never.d.ts';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
Create a type that requires exactly one of the given keys and disallows more
|
|
7
|
+
Create a type that requires exactly one of the given keys and disallows more, while keeping the remaining keys as is.
|
|
8
8
|
|
|
9
9
|
Use-cases:
|
|
10
10
|
- Creating interfaces for components that only need one of the keys to display properly.
|
|
@@ -41,8 +41,8 @@ export type RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType = ke
|
|
|
41
41
|
|
|
42
42
|
type _RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType> =
|
|
43
43
|
{[Key in KeysType]: (
|
|
44
|
-
Required<Pick<ObjectType, Key>>
|
|
45
|
-
Partial<Record<Exclude<KeysType, Key>, never>>
|
|
44
|
+
Required<Pick<ObjectType, Key>>
|
|
45
|
+
& Partial<Record<Exclude<KeysType, Key>, never>>
|
|
46
46
|
)}[KeysType] & Omit<ObjectType, KeysType>;
|
|
47
47
|
|
|
48
48
|
export {};
|
|
@@ -5,7 +5,7 @@ import type {IsAny} from './is-any.d.ts';
|
|
|
5
5
|
import type {IsNever} from './is-never.d.ts';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
Create a type that requires exactly one of the given keys
|
|
8
|
+
Create a type that requires exactly one of the given keys or none of the given keys, while keeping the remaining keys as is.
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```
|
|
@@ -3,12 +3,14 @@ import type {IsNever} from './is-never.d.ts';
|
|
|
3
3
|
import type {Simplify} from './simplify.d.ts';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
Create a
|
|
6
|
+
Create a deeply required version of another type.
|
|
7
7
|
|
|
8
8
|
Use-cases:
|
|
9
9
|
- Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified.
|
|
10
10
|
- Modeling the resulting type after a deep merge with a set of defaults.
|
|
11
11
|
|
|
12
|
+
Use [`Required<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype) if you only need one level deep.
|
|
13
|
+
|
|
12
14
|
@example
|
|
13
15
|
```
|
|
14
16
|
import type {RequiredDeep} from 'type-fest';
|
|
@@ -14,6 +14,7 @@ import type {ScreamingSnakeCase} from 'type-fest';
|
|
|
14
14
|
|
|
15
15
|
const someVariable: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR';
|
|
16
16
|
const someVariableNoSplitOnNumbers: ScreamingSnakeCase<'p2pNetwork', {splitOnNumbers: false}> = 'P2P_NETWORK';
|
|
17
|
+
const someVariableWithPunctuation: ScreamingSnakeCase<'div.card::after', {splitOnPunctuation: true}> = 'DIV_CARD_AFTER';
|
|
17
18
|
|
|
18
19
|
```
|
|
19
20
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type {NonRecursiveType, StringToNumber} from './internal/index.d.ts';
|
|
2
|
+
import type {IsAny} from './is-any.d.ts';
|
|
3
|
+
import type {NonNullableDeep} from './non-nullable-deep.d.ts';
|
|
2
4
|
import type {Paths} from './paths.d.ts';
|
|
3
5
|
import type {SetNonNullable} from './set-non-nullable.d.ts';
|
|
4
6
|
import type {Simplify} from './simplify.d.ts';
|
|
@@ -8,7 +10,7 @@ import type {UnknownArray} from './unknown-array.d.ts';
|
|
|
8
10
|
/**
|
|
9
11
|
Create a type that makes the specified keys non-nullable (removes `null` and `undefined`), supports deeply nested key paths, and leaves all other keys unchanged.
|
|
10
12
|
|
|
11
|
-
NOTE: Optional modifiers (`?`) are not removed from properties. For example, `SetNonNullableDeep<{foo?: string | null | undefined}, 'foo'>` will result in `{foo?: string}`.
|
|
13
|
+
NOTE: Optional modifiers (`?`) are not removed from properties. For example, `SetNonNullableDeep<{foo?: string | null | undefined}, 'foo'>` will result in `{foo?: string}`. To remove both optional modifiers and nullables, use {@link SetRequiredDeep} in conjunction with this type.
|
|
12
14
|
|
|
13
15
|
@example
|
|
14
16
|
```
|
|
@@ -55,8 +57,9 @@ type ArrayExample2 = SetNonNullableDeep<{a: [(number | null)?, (number | null)?]
|
|
|
55
57
|
|
|
56
58
|
@category Object
|
|
57
59
|
*/
|
|
58
|
-
export type SetNonNullableDeep<BaseType, KeyPaths extends Paths<BaseType>> =
|
|
59
|
-
|
|
60
|
+
export type SetNonNullableDeep<BaseType, KeyPaths extends Paths<BaseType>> = IsAny<KeyPaths> extends true
|
|
61
|
+
? NonNullableDeep<BaseType>
|
|
62
|
+
: SetNonNullableDeepHelper<BaseType, UnionToTuple<KeyPaths>>;
|
|
60
63
|
|
|
61
64
|
/**
|
|
62
65
|
Internal helper for {@link SetNonNullableDeep}.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
Create a type that makes the given keys non-nullable,
|
|
2
|
+
Create a type that makes the given keys non-nullable, while keeping the remaining keys as is.
|
|
3
3
|
|
|
4
4
|
If no keys are given, all keys will be made non-nullable.
|
|
5
5
|
|
|
@@ -15,19 +15,12 @@ type Foo = {
|
|
|
15
15
|
c?: boolean | null;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
+
// Note: In the following example, `c` can no longer be `null`, but it's still optional.
|
|
18
19
|
type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;
|
|
19
|
-
|
|
20
|
-
// a: number | null;
|
|
21
|
-
// b: string; // Can no longer be undefined.
|
|
22
|
-
// c?: boolean; // Can no longer be null, but is still optional.
|
|
23
|
-
// }
|
|
20
|
+
//=> {a: null | number; b: string; c?: boolean}
|
|
24
21
|
|
|
25
22
|
type AllNonNullable = SetNonNullable<Foo>;
|
|
26
|
-
|
|
27
|
-
// a: number; // Can no longer be null.
|
|
28
|
-
// b: string; // Can no longer be undefined.
|
|
29
|
-
// c?: boolean; // Can no longer be null, but is still optional.
|
|
30
|
-
// }
|
|
23
|
+
//=> {a: number; b: string; c?: boolean}
|
|
31
24
|
```
|
|
32
25
|
|
|
33
26
|
@category Object
|
package/source/set-optional.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type {HomomorphicPick} from './internal/index.d.ts';
|
|
|
3
3
|
import type {Simplify} from './simplify.d.ts';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
Create a type that makes the given keys optional
|
|
6
|
+
Create a type that makes the given keys optional, while keeping the remaining keys as is.
|
|
7
7
|
|
|
8
8
|
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are optional.
|
|
9
9
|
|
|
@@ -18,11 +18,7 @@ type Foo = {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
|
|
21
|
-
|
|
22
|
-
// a: number;
|
|
23
|
-
// b?: string; // Was already optional and still is.
|
|
24
|
-
// c?: boolean; // Is now optional.
|
|
25
|
-
// }
|
|
21
|
+
//=> {a: number; b?: string; c?: boolean}
|
|
26
22
|
```
|
|
27
23
|
|
|
28
24
|
@category Object
|
|
@@ -36,10 +32,10 @@ export type SetOptional<BaseType, Keys extends keyof BaseType> =
|
|
|
36
32
|
type _SetOptional<BaseType, Keys extends keyof BaseType> =
|
|
37
33
|
BaseType extends unknown // To distribute `BaseType` when it's a union type.
|
|
38
34
|
? Simplify<
|
|
39
|
-
|
|
40
|
-
Except<BaseType, Keys>
|
|
41
|
-
|
|
42
|
-
Partial<HomomorphicPick<BaseType, Keys>>
|
|
35
|
+
// Pick just the keys that are readonly from the base type.
|
|
36
|
+
Except<BaseType, Keys>
|
|
37
|
+
// Pick the keys that should be mutable from the base type and make them mutable.
|
|
38
|
+
& Partial<HomomorphicPick<BaseType, Keys>>
|
|
43
39
|
>
|
|
44
40
|
: never;
|
|
45
41
|
|
|
@@ -38,14 +38,14 @@ type MergeObjectToArray<TArray extends UnknownArray, TObject, TArrayCopy extends
|
|
|
38
38
|
: K extends keyof TObject ? TObject[K] : TArray[K]
|
|
39
39
|
}
|
|
40
40
|
: TObject extends object
|
|
41
|
-
// If `TObject` is
|
|
41
|
+
// If `TObject` is an object with number keys like `{0: string, 1: number}`
|
|
42
42
|
? {
|
|
43
43
|
[K in keyof TArray]:
|
|
44
44
|
K extends `${infer NumberK extends number}`
|
|
45
45
|
? NumberK extends keyof TObject ? TObject[NumberK] : TArray[K]
|
|
46
46
|
: number extends K
|
|
47
47
|
// If array key `K` is `number`, means it's a rest parameter, we should set the rest parameter type to corresponding type in `TObject`.
|
|
48
|
-
// example: `
|
|
48
|
+
// example: `MergeObjectToArray<[string, ...boolean[]], {1: number}>` => `[string, ...number[]]`
|
|
49
49
|
? StaticPartOfArray<TArrayCopy>['length'] extends keyof TObject
|
|
50
50
|
? TObject[StaticPartOfArray<TArrayCopy>['length']]
|
|
51
51
|
: TArray[K]
|