@depup/type-fest 5.4.4-depup.0 → 5.9.0-depup.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/README.md +2 -2
- package/changes.json +5 -0
- package/index.d.ts +23 -8
- package/package.json +17 -11
- package/readme.md +98 -64
- package/source/absolute.d.ts +52 -0
- package/source/all-extend.d.ts +8 -7
- 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-reverse.d.ts +4 -3
- package/source/array-splice.d.ts +26 -26
- package/source/array-tail.d.ts +4 -4
- 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-keys.d.ts +1 -1
- package/source/conditional-pick-deep.d.ts +5 -3
- package/source/conditional-pick.d.ts +6 -4
- package/source/delimiter-case.d.ts +11 -9
- 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/exclude-rest-element.d.ts +4 -4
- package/source/exclusify-union.d.ts +4 -4
- package/source/extends-strict.d.ts +129 -22
- package/source/extract-exactly.d.ts +56 -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 +8 -15
- package/source/internal/keys.d.ts +9 -9
- package/source/internal/numeric.d.ts +19 -27
- package/source/internal/object.d.ts +44 -6
- package/source/internal/string.d.ts +1 -76
- package/source/internal/tuple.d.ts +3 -3
- package/source/internal/type.d.ts +16 -9
- package/source/is-boolean-literal.d.ts +39 -0
- package/source/is-equal.d.ts +0 -1
- package/source/is-integer.d.ts +8 -8
- package/source/is-literal.d.ts +43 -267
- package/source/is-numeric-literal.d.ts +50 -0
- package/source/is-string-literal.d.ts +72 -0
- package/source/is-symbol-literal.d.ts +39 -0
- package/source/is-union.d.ts +12 -12
- package/source/iterable-element.d.ts +5 -5
- package/source/jsonify.d.ts +5 -9
- 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/last-array-element.d.ts +66 -13
- 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/object-merge.d.ts +21 -16
- 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 +16 -34
- package/source/remove-suffix.d.ts +114 -0
- package/source/rename-keys.d.ts +163 -0
- package/source/replace.d.ts +2 -2
- package/source/require-all-or-none.d.ts +5 -5
- package/source/require-at-least-one.d.ts +9 -11
- package/source/require-exactly-one.d.ts +7 -7
- package/source/require-one-or-none.d.ts +5 -5
- package/source/required-deep.d.ts +3 -1
- package/source/schema.d.ts +16 -8
- package/source/screaming-snake-case.d.ts +1 -0
- package/source/set-non-nullable-deep.d.ts +8 -4
- 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 +3 -2
- package/source/set-required.d.ts +4 -8
- package/source/shared-union-fields-deep.d.ts +5 -4
- 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 +115 -0
- package/source/split-on-rest-element.d.ts +6 -4
- package/source/split.d.ts +1 -1
- package/source/spread.d.ts +1 -5
- package/source/string-length.d.ts +38 -0
- package/source/string-repeat.d.ts +48 -21
- package/source/string-slice.d.ts +1 -1
- package/source/string-to-array.d.ts +97 -0
- package/source/string-to-number.d.ts +67 -0
- 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 +40 -8
- package/source/tuple-of.d.ts +42 -8
- package/source/typed-array.d.ts +1 -0
- 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/unwrap-required.d.ts +37 -0
- package/source/words.d.ts +30 -4
- package/source/writable.d.ts +15 -19
- package/source/xor.d.ts +1 -1
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type {BuiltIns, HasMultipleCallSignatures} from './internal/type.d.ts';
|
|
2
|
+
import type {IsNever} from './is-never.d.ts';
|
|
3
|
+
import type {Simplify} from './simplify.d.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Recursively removes `null` and `undefined` from the specified type.
|
|
7
|
+
|
|
8
|
+
Use-cases:
|
|
9
|
+
- Normalizing data received from external sources where `null`/`undefined` have been cleaned.
|
|
10
|
+
- Creating non-nullable variants of deeply nested types.
|
|
11
|
+
|
|
12
|
+
NOTE: Optional modifiers (`?`) are not removed from properties. For example, `NonNullableDeep<{foo?: string | null | undefined}>` will result in `{foo?: string}`. To remove both optional modifiers and nullables, use {@link RequiredDeep} in conjunction with this type.
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import type {NonNullableDeep} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
type UserDraft = {
|
|
19
|
+
name: string | null;
|
|
20
|
+
address: {
|
|
21
|
+
city: string | undefined;
|
|
22
|
+
postalCode: string | null;
|
|
23
|
+
landmark?: string | undefined;
|
|
24
|
+
};
|
|
25
|
+
tags: Array<string | null>;
|
|
26
|
+
visits: Map<string | null, {
|
|
27
|
+
date: Date | null;
|
|
28
|
+
notes: Set<string | undefined>;
|
|
29
|
+
}>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type User = NonNullableDeep<UserDraft>;
|
|
33
|
+
//=> {
|
|
34
|
+
// name: string;
|
|
35
|
+
// address: {
|
|
36
|
+
// city: string;
|
|
37
|
+
// postalCode: string;
|
|
38
|
+
// landmark?: string;
|
|
39
|
+
// };
|
|
40
|
+
// tags: string[];
|
|
41
|
+
// visits: Map<string, {
|
|
42
|
+
// date: Date;
|
|
43
|
+
// notes: Set<string>;
|
|
44
|
+
// }>;
|
|
45
|
+
// }
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
@example
|
|
49
|
+
```
|
|
50
|
+
import type {NonNullableDeep} from 'type-fest';
|
|
51
|
+
|
|
52
|
+
type ArrayExample = NonNullableDeep<[{a: number | undefined}, ...Array<{b: string | null}>]>;
|
|
53
|
+
//=> [{a: number}, ...{b: string}[]]
|
|
54
|
+
|
|
55
|
+
type MapExample = NonNullableDeep<{a: Map<{a: string | null}, {c: number | undefined}>}>;
|
|
56
|
+
//=> {a: Map<{a: string}, {c: number}>}
|
|
57
|
+
|
|
58
|
+
type SetExample = NonNullableDeep<Set<{a: string | null}> | null | undefined>;
|
|
59
|
+
//=> Set<{a: string}>
|
|
60
|
+
|
|
61
|
+
type PromiseExample = NonNullableDeep<{a: Promise<{b: string | null}>}>;
|
|
62
|
+
//=> {a: Promise<{b: string}>}
|
|
63
|
+
|
|
64
|
+
type FunctionExample = NonNullableDeep<(a: string | null) => number | undefined>;
|
|
65
|
+
//=> (a: string) => number
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
@category Utilities
|
|
69
|
+
@category Object
|
|
70
|
+
@category Array
|
|
71
|
+
@category Set
|
|
72
|
+
@category Map
|
|
73
|
+
*/
|
|
74
|
+
export type NonNullableDeep<T> =
|
|
75
|
+
T extends BuiltIns | (new (...arguments_: any[]) => unknown)
|
|
76
|
+
? Exclude<T, null | undefined> // `Exclude` is used instead of `NonNullable` because `NonNullable<void>` results in `void & {}`.
|
|
77
|
+
: T extends Map<infer KeyType, infer ValueType>
|
|
78
|
+
? Map<NonNullableDeep<KeyType>, NonNullableDeep<ValueType>>
|
|
79
|
+
: T extends Set<infer ItemType>
|
|
80
|
+
? Set<NonNullableDeep<ItemType>>
|
|
81
|
+
: T extends ReadonlyMap<infer KeyType, infer ValueType>
|
|
82
|
+
? ReadonlyMap<NonNullableDeep<KeyType>, NonNullableDeep<ValueType>>
|
|
83
|
+
: T extends ReadonlySet<infer ItemType>
|
|
84
|
+
? ReadonlySet<NonNullableDeep<ItemType>>
|
|
85
|
+
: T extends WeakMap<infer KeyType, infer ValueType>
|
|
86
|
+
? WeakMap<NonNullableDeep<KeyType>, NonNullableDeep<ValueType>>
|
|
87
|
+
: T extends WeakSet<infer ItemType>
|
|
88
|
+
? WeakSet<NonNullableDeep<ItemType>>
|
|
89
|
+
: T extends Promise<infer ValueType>
|
|
90
|
+
? Promise<NonNullableDeep<ValueType>>
|
|
91
|
+
: T extends (...arguments_: any[]) => unknown
|
|
92
|
+
? HasMultipleCallSignatures<T> extends true
|
|
93
|
+
? T
|
|
94
|
+
: ((...arguments_: NonNullableDeep<Parameters<T>>) => NonNullableDeep<ReturnType<T>>)
|
|
95
|
+
& (IsNever<keyof T> extends true
|
|
96
|
+
? unknown
|
|
97
|
+
: NonNullableDeep<Simplify<T>>) // `Simplify` removes the call signature
|
|
98
|
+
: T extends object
|
|
99
|
+
? {[P in keyof T]: NonNullableDeep<T[P]>}
|
|
100
|
+
: unknown;
|
|
101
|
+
|
|
102
|
+
export {};
|
package/source/numeric.d.ts
CHANGED
|
@@ -121,9 +121,9 @@ declare function setPercentage<T extends number>(length: Float<T>): void;
|
|
|
121
121
|
@category Numeric
|
|
122
122
|
*/
|
|
123
123
|
export type Float<T> =
|
|
124
|
-
T extends unknown // To distributive type
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
T extends unknown // To distributive type
|
|
125
|
+
? IsFloat<T> extends true ? T : never
|
|
126
|
+
: never; // Never happens
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
129
|
A negative (`-∞ < x < 0`) `number` that is not an integer.
|
package/source/object-merge.d.ts
CHANGED
|
@@ -90,22 +90,27 @@ Note: If you want a simple merge where properties from the second object always
|
|
|
90
90
|
@category Object
|
|
91
91
|
*/
|
|
92
92
|
export type ObjectMerge<First extends object, Second extends object> =
|
|
93
|
-
IfNotAnyOrNever<First,
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
? unknown
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
93
|
+
IfNotAnyOrNever<First, {
|
|
94
|
+
ifNot: IfNotAnyOrNever<Second, {
|
|
95
|
+
ifNot: First extends unknown // For distributing `First`
|
|
96
|
+
? Second extends unknown // For distributing `Second`
|
|
97
|
+
? First extends MapsSetsOrArrays
|
|
98
|
+
? unknown
|
|
99
|
+
: Second extends MapsSetsOrArrays
|
|
100
|
+
? unknown
|
|
101
|
+
: _ObjectMerge<
|
|
102
|
+
First,
|
|
103
|
+
Second,
|
|
104
|
+
NormalizedLiteralKeys<First>,
|
|
105
|
+
NormalizedLiteralKeys<Second>,
|
|
106
|
+
IsExactOptionalPropertyTypesEnabled extends true ? Required<First> : First,
|
|
107
|
+
IsExactOptionalPropertyTypesEnabled extends true ? Required<Second> : Second
|
|
108
|
+
>
|
|
109
|
+
: never // Should never happen
|
|
110
|
+
: never; // Should never happen
|
|
111
|
+
}>;
|
|
112
|
+
ifAny: First & Second;
|
|
113
|
+
}>;
|
|
109
114
|
|
|
110
115
|
type _ObjectMerge<
|
|
111
116
|
First extends object,
|
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>]
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type {ApplyDefaultOptions} from './internal/object.d.ts';
|
|
2
2
|
import type {IfNotAnyOrNever, Not} from './internal/type.d.ts';
|
|
3
|
-
import type {IsStringLiteral} from './is-literal.d.ts';
|
|
3
|
+
import type {IsStringLiteral} from './is-string-literal.d.ts';
|
|
4
|
+
import type {IsNever} from './is-never.d.ts';
|
|
4
5
|
import type {Or} from './or.d.ts';
|
|
6
|
+
import type {If} from './if.d.ts';
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
@see {@link RemovePrefix}
|
|
@@ -58,24 +60,6 @@ export type RemovePrefixOptions = {
|
|
|
58
60
|
type D = RemovePrefix<`id-${number}`, 'id-', {strict: false}>;
|
|
59
61
|
//=> `${number}`
|
|
60
62
|
```
|
|
61
|
-
|
|
62
|
-
Note: If it can be statically determined that the input string can never start with the specified non-literal prefix, then the input string is returned as-is, regardless of the value of this option.
|
|
63
|
-
For example, ``RemovePrefix<`${string}/${number}`, `${string}:`>`` returns `` `${string}/${number}` ``, since a string of type `` `${string}/${number}` `` can never start with a prefix of type `` `${string}:` ``.
|
|
64
|
-
```
|
|
65
|
-
import type {RemovePrefix} from 'type-fest';
|
|
66
|
-
|
|
67
|
-
type A = RemovePrefix<`${string}/${number}`, `${string}:`, {strict: true}>;
|
|
68
|
-
//=> `${string}/${number}`
|
|
69
|
-
|
|
70
|
-
type B = RemovePrefix<`${string}/${number}`, `${string}:`, {strict: false}>;
|
|
71
|
-
//=> `${string}/${number}`
|
|
72
|
-
|
|
73
|
-
type C = RemovePrefix<'on-change', `${number}-`, {strict: true}>;
|
|
74
|
-
//=> 'on-change'
|
|
75
|
-
|
|
76
|
-
type D = RemovePrefix<'on-change', `${number}-`, {strict: false}>;
|
|
77
|
-
//=> 'on-change'
|
|
78
|
-
```
|
|
79
63
|
*/
|
|
80
64
|
strict?: boolean;
|
|
81
65
|
};
|
|
@@ -110,23 +94,21 @@ type D = RemovePrefix<`handle${Capitalize<string>}`, 'handle'>;
|
|
|
110
94
|
@category Template literal
|
|
111
95
|
*/
|
|
112
96
|
export type RemovePrefix<S extends string, Prefix extends string, Options extends RemovePrefixOptions = {}> =
|
|
113
|
-
IfNotAnyOrNever<
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
>
|
|
121
|
-
>;
|
|
97
|
+
IfNotAnyOrNever<S, {
|
|
98
|
+
ifNot: If<
|
|
99
|
+
IsNever<Prefix>,
|
|
100
|
+
S,
|
|
101
|
+
_RemovePrefix<S, Prefix, ApplyDefaultOptions<RemovePrefixOptions, DefaultRemovePrefixOptions, Options>>
|
|
102
|
+
>;
|
|
103
|
+
}>;
|
|
122
104
|
|
|
123
105
|
type _RemovePrefix<S extends string, Prefix extends string, Options extends Required<RemovePrefixOptions>> =
|
|
124
|
-
Prefix extends string // For distributing `Prefix`
|
|
125
|
-
? S extends `${Prefix}${infer Rest}`
|
|
106
|
+
Prefix extends string // For distributing `Prefix`
|
|
126
107
|
? Or<IsStringLiteral<Prefix>, Not<Options['strict']>> extends true
|
|
127
|
-
? Rest
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
108
|
+
? S extends `${Prefix}${infer Rest}`
|
|
109
|
+
? Rest
|
|
110
|
+
: S // Return back `S` when `Prefix` is not present at the start of `S`
|
|
111
|
+
: string // Fallback to `string` when `Prefix` is non-literal and `strict` is enabled
|
|
112
|
+
: never;
|
|
131
113
|
|
|
132
114
|
export {};
|