type-fest 5.4.4 → 5.5.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 +7 -0
- package/package.json +1 -1
- package/readme.md +14 -4
- package/source/all-extend.d.ts +5 -6
- package/source/and-all.d.ts +76 -0
- package/source/and.d.ts +3 -2
- package/source/array-length.d.ts +36 -0
- package/source/array-splice.d.ts +2 -2
- package/source/conditional-pick-deep.d.ts +5 -3
- package/source/conditional-pick.d.ts +6 -4
- package/source/exclude-exactly.d.ts +57 -0
- package/source/greater-than-or-equal.d.ts +34 -1
- package/source/greater-than.d.ts +34 -1
- package/source/internal/array.d.ts +0 -7
- package/source/internal/tuple.d.ts +1 -1
- package/source/internal/type.d.ts +1 -0
- package/source/is-equal.d.ts +0 -1
- package/source/less-than-or-equal.d.ts +39 -3
- package/source/less-than.d.ts +35 -3
- package/source/merge.d.ts +25 -0
- package/source/omit-deep.d.ts +1 -2
- package/source/optional.d.ts +31 -0
- package/source/or-all.d.ts +73 -0
- package/source/or.d.ts +3 -10
- package/source/pick-deep.d.ts +6 -21
- package/source/set-non-nullable.d.ts +3 -10
- package/source/set-optional.d.ts +1 -5
- package/source/set-parameter-type.d.ts +2 -2
- package/source/set-readonly.d.ts +1 -5
- package/source/set-required.d.ts +1 -5
- package/source/shared-union-fields-deep.d.ts +1 -1
- package/source/some-extend.d.ts +113 -0
- package/source/spread.d.ts +1 -5
- package/source/tagged.d.ts +2 -2
- package/source/union-member.d.ts +65 -0
- package/source/union-to-tuple.d.ts +4 -17
- package/source/writable.d.ts +1 -5
package/index.d.ts
CHANGED
|
@@ -43,6 +43,7 @@ export type {ReadonlyDeep} from './source/readonly-deep.d.ts';
|
|
|
43
43
|
export type {LiteralUnion} from './source/literal-union.d.ts';
|
|
44
44
|
export type {Promisable} from './source/promisable.d.ts';
|
|
45
45
|
export type {Arrayable} from './source/arrayable.d.ts';
|
|
46
|
+
export type {Optional} from './source/optional.d.ts';
|
|
46
47
|
export type {Opaque, UnwrapOpaque, Tagged, GetTagMetadata, UnwrapTagged} from './source/tagged.d.ts';
|
|
47
48
|
export type {InvariantOf} from './source/invariant-of.d.ts';
|
|
48
49
|
export type {SetOptional} from './source/set-optional.d.ts';
|
|
@@ -144,6 +145,7 @@ export type {ArraySlice} from './source/array-slice.d.ts';
|
|
|
144
145
|
export type {ArraySplice} from './source/array-splice.d.ts';
|
|
145
146
|
export type {ArrayTail} from './source/array-tail.d.ts';
|
|
146
147
|
export type {ArrayElement} from './source/array-element.d.ts';
|
|
148
|
+
export type {ArrayLength} from './source/array-length.d.ts';
|
|
147
149
|
export type {SetFieldType, SetFieldTypeOptions} from './source/set-field-type.d.ts';
|
|
148
150
|
export type {Paths, PathsOptions} from './source/paths.d.ts';
|
|
149
151
|
export type {AllUnionFields} from './source/all-union-fields.d.ts';
|
|
@@ -153,9 +155,12 @@ export type {IsNull} from './source/is-null.d.ts';
|
|
|
153
155
|
export type {IfNull} from './source/if-null.d.ts';
|
|
154
156
|
export type {IsUndefined} from './source/is-undefined.d.ts';
|
|
155
157
|
export type {And} from './source/and.d.ts';
|
|
158
|
+
export type {AndAll} from './source/and-all.d.ts';
|
|
156
159
|
export type {Or} from './source/or.d.ts';
|
|
160
|
+
export type {OrAll} from './source/or-all.d.ts';
|
|
157
161
|
export type {Xor} from './source/xor.d.ts';
|
|
158
162
|
export type {AllExtend, AllExtendOptions} from './source/all-extend.d.ts';
|
|
163
|
+
export type {SomeExtend, SomeExtendOptions} from './source/some-extend.d.ts';
|
|
159
164
|
export type {NonEmptyTuple} from './source/non-empty-tuple.d.ts';
|
|
160
165
|
export type {FindGlobalInstanceType, FindGlobalType} from './source/find-global-type.d.ts';
|
|
161
166
|
export type {If} from './source/if.d.ts';
|
|
@@ -167,6 +172,7 @@ export type {IsNullable} from './source/is-nullable.d.ts';
|
|
|
167
172
|
export type {TupleOf} from './source/tuple-of.d.ts';
|
|
168
173
|
export type {ExclusifyUnion} from './source/exclusify-union.d.ts';
|
|
169
174
|
export type {ArrayReverse} from './source/array-reverse.d.ts';
|
|
175
|
+
export type {UnionMember} from './source/union-member.d.ts';
|
|
170
176
|
|
|
171
177
|
// Template literal types
|
|
172
178
|
export type {CamelCase, CamelCaseOptions} from './source/camel-case.d.ts';
|
|
@@ -207,5 +213,6 @@ export type {TsConfigJson} from './source/tsconfig-json.d.ts';
|
|
|
207
213
|
export type {ExtendsStrict} from './source/extends-strict.d.ts';
|
|
208
214
|
export type {ExtractStrict} from './source/extract-strict.d.ts';
|
|
209
215
|
export type {ExcludeStrict} from './source/exclude-strict.d.ts';
|
|
216
|
+
export type {ExcludeExactly} from './source/exclude-exactly.d.ts';
|
|
210
217
|
|
|
211
218
|
export {};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -180,10 +180,13 @@ Click the type names for complete docs.
|
|
|
180
180
|
- [`AllUnionFields`](source/all-union-fields.d.ts) - Create a type with all fields from a union of object types.
|
|
181
181
|
- [`DistributedOmit`](source/distributed-omit.d.ts) - Omits keys from a type, distributing the operation over a union.
|
|
182
182
|
- [`DistributedPick`](source/distributed-pick.d.ts) - Picks keys from a type, distributing the operation over a union.
|
|
183
|
-
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true
|
|
184
|
-
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types is true
|
|
185
|
-
- [`Xor`](source/xor.d.ts) - Returns a boolean for whether only one of two given types is true
|
|
183
|
+
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both `true`.
|
|
184
|
+
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types is `true`.
|
|
185
|
+
- [`Xor`](source/xor.d.ts) - Returns a boolean for whether only one of two given types is `true`.
|
|
186
|
+
- [`AndAll`](source/and-all.d.ts) - Returns a boolean for whether all of the given elements are `true`.
|
|
187
|
+
- [`OrAll`](source/or-all.d.ts) - Returns a boolean for whether any of the given elements is `true`.
|
|
186
188
|
- [`AllExtend`](source/all-extend.d.ts) - Returns a boolean for whether every element in an array type extends another type.
|
|
189
|
+
- [`SomeExtend`](source/some-extend.d.ts) - Returns a boolean for whether some element in an array type extends another type.
|
|
187
190
|
- [`NonEmptyTuple`](source/non-empty-tuple.d.ts) - Matches any non-empty tuple.
|
|
188
191
|
- [`NonEmptyString`](source/non-empty-string.d.ts) - Matches any non-empty string.
|
|
189
192
|
- [`FindGlobalType`](source/find-global-type.d.ts) - Tries to find the type of a global with the given name.
|
|
@@ -191,6 +194,8 @@ Click the type names for complete docs.
|
|
|
191
194
|
- [`ConditionalSimplify`](source/conditional-simplify.d.ts) - Simplifies a type while including and/or excluding certain types from being simplified.
|
|
192
195
|
- [`ConditionalSimplifyDeep`](source/conditional-simplify-deep.d.ts) - Recursively simplifies a type while including and/or excluding certain types from being simplified.
|
|
193
196
|
- [`ExclusifyUnion`](source/exclusify-union.d.ts) - Ensure mutual exclusivity in object unions by adding other members’ keys as `?: never`.
|
|
197
|
+
- [`Optional`](source/optional.d.ts) - Create a type that represents either the value or `undefined`, while stripping `null` from the type.
|
|
198
|
+
- [`UnionMember`](source/union-member.d.ts) - Returns an arbitrary member of a union type.
|
|
194
199
|
|
|
195
200
|
### Type Guard
|
|
196
201
|
|
|
@@ -266,6 +271,7 @@ Click the type names for complete docs.
|
|
|
266
271
|
- [`ExtractRestElement`](source/extract-rest-element.d.ts) - Extract the [`rest`](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) element type from an array.
|
|
267
272
|
- [`ExcludeRestElement`](source/exclude-rest-element.d.ts) - Create a tuple with the [`rest`](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) element removed.
|
|
268
273
|
- [`ArrayReverse`](source/array-reverse.d.ts) - Reverse the order of elements in a tuple type.
|
|
274
|
+
- [`ArrayLength`](source/array-length.d.ts) - Return the length of an array. Equivalent to `T['length']` where `T` extends any array.
|
|
269
275
|
|
|
270
276
|
### Numeric
|
|
271
277
|
|
|
@@ -281,7 +287,7 @@ Click the type names for complete docs.
|
|
|
281
287
|
- [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.
|
|
282
288
|
- [`IsNegative`](source/numeric.d.ts) - Returns a boolean for whether the given number is a negative number.
|
|
283
289
|
- [`IsFloat`](source/is-float.d.ts) - Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
|
|
284
|
-
- [`IsInteger`](source/is-integer.d.ts) - Returns a boolean for whether the given number is
|
|
290
|
+
- [`IsInteger`](source/is-integer.d.ts) - Returns a boolean for whether the given number is an integer, like `-5`, `1.0` or `100`.
|
|
285
291
|
- [`GreaterThan`](source/greater-than.d.ts) - Returns a boolean for whether a given number is greater than another number.
|
|
286
292
|
- [`GreaterThanOrEqual`](source/greater-than-or-equal.d.ts) - Returns a boolean for whether a given number is greater than or equal to another number.
|
|
287
293
|
- [`LessThan`](source/less-than.d.ts) - Returns a boolean for whether a given number is less than another number.
|
|
@@ -319,6 +325,7 @@ Click the type names for complete docs.
|
|
|
319
325
|
- [`ExtendsStrict`](source/extends-strict.d.ts) - A stricter, non-distributive version of `extends` for checking whether one type is assignable to another.
|
|
320
326
|
- [`ExtractStrict`](source/extract-strict.d.ts) - A stricter version of `Extract<T, U>` that ensures every member of `U` can successfully extract something from `T`.
|
|
321
327
|
- [`ExcludeStrict`](source/exclude-strict.d.ts) - A stricter version of `Exclude<T, U>` that ensures every member of `U` can successfully exclude something from `T`.
|
|
328
|
+
- [`ExcludeExactly`](source/exclude-exactly.d.ts) - A stricter version of `Exclude<T, U>` that excludes types only when they are exactly identical.
|
|
322
329
|
|
|
323
330
|
## Declined types
|
|
324
331
|
|
|
@@ -353,8 +360,11 @@ Click the type names for complete docs.
|
|
|
353
360
|
- `PickByTypes` - See [`ConditionalPick`](source/conditional-pick.d.ts)
|
|
354
361
|
- `HomomorphicOmit` - See [`Except`](source/except.d.ts)
|
|
355
362
|
- `IfAny`, `IfNever`, `If*` - See [`If`](source/if.d.ts)
|
|
363
|
+
- `Maybe`, `Option` - See [`Optional`](source/optional.d.ts)
|
|
356
364
|
- `MaybePromise` - See [`Promisable`](source/promisable.d.ts)
|
|
357
365
|
- `ReadonlyTuple` - See [`TupleOf`](source/tuple-of.d.ts)
|
|
366
|
+
- `LastOfUnion` - See [`UnionMember`](source/union-member.d.ts)
|
|
367
|
+
- `FirstOfUnion` - See [`UnionMember`](source/union-member.d.ts)
|
|
358
368
|
|
|
359
369
|
## Tips
|
|
360
370
|
|
package/source/all-extend.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type {If} from './if.d.ts';
|
|
2
1
|
import type {CollapseRestElement} from './internal/array.d.ts';
|
|
3
2
|
import type {ApplyDefaultOptions} from './internal/object.d.ts';
|
|
4
3
|
import type {IfNotAnyOrNever, Not} from './internal/type.d.ts';
|
|
@@ -104,17 +103,17 @@ type B = AllExtend<[1?, 2?, 3?], number | undefined>;
|
|
|
104
103
|
export type AllExtend<TArray extends UnknownArray, Type, Options extends AllExtendOptions = {}> =
|
|
105
104
|
_AllExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<AllExtendOptions, DefaultAllExtendOptions, Options>>;
|
|
106
105
|
|
|
107
|
-
type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray,
|
|
106
|
+
type _AllExtend<TArray extends UnknownArray, Type, Options extends Required<AllExtendOptions>> = IfNotAnyOrNever<TArray,
|
|
108
107
|
TArray extends readonly [infer First, ...infer Rest]
|
|
109
108
|
? IsNever<First> extends true
|
|
110
|
-
? Or<IsNever<Type>, Not<Options['strictNever']>> extends true
|
|
111
|
-
// If target `Type` is also `never`
|
|
109
|
+
? Or<Or<IsNever<Type>, IsAny<Type>>, Not<Options['strictNever']>> extends true
|
|
110
|
+
// If target `Type` is also `never`, or is `any`, or `strictNever` is disabled, recurse further.
|
|
112
111
|
? _AllExtend<Rest, Type, Options>
|
|
113
112
|
: false
|
|
114
113
|
: First extends Type
|
|
115
114
|
? _AllExtend<Rest, Type, Options>
|
|
116
115
|
: false
|
|
117
|
-
: true
|
|
118
|
-
|
|
116
|
+
: true,
|
|
117
|
+
false, false>;
|
|
119
118
|
|
|
120
119
|
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type {AllExtend} from './all-extend.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether all of the given elements are `true`.
|
|
5
|
+
|
|
6
|
+
Use-cases:
|
|
7
|
+
- Check if all conditions in a list of booleans are met.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {AndAll} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type TTT = AndAll<[true, true, true]>;
|
|
14
|
+
//=> true
|
|
15
|
+
|
|
16
|
+
type TTF = AndAll<[true, true, false]>;
|
|
17
|
+
//=> false
|
|
18
|
+
|
|
19
|
+
type TFT = AndAll<[true, false, true]>;
|
|
20
|
+
//=> false
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Note: When `boolean` is passed as an element, it is distributed into separate cases, and the final result is a union of those cases.
|
|
24
|
+
For example, `AndAll<[true, boolean]>` expands to `AndAll<[true, true]> | AndAll<[true, false]>`, which simplifies to `true | false` (i.e., `boolean`).
|
|
25
|
+
|
|
26
|
+
@example
|
|
27
|
+
```
|
|
28
|
+
import type {AndAll} from 'type-fest';
|
|
29
|
+
|
|
30
|
+
type A = AndAll<[true, boolean]>;
|
|
31
|
+
//=> boolean
|
|
32
|
+
|
|
33
|
+
type B = AndAll<[false, boolean]>;
|
|
34
|
+
//=> false
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Note: If any of the elements is `never`, the result becomes `false`.
|
|
38
|
+
|
|
39
|
+
@example
|
|
40
|
+
```
|
|
41
|
+
import type {AndAll} from 'type-fest';
|
|
42
|
+
|
|
43
|
+
type A = AndAll<[true, true, never]>;
|
|
44
|
+
//=> false
|
|
45
|
+
|
|
46
|
+
type B = AndAll<[false, never, never]>;
|
|
47
|
+
//=> false
|
|
48
|
+
|
|
49
|
+
type C = AndAll<[never, never, never]>;
|
|
50
|
+
//=> false
|
|
51
|
+
|
|
52
|
+
type D = AndAll<[boolean, true, never]>;
|
|
53
|
+
//=> false
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Note: If `any` is passed as an element, it is treated as `boolean` and the result is computed accordingly.
|
|
57
|
+
|
|
58
|
+
@example
|
|
59
|
+
```
|
|
60
|
+
import type {AndAll} from 'type-fest';
|
|
61
|
+
|
|
62
|
+
type A = AndAll<[false, any]>;
|
|
63
|
+
//=> false
|
|
64
|
+
|
|
65
|
+
type B = AndAll<[true, any]>;
|
|
66
|
+
//=> boolean
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Note: `AndAll<[]>` evaluates to `true` due to the concept of [vacuous truth](https://en.wikipedia.org/wiki/Logical_conjunction#:~:text=In%20keeping%20with%20the%20concept%20of%20vacuous%20truth%2C%20when%20conjunction%20is%20defined%20as%20an%20operator%20or%20function%20of%20arbitrary%20arity%2C%20the%20empty%20conjunction%20(AND%2Ding%20over%20an%20empty%20set%20of%20operands)%20is%20often%20defined%20as%20having%20the%20result%20true.), i.e., there are no `false` elements in an empty tuple.
|
|
70
|
+
|
|
71
|
+
@see {@link And}
|
|
72
|
+
@see {@link OrAll}
|
|
73
|
+
*/
|
|
74
|
+
export type AndAll<T extends readonly boolean[]> = AllExtend<T, true>;
|
|
75
|
+
|
|
76
|
+
export {};
|
package/source/and.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {AndAll} from './and-all.d.ts';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Returns a boolean for whether two given types are both true.
|
|
@@ -73,9 +73,10 @@ type G = And<never, never>;
|
|
|
73
73
|
//=> false
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
+
@see {@link AndAll}
|
|
76
77
|
@see {@link Or}
|
|
77
78
|
@see {@link Xor}
|
|
78
79
|
*/
|
|
79
|
-
export type And<A extends boolean, B extends boolean> =
|
|
80
|
+
export type And<A extends boolean, B extends boolean> = AndAll<[A, B]>;
|
|
80
81
|
|
|
81
82
|
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Return the length of an array. Equivalent to `T['length']` where `T` extends any array.
|
|
3
|
+
|
|
4
|
+
Tuples resolve to numeric literals, while non-tuples resolve to the `number` type.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {ArrayLength} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type TupleLength = ArrayLength<[1, 2, 3]>;
|
|
11
|
+
//=> 3
|
|
12
|
+
|
|
13
|
+
type TupleWithOptionalMembersLength = ArrayLength<[1, 2, number?]>;
|
|
14
|
+
//=> 2 | 3
|
|
15
|
+
|
|
16
|
+
type NonTupleArrayLength = ArrayLength<string[]>;
|
|
17
|
+
//=> number
|
|
18
|
+
|
|
19
|
+
type TupleWithRestElementLength = ArrayLength<[1, 2, ...string[]]>;
|
|
20
|
+
//=> number
|
|
21
|
+
|
|
22
|
+
// Distinguish between arrays with fixed and non-fixed lengths
|
|
23
|
+
type IsFixedLengthArray<T extends readonly unknown[]> = number extends ArrayLength<T> ? false : true;
|
|
24
|
+
|
|
25
|
+
type A = IsFixedLengthArray<number[]>;
|
|
26
|
+
//=> false
|
|
27
|
+
|
|
28
|
+
type B = IsFixedLengthArray<[1, 2, 3]>;
|
|
29
|
+
//=> true
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
@category Array
|
|
33
|
+
*/
|
|
34
|
+
export type ArrayLength<T extends readonly unknown[]> = T['length'];
|
|
35
|
+
|
|
36
|
+
export {};
|
package/source/array-splice.d.ts
CHANGED
|
@@ -50,10 +50,10 @@ Split the given array `T` by the given `SplitIndex`.
|
|
|
50
50
|
@example
|
|
51
51
|
```
|
|
52
52
|
type A = SplitArrayByIndex<[1, 2, 3, 4], 2>;
|
|
53
|
-
|
|
53
|
+
//=> [[1, 2], [3, 4]];
|
|
54
54
|
|
|
55
55
|
type B = SplitArrayByIndex<[1, 2, 3, 4], 0>;
|
|
56
|
-
|
|
56
|
+
//=> [[], [1, 2, 3, 4]];
|
|
57
57
|
```
|
|
58
58
|
*/
|
|
59
59
|
type SplitArrayByIndex<T extends UnknownArray, SplitIndex extends number> =
|
|
@@ -74,7 +74,7 @@ type BooleanPick = ConditionalPickDeep<Example, boolean | undefined>;
|
|
|
74
74
|
//=> {c: {e: {g?: boolean}; j: boolean}}
|
|
75
75
|
|
|
76
76
|
type NumberPick = ConditionalPickDeep<Example, number>;
|
|
77
|
-
//=>
|
|
77
|
+
//=> never
|
|
78
78
|
|
|
79
79
|
type StringOrBooleanPick = ConditionalPickDeep<Example, string | boolean>;
|
|
80
80
|
//=> {
|
|
@@ -99,11 +99,13 @@ export type ConditionalPickDeep<
|
|
|
99
99
|
Type,
|
|
100
100
|
Condition,
|
|
101
101
|
Options extends ConditionalPickDeepOptions = {},
|
|
102
|
-
> = _ConditionalPickDeep<
|
|
102
|
+
> = _NeverIfEmpty<_ConditionalPickDeep<
|
|
103
103
|
Type,
|
|
104
104
|
Condition,
|
|
105
105
|
ApplyDefaultOptions<ConditionalPickDeepOptions, DefaultConditionalPickDeepOptions, Options>
|
|
106
|
-
|
|
106
|
+
>>;
|
|
107
|
+
|
|
108
|
+
type _NeverIfEmpty<Type> = Type extends EmptyObject ? never : Type;
|
|
107
109
|
|
|
108
110
|
type _ConditionalPickDeep<
|
|
109
111
|
Type,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {ConditionalKeys} from './conditional-keys.d.ts';
|
|
2
|
+
import type {IsNever} from './is-never.d.ts';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
Pick keys from the shape that matches the given `Condition`.
|
|
@@ -38,9 +39,10 @@ type StringKeysOnly = ConditionalPick<Example, string>;
|
|
|
38
39
|
|
|
39
40
|
@category Object
|
|
40
41
|
*/
|
|
41
|
-
export type ConditionalPick<Base, Condition> =
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
export type ConditionalPick<Base, Condition> = ConditionalKeys<Base, Condition> extends infer Keys
|
|
43
|
+
? IsNever<Keys> extends true
|
|
44
|
+
? never
|
|
45
|
+
: Pick<Base, Keys & keyof Base>
|
|
46
|
+
: never;
|
|
45
47
|
|
|
46
48
|
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type {IsNever} from './is-never.d.ts';
|
|
2
|
+
import type {IsAny} from './is-any.d.ts';
|
|
3
|
+
import type {If} from './if.d.ts';
|
|
4
|
+
import type {IsEqual} from './is-equal.d.ts';
|
|
5
|
+
import type {IfNotAnyOrNever} from './internal/type.d.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
A stricter version of `Exclude<T, U>` that excludes types only when they are exactly identical.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {ExcludeExactly} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type TestExclude1 = Exclude<'a' | 'b' | 'c' | 1 | 2 | 3, string>;
|
|
15
|
+
//=> 1 | 2 | 3
|
|
16
|
+
|
|
17
|
+
type TestExcludeExactly1 = ExcludeExactly<'a' | 'b' | 'c' | 1 | 2 | 3, string>;
|
|
18
|
+
//=> 'a' | 'b' | 'c' | 1 | 2 | 3
|
|
19
|
+
|
|
20
|
+
type TestExclude2 = Exclude<'a' | 'b' | 'c' | 1 | 2 | 3, any>;
|
|
21
|
+
//=> never
|
|
22
|
+
|
|
23
|
+
type TestExcludeExactly2 = ExcludeExactly<'a' | 'b' | 'c' | 1 | 2 | 3, any>;
|
|
24
|
+
//=> 'a' | 'b' | 'c' | 1 | 2 | 3
|
|
25
|
+
|
|
26
|
+
type TestExclude3 = Exclude<{a: string} | {a: string; b: string}, {a: string}>;
|
|
27
|
+
//=> never
|
|
28
|
+
|
|
29
|
+
type TestExcludeExactly3 = ExcludeExactly<{a: string} | {a: string; b: string}, {a: string}>;
|
|
30
|
+
//=> {a: string; b: string}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
@category Improved Built-in
|
|
34
|
+
*/
|
|
35
|
+
export type ExcludeExactly<Union, Delete> =
|
|
36
|
+
IfNotAnyOrNever<
|
|
37
|
+
Union,
|
|
38
|
+
_ExcludeExactly<Union, Delete>,
|
|
39
|
+
// If `Union` is `any`, then if `Delete` is `any`, return `never`, else return `Union`.
|
|
40
|
+
If<IsAny<Delete>, never, Union>,
|
|
41
|
+
// If `Union` is `never`, then if `Delete` is `never`, return `never`, else return `Union`.
|
|
42
|
+
If<IsNever<Delete>, never, Union>
|
|
43
|
+
>;
|
|
44
|
+
|
|
45
|
+
type _ExcludeExactly<Union, Delete> =
|
|
46
|
+
IfNotAnyOrNever<Delete,
|
|
47
|
+
Union extends unknown // For distributing `Union`
|
|
48
|
+
? [Delete extends unknown // For distributing `Delete`
|
|
49
|
+
? If<IsEqual<Union, Delete>, true, never>
|
|
50
|
+
: never] extends [never] ? Union : never
|
|
51
|
+
: never,
|
|
52
|
+
// If `Delete` is `any` or `never`, then return `Union`,
|
|
53
|
+
// because `Union` cannot be `any` or `never` here.
|
|
54
|
+
Union, Union
|
|
55
|
+
>;
|
|
56
|
+
|
|
57
|
+
export {};
|
|
@@ -16,9 +16,42 @@ type B = GreaterThanOrEqual<1, 1>;
|
|
|
16
16
|
type C = GreaterThanOrEqual<1, 5>;
|
|
17
17
|
//=> false
|
|
18
18
|
```
|
|
19
|
+
|
|
20
|
+
Note: If either argument is the non-literal `number` type, the result is `boolean`.
|
|
21
|
+
|
|
22
|
+
@example
|
|
23
|
+
```
|
|
24
|
+
import type {GreaterThanOrEqual} from 'type-fest';
|
|
25
|
+
|
|
26
|
+
type A = GreaterThanOrEqual<number, 1>;
|
|
27
|
+
//=> boolean
|
|
28
|
+
|
|
29
|
+
type B = GreaterThanOrEqual<1, number>;
|
|
30
|
+
//=> boolean
|
|
31
|
+
|
|
32
|
+
type C = GreaterThanOrEqual<number, number>;
|
|
33
|
+
//=> boolean
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
@example
|
|
37
|
+
```
|
|
38
|
+
import type {GreaterThanOrEqual} from 'type-fest';
|
|
39
|
+
|
|
40
|
+
// Use `GreaterThanOrEqual` to constrain a function parameter to non-negative numbers.
|
|
41
|
+
declare function setNonNegative<N extends number>(value: GreaterThanOrEqual<N, 0> extends true ? N : never): void;
|
|
42
|
+
|
|
43
|
+
setNonNegative(0); // ✅ Allowed
|
|
44
|
+
setNonNegative(1); // ✅ Allowed
|
|
45
|
+
|
|
46
|
+
// @ts-expect-error
|
|
47
|
+
setNonNegative(-1);
|
|
48
|
+
|
|
49
|
+
// @ts-expect-error
|
|
50
|
+
setNonNegative(-2);
|
|
51
|
+
```
|
|
19
52
|
*/
|
|
20
53
|
export type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
|
|
21
|
-
?
|
|
54
|
+
? boolean
|
|
22
55
|
: A extends number // For distributing `A`
|
|
23
56
|
? B extends number // For distributing `B`
|
|
24
57
|
? A extends B
|
package/source/greater-than.d.ts
CHANGED
|
@@ -20,12 +20,45 @@ type B = GreaterThan<1, 1>;
|
|
|
20
20
|
type C = GreaterThan<1, 5>;
|
|
21
21
|
//=> false
|
|
22
22
|
```
|
|
23
|
+
|
|
24
|
+
Note: If either argument is the non-literal `number` type, the result is `boolean`.
|
|
25
|
+
|
|
26
|
+
@example
|
|
27
|
+
```
|
|
28
|
+
import type {GreaterThan} from 'type-fest';
|
|
29
|
+
|
|
30
|
+
type A = GreaterThan<number, 1>;
|
|
31
|
+
//=> boolean
|
|
32
|
+
|
|
33
|
+
type B = GreaterThan<1, number>;
|
|
34
|
+
//=> boolean
|
|
35
|
+
|
|
36
|
+
type C = GreaterThan<number, number>;
|
|
37
|
+
//=> boolean
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
@example
|
|
41
|
+
```
|
|
42
|
+
import type {GreaterThan} from 'type-fest';
|
|
43
|
+
|
|
44
|
+
// Use `GreaterThan` to constrain a function parameter to positive numbers.
|
|
45
|
+
declare function setPositive<N extends number>(value: GreaterThan<N, 0> extends true ? N : never): void;
|
|
46
|
+
|
|
47
|
+
setPositive(1); // ✅ Allowed
|
|
48
|
+
setPositive(2); // ✅ Allowed
|
|
49
|
+
|
|
50
|
+
// @ts-expect-error
|
|
51
|
+
setPositive(0);
|
|
52
|
+
|
|
53
|
+
// @ts-expect-error
|
|
54
|
+
setPositive(-1);
|
|
55
|
+
```
|
|
23
56
|
*/
|
|
24
57
|
export type GreaterThan<A extends number, B extends number> =
|
|
25
58
|
A extends number // For distributing `A`
|
|
26
59
|
? B extends number // For distributing `B`
|
|
27
60
|
? number extends A | B
|
|
28
|
-
?
|
|
61
|
+
? boolean
|
|
29
62
|
: [
|
|
30
63
|
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
31
64
|
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
@@ -4,13 +4,6 @@ import type {OptionalKeysOf} from '../optional-keys-of.d.ts';
|
|
|
4
4
|
import type {UnknownArray} from '../unknown-array.d.ts';
|
|
5
5
|
import type {IsExactOptionalPropertyTypesEnabled, IfNotAnyOrNever} from './type.d.ts';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
Infer the length of the given array `<T>`.
|
|
9
|
-
|
|
10
|
-
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
11
|
-
*/
|
|
12
|
-
type ArrayLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never;
|
|
13
|
-
|
|
14
7
|
/**
|
|
15
8
|
Matches any unknown array or tuple.
|
|
16
9
|
*/
|
|
@@ -23,7 +23,7 @@ type Union = TupleLength<[] | [1, 2, 3] | number[]>;
|
|
|
23
23
|
*/
|
|
24
24
|
export type TupleLength<T extends UnknownArray> =
|
|
25
25
|
// `extends unknown` is used to convert `T` (if `T` is a union type) to
|
|
26
|
-
// a [distributive
|
|
26
|
+
// a [distributive conditional type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types))
|
|
27
27
|
T extends unknown
|
|
28
28
|
? number extends T['length']
|
|
29
29
|
? never // Return never if the given type is an non-flexed-length array like `Array<string>`
|
|
@@ -3,6 +3,7 @@ import type {IsAny} from '../is-any.d.ts';
|
|
|
3
3
|
import type {IsNever} from '../is-never.d.ts';
|
|
4
4
|
import type {Primitive} from '../primitive.d.ts';
|
|
5
5
|
import type {UnknownArray} from '../unknown-array.d.ts';
|
|
6
|
+
import type {UnionToIntersection} from '../union-to-intersection.d.ts';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
package/source/is-equal.d.ts
CHANGED
|
@@ -16,9 +16,45 @@ type B = LessThanOrEqual<1, 1>;
|
|
|
16
16
|
type C = LessThanOrEqual<1, 5>;
|
|
17
17
|
//=> true
|
|
18
18
|
```
|
|
19
|
+
|
|
20
|
+
Note: If either argument is the non-literal `number` type, the result is `boolean`.
|
|
21
|
+
|
|
22
|
+
@example
|
|
23
|
+
```
|
|
24
|
+
import type {LessThanOrEqual} from 'type-fest';
|
|
25
|
+
|
|
26
|
+
type A = LessThanOrEqual<number, 1>;
|
|
27
|
+
//=> boolean
|
|
28
|
+
|
|
29
|
+
type B = LessThanOrEqual<1, number>;
|
|
30
|
+
//=> boolean
|
|
31
|
+
|
|
32
|
+
type C = LessThanOrEqual<number, number>;
|
|
33
|
+
//=> boolean
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
@example
|
|
37
|
+
```
|
|
38
|
+
import type {LessThanOrEqual} from 'type-fest';
|
|
39
|
+
|
|
40
|
+
// Use `LessThanOrEqual` to constrain a function parameter to non-positive numbers.
|
|
41
|
+
declare function setNonPositive<N extends number>(value: LessThanOrEqual<N, 0> extends true ? N : never): void;
|
|
42
|
+
|
|
43
|
+
setNonPositive(0); // ✅ Allowed
|
|
44
|
+
setNonPositive(-1); // ✅ Allowed
|
|
45
|
+
|
|
46
|
+
// @ts-expect-error
|
|
47
|
+
setNonPositive(1);
|
|
48
|
+
|
|
49
|
+
// @ts-expect-error
|
|
50
|
+
setNonPositive(2);
|
|
51
|
+
```
|
|
19
52
|
*/
|
|
20
|
-
export type LessThanOrEqual<A extends number, B extends number> =
|
|
21
|
-
|
|
22
|
-
|
|
53
|
+
export type LessThanOrEqual<A extends number, B extends number> =
|
|
54
|
+
GreaterThan<A, B> extends infer Result
|
|
55
|
+
? Result extends true
|
|
56
|
+
? false
|
|
57
|
+
: true
|
|
58
|
+
: never; // Should never happen
|
|
23
59
|
|
|
24
60
|
export {};
|
package/source/less-than.d.ts
CHANGED
|
@@ -16,10 +16,42 @@ type B = LessThan<1, 1>;
|
|
|
16
16
|
type C = LessThan<1, 5>;
|
|
17
17
|
//=> true
|
|
18
18
|
```
|
|
19
|
+
|
|
20
|
+
Note: If either argument is the non-literal `number` type, the result is `boolean`.
|
|
21
|
+
|
|
22
|
+
@example
|
|
23
|
+
```
|
|
24
|
+
import type {LessThan} from 'type-fest';
|
|
25
|
+
|
|
26
|
+
type A = LessThan<number, 1>;
|
|
27
|
+
//=> boolean
|
|
28
|
+
|
|
29
|
+
type B = LessThan<1, number>;
|
|
30
|
+
//=> boolean
|
|
31
|
+
|
|
32
|
+
type C = LessThan<number, number>;
|
|
33
|
+
//=> boolean
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
@example
|
|
37
|
+
```
|
|
38
|
+
import type {LessThan} from 'type-fest';
|
|
39
|
+
|
|
40
|
+
// Use `LessThan` to constrain a function parameter to negative numbers.
|
|
41
|
+
declare function setNegative<N extends number>(value: LessThan<N, 0> extends true ? N : never): void;
|
|
42
|
+
|
|
43
|
+
setNegative(-1); // ✅ Allowed
|
|
44
|
+
setNegative(-2); // ✅ Allowed
|
|
45
|
+
|
|
46
|
+
// @ts-expect-error
|
|
47
|
+
setNegative(0);
|
|
48
|
+
|
|
49
|
+
// @ts-expect-error
|
|
50
|
+
setNegative(1);
|
|
51
|
+
```
|
|
19
52
|
*/
|
|
20
|
-
export type LessThan<A extends number, B extends number> =
|
|
21
|
-
|
|
22
|
-
: GreaterThanOrEqual<A, B> extends infer Result
|
|
53
|
+
export type LessThan<A extends number, B extends number> =
|
|
54
|
+
GreaterThanOrEqual<A, B> extends infer Result
|
|
23
55
|
? Result extends true
|
|
24
56
|
? false
|
|
25
57
|
: true
|
package/source/merge.d.ts
CHANGED
|
@@ -12,6 +12,31 @@ type SimpleMerge<Destination, Source> = Simplify<{
|
|
|
12
12
|
/**
|
|
13
13
|
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
14
14
|
|
|
15
|
+
This is different from the TypeScript `&` (intersection) operator. With `&`, conflicting property types are intersected, which often results in `never`. For example, `{a: string} & {a: number}` makes `a` become `string & number`, which resolves to `never`. With `Merge`, the second type's keys cleanly override the first, so `Merge<{a: string}, {a: number}>` gives `{a: number}` as expected. `Merge` also produces a flattened type (via `Simplify`), making it more readable in IDE tooltips compared to `A & B`.
|
|
16
|
+
|
|
17
|
+
@example
|
|
18
|
+
```
|
|
19
|
+
import type {Merge} from 'type-fest';
|
|
20
|
+
|
|
21
|
+
type Foo = {
|
|
22
|
+
a: string;
|
|
23
|
+
b: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type Bar = {
|
|
27
|
+
a: number; // Conflicts with Foo['a']
|
|
28
|
+
c: boolean;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// With `&`, `a` becomes `string & number` which is `never`. Not what you want.
|
|
32
|
+
type WithIntersection = (Foo & Bar)['a'];
|
|
33
|
+
//=> never
|
|
34
|
+
|
|
35
|
+
// With `Merge`, `a` is cleanly overridden to `number`.
|
|
36
|
+
type WithMerge = Merge<Foo, Bar>['a'];
|
|
37
|
+
//=> number
|
|
38
|
+
```
|
|
39
|
+
|
|
15
40
|
@example
|
|
16
41
|
```
|
|
17
42
|
import type {Merge} from 'type-fest';
|
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
|
|
|
@@ -119,7 +118,7 @@ P extends `${infer RecordKeyInPath}.${infer SubPath}`
|
|
|
119
118
|
? IsNever<Key> extends true
|
|
120
119
|
? ObjectT
|
|
121
120
|
: Key extends PropertyKey
|
|
122
|
-
?
|
|
121
|
+
? Omit<ObjectT, Key>
|
|
123
122
|
: ObjectT
|
|
124
123
|
: ObjectT;
|
|
125
124
|
|
|
@@ -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,5 +1,4 @@
|
|
|
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
4
|
Returns a boolean for whether either of two given types is true.
|
|
@@ -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/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.
|
|
@@ -36,24 +37,15 @@ type Configuration = {
|
|
|
36
37
|
};
|
|
37
38
|
|
|
38
39
|
type NameConfig = PickDeep<Configuration, 'userConfig.name'>;
|
|
39
|
-
|
|
40
|
-
// userConfig: {
|
|
41
|
-
// name: string;
|
|
42
|
-
// }
|
|
43
|
-
// };
|
|
40
|
+
//=> {userConfig: {name: string}}
|
|
44
41
|
|
|
45
42
|
// Supports optional properties
|
|
46
43
|
type User = PickDeep<PartialDeep<Configuration>, 'userConfig.name' | 'userConfig.age'>;
|
|
47
|
-
|
|
48
|
-
// userConfig?: {
|
|
49
|
-
// name?: string;
|
|
50
|
-
// age?: number;
|
|
51
|
-
// };
|
|
52
|
-
// };
|
|
44
|
+
//=> {userConfig?: {name?: string; age?: number}}
|
|
53
45
|
|
|
54
46
|
// Supports array
|
|
55
47
|
type AddressConfig = PickDeep<Configuration, 'userConfig.address.0'>;
|
|
56
|
-
|
|
48
|
+
//=> {
|
|
57
49
|
// userConfig: {
|
|
58
50
|
// address: [{
|
|
59
51
|
// city1: string;
|
|
@@ -64,14 +56,7 @@ type AddressConfig = PickDeep<Configuration, 'userConfig.address.0'>;
|
|
|
64
56
|
|
|
65
57
|
// Supports recurse into array
|
|
66
58
|
type Street = PickDeep<Configuration, 'userConfig.address.1.street2'>;
|
|
67
|
-
|
|
68
|
-
// userConfig: {
|
|
69
|
-
// address: [
|
|
70
|
-
// unknown,
|
|
71
|
-
// {street2: string}
|
|
72
|
-
// ];
|
|
73
|
-
// };
|
|
74
|
-
// }
|
|
59
|
+
//=> {userConfig: {address: [unknown, {street2: string}]}}
|
|
75
60
|
```
|
|
76
61
|
|
|
77
62
|
@category Object
|
|
@@ -86,7 +71,7 @@ export type PickDeep<T, PathUnion extends Paths<T>> =
|
|
|
86
71
|
}[PathUnion]
|
|
87
72
|
>
|
|
88
73
|
: T extends object
|
|
89
|
-
?
|
|
74
|
+
? SimplifyDeep<UnionToIntersection<{
|
|
90
75
|
[P in PathUnion]: InternalPickDeep<T, P>;
|
|
91
76
|
}[PathUnion]>>
|
|
92
77
|
: never;
|
|
@@ -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
|
@@ -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
|
|
@@ -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]
|
package/source/set-readonly.d.ts
CHANGED
|
@@ -18,11 +18,7 @@ type Foo = {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
type SomeReadonly = SetReadonly<Foo, 'b' | 'c'>;
|
|
21
|
-
|
|
22
|
-
// a: number;
|
|
23
|
-
// readonly b: string; // Was already readonly and still is.
|
|
24
|
-
// readonly c: boolean; // Is now readonly.
|
|
25
|
-
// }
|
|
21
|
+
//=> {a: number; readonly b: string; readonly c: boolean}
|
|
26
22
|
```
|
|
27
23
|
|
|
28
24
|
@category Object
|
package/source/set-required.d.ts
CHANGED
|
@@ -21,11 +21,7 @@ type Foo = {
|
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
|
|
24
|
-
|
|
25
|
-
// a?: number;
|
|
26
|
-
// b: string; // Was already required and still is.
|
|
27
|
-
// c: boolean; // Is now required.
|
|
28
|
-
// }
|
|
24
|
+
//=> {a?: number; b: string; c: boolean}
|
|
29
25
|
|
|
30
26
|
// Set specific indices in an array to be required.
|
|
31
27
|
type ArrayExample = SetRequired<[number?, number?, number?], 0 | 1>;
|
|
@@ -89,7 +89,7 @@ function displayPetInfoWithSharedUnionFieldsDeep(petInfo: SharedUnionFieldsDeep<
|
|
|
89
89
|
export type SharedUnionFieldsDeep<Union, Options extends SharedUnionFieldsDeepOptions = {}> =
|
|
90
90
|
ApplyDefaultOptions<SharedUnionFieldsDeepOptions, DefaultSharedUnionFieldsDeepOptions, Options> extends infer OptionsWithDefaults extends Required<SharedUnionFieldsDeepOptions>
|
|
91
91
|
// `Union extends` will convert `Union`
|
|
92
|
-
// to a [distributive
|
|
92
|
+
// to a [distributive conditional type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
93
93
|
// But this is not what we want, so we need to wrap `Union` with `[]` to prevent it.
|
|
94
94
|
? [Union] extends [NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>]
|
|
95
95
|
? Union
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type {CollapseRestElement} from './internal/array.d.ts';
|
|
2
|
+
import type {ApplyDefaultOptions} from './internal/object.d.ts';
|
|
3
|
+
import type {IfNotAnyOrNever, Not} from './internal/type.d.ts';
|
|
4
|
+
import type {IsAny} from './is-any.d.ts';
|
|
5
|
+
import type {IsNever} from './is-never.d.ts';
|
|
6
|
+
import type {Or} from './or.d.ts';
|
|
7
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
@see {@link SomeExtend}
|
|
11
|
+
*/
|
|
12
|
+
export type SomeExtendOptions = {
|
|
13
|
+
/**
|
|
14
|
+
Consider `never` elements to match the target type only if the target type itself is `never` (or `any`).
|
|
15
|
+
|
|
16
|
+
- When set to `true` (default), `never` is _not_ treated as a bottom type, instead, it is treated as a type that matches only itself (or `any`).
|
|
17
|
+
- When set to `false`, `never` is treated as a bottom type, and behaves as it normally would.
|
|
18
|
+
|
|
19
|
+
@default true
|
|
20
|
+
|
|
21
|
+
@example
|
|
22
|
+
```
|
|
23
|
+
import type {SomeExtend} from 'type-fest';
|
|
24
|
+
|
|
25
|
+
type A = SomeExtend<[1, 2, never], string, {strictNever: true}>;
|
|
26
|
+
//=> false
|
|
27
|
+
|
|
28
|
+
type B = SomeExtend<[1, 2, never], string, {strictNever: false}>;
|
|
29
|
+
//=> true
|
|
30
|
+
|
|
31
|
+
type C = SomeExtend<[1, never], never, {strictNever: true}>;
|
|
32
|
+
//=> true
|
|
33
|
+
|
|
34
|
+
type D = SomeExtend<[1, never], never, {strictNever: false}>;
|
|
35
|
+
//=> true
|
|
36
|
+
|
|
37
|
+
type E = SomeExtend<[never], any, {strictNever: true}>;
|
|
38
|
+
//=> true
|
|
39
|
+
|
|
40
|
+
type F = SomeExtend<[never], any, {strictNever: false}>;
|
|
41
|
+
//=> true
|
|
42
|
+
```
|
|
43
|
+
*/
|
|
44
|
+
strictNever?: boolean;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type DefaultSomeExtendOptions = {
|
|
48
|
+
strictNever: true;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
Returns a boolean for whether some element in an array type extends another type.
|
|
53
|
+
|
|
54
|
+
@example
|
|
55
|
+
```
|
|
56
|
+
import type {SomeExtend} from 'type-fest';
|
|
57
|
+
|
|
58
|
+
type A = SomeExtend<['1', '2', 3], number>;
|
|
59
|
+
//=> true
|
|
60
|
+
|
|
61
|
+
type B = SomeExtend<[1, 2, 3], string>;
|
|
62
|
+
//=> false
|
|
63
|
+
|
|
64
|
+
type C = SomeExtend<[string, number | string], number>;
|
|
65
|
+
//=> boolean
|
|
66
|
+
|
|
67
|
+
type D = SomeExtend<[true, boolean, true], false>;
|
|
68
|
+
//=> boolean
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Note: Behaviour of optional elements depend on the `exactOptionalPropertyTypes` compiler option. When the option is disabled, the target type must include `undefined` for a successful match.
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
// @exactOptionalPropertyTypes: true
|
|
75
|
+
import type {SomeExtend} from 'type-fest';
|
|
76
|
+
|
|
77
|
+
type A = SomeExtend<[1?, 2?, '3'?], string>;
|
|
78
|
+
//=> true
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
// @exactOptionalPropertyTypes: false
|
|
83
|
+
import type {SomeExtend} from 'type-fest';
|
|
84
|
+
|
|
85
|
+
type A = SomeExtend<[1?, 2?, '3'?], string>;
|
|
86
|
+
//=> boolean
|
|
87
|
+
|
|
88
|
+
type B = SomeExtend<[1?, 2?, '3'?], string | undefined>;
|
|
89
|
+
//=> true
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
@see {@link SomeExtendOptions}
|
|
93
|
+
|
|
94
|
+
@category Utilities
|
|
95
|
+
@category Array
|
|
96
|
+
*/
|
|
97
|
+
export type SomeExtend<TArray extends UnknownArray, Type, Options extends SomeExtendOptions = {}> =
|
|
98
|
+
_SomeExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<SomeExtendOptions, DefaultSomeExtendOptions, Options>>;
|
|
99
|
+
|
|
100
|
+
type _SomeExtend<TArray extends UnknownArray, Type, Options extends Required<SomeExtendOptions>> = IfNotAnyOrNever<TArray,
|
|
101
|
+
TArray extends readonly [infer First, ...infer Rest]
|
|
102
|
+
? IsNever<First> extends true
|
|
103
|
+
? Or<Or<IsNever<Type>, IsAny<Type>>, Not<Options['strictNever']>> extends true
|
|
104
|
+
// If target `Type` is also `never`, or is `any`, or `strictNever` is disabled, return `true`.
|
|
105
|
+
? true
|
|
106
|
+
: _SomeExtend<Rest, Type, Options>
|
|
107
|
+
: First extends Type
|
|
108
|
+
? true
|
|
109
|
+
: _SomeExtend<Rest, Type, Options>
|
|
110
|
+
: false,
|
|
111
|
+
false, false>;
|
|
112
|
+
|
|
113
|
+
export {};
|
package/source/spread.d.ts
CHANGED
|
@@ -41,11 +41,7 @@ const bar = {c: false};
|
|
|
41
41
|
const fooBar = {...foo, ...bar};
|
|
42
42
|
|
|
43
43
|
type FooBar = Spread<Foo, Bar>;
|
|
44
|
-
|
|
45
|
-
// a: number;
|
|
46
|
-
// b?: string | number | undefined;
|
|
47
|
-
// c: boolean;
|
|
48
|
-
// }
|
|
44
|
+
//=> {a: number; b?: string | number; c: boolean}
|
|
49
45
|
|
|
50
46
|
declare function baz(argument: FooBar): void;
|
|
51
47
|
|
package/source/tagged.d.ts
CHANGED
|
@@ -121,7 +121,7 @@ const moneyByAccountType: Record<UnwrapTagged<AccountType>, number> = {
|
|
|
121
121
|
// Without UnwrapTagged, the following expression would throw a type error.
|
|
122
122
|
const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
|
|
123
123
|
|
|
124
|
-
// Attempting to pass
|
|
124
|
+
// Attempting to pass a non-Tagged type to UnwrapTagged will raise a type error.
|
|
125
125
|
// @ts-expect-error
|
|
126
126
|
type WontWork = UnwrapTagged<string>;
|
|
127
127
|
```
|
|
@@ -239,7 +239,7 @@ const moneyByAccountType: Record<UnwrapOpaque<AccountType>, number> = {
|
|
|
239
239
|
// Without UnwrapOpaque, the following expression would throw a type error.
|
|
240
240
|
const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
|
|
241
241
|
|
|
242
|
-
// Attempting to pass
|
|
242
|
+
// Attempting to pass a non-Opaque type to UnwrapOpaque will raise a type error.
|
|
243
243
|
// @ts-expect-error
|
|
244
244
|
type WontWork = UnwrapOpaque<string>;
|
|
245
245
|
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type {UnionToIntersection} from './union-to-intersection.d.ts';
|
|
2
|
+
import type {IsNever} from './is-never.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Returns an arbitrary member of a union type.
|
|
6
|
+
|
|
7
|
+
Use-cases:
|
|
8
|
+
- Implementing recursive type functions that accept a union type.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {UnionMember, IsNever} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type UnionLength<T, Acc extends any[] = []> =
|
|
15
|
+
UnionMember<T> extends infer Member
|
|
16
|
+
? IsNever<Member> extends false
|
|
17
|
+
? UnionLength<Exclude<T, Member>, [...Acc, Member]>
|
|
18
|
+
: Acc['length']
|
|
19
|
+
: never;
|
|
20
|
+
|
|
21
|
+
type T1 = UnionLength<'foo' | 'bar' | 'baz'>;
|
|
22
|
+
//=> 3
|
|
23
|
+
|
|
24
|
+
type T2 = UnionLength<{a: string}>;
|
|
25
|
+
//=> 1
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- Picking an arbitrary member from a union
|
|
29
|
+
|
|
30
|
+
@example
|
|
31
|
+
```
|
|
32
|
+
import type {UnionMember, Primitive, LiteralToPrimitive} from 'type-fest';
|
|
33
|
+
|
|
34
|
+
type IsHomogenous<T extends Primitive> = [T] extends [LiteralToPrimitive<UnionMember<T>>] ? true : false;
|
|
35
|
+
|
|
36
|
+
type T1 = IsHomogenous<1 | 2 | 3 | 4>;
|
|
37
|
+
//=> true
|
|
38
|
+
|
|
39
|
+
type T2 = IsHomogenous<'foo' | 'bar'>;
|
|
40
|
+
//=> true
|
|
41
|
+
|
|
42
|
+
type T3 = IsHomogenous<'foo' | 'bar' | 1>;
|
|
43
|
+
//=> false
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Returns `never` when the input is `never`.
|
|
47
|
+
|
|
48
|
+
@example
|
|
49
|
+
```
|
|
50
|
+
import type {UnionMember} from 'type-fest';
|
|
51
|
+
|
|
52
|
+
type LastNever = UnionMember<never>;
|
|
53
|
+
//=> never
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
@category Type
|
|
57
|
+
*/
|
|
58
|
+
export type UnionMember<T> =
|
|
59
|
+
IsNever<T> extends true
|
|
60
|
+
? never
|
|
61
|
+
: UnionToIntersection<T extends any ? () => T : never> extends () => (infer R)
|
|
62
|
+
? R
|
|
63
|
+
: never;
|
|
64
|
+
|
|
65
|
+
export {};
|
|
@@ -1,19 +1,6 @@
|
|
|
1
|
+
import type {ExcludeExactly} from './exclude-exactly.d.ts';
|
|
1
2
|
import type {IsNever} from './is-never.d.ts';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
Returns the last element of a union type.
|
|
6
|
-
|
|
7
|
-
@example
|
|
8
|
-
```
|
|
9
|
-
type Last = LastOfUnion<1 | 2 | 3>;
|
|
10
|
-
//=> 3
|
|
11
|
-
```
|
|
12
|
-
*/
|
|
13
|
-
type LastOfUnion<T> =
|
|
14
|
-
UnionToIntersection<T extends any ? () => T : never> extends () => (infer R)
|
|
15
|
-
? R
|
|
16
|
-
: never;
|
|
3
|
+
import type {UnionMember} from './union-member.d.ts';
|
|
17
4
|
|
|
18
5
|
/**
|
|
19
6
|
Convert a union type into an unordered tuple type of its elements.
|
|
@@ -50,9 +37,9 @@ const petList = Object.keys(pets) as UnionToTuple<Pet>;
|
|
|
50
37
|
|
|
51
38
|
@category Array
|
|
52
39
|
*/
|
|
53
|
-
export type UnionToTuple<T, L =
|
|
40
|
+
export type UnionToTuple<T, L = UnionMember<T>> =
|
|
54
41
|
IsNever<T> extends false
|
|
55
|
-
? [...UnionToTuple<
|
|
42
|
+
? [...UnionToTuple<ExcludeExactly<T, L>>, L]
|
|
56
43
|
: [];
|
|
57
44
|
|
|
58
45
|
export {};
|
package/source/writable.d.ts
CHANGED
|
@@ -37,11 +37,7 @@ writableFoo.b[0] = 'new value'; // Will still fail as the value of property "b"
|
|
|
37
37
|
writableFoo.b = ['something']; // Will work as the "b" property itself is no longer readonly.
|
|
38
38
|
|
|
39
39
|
type SomeWritable = Writable<Foo, 'b' | 'c'>;
|
|
40
|
-
|
|
41
|
-
// readonly a: number;
|
|
42
|
-
// b: readonly string[]; // It's now writable. The type of the property remains unaffected.
|
|
43
|
-
// c: boolean; // It's now writable.
|
|
44
|
-
// }
|
|
40
|
+
//=> {readonly a: number; b: readonly string[]; c: boolean}
|
|
45
41
|
|
|
46
42
|
// Also supports array
|
|
47
43
|
const readonlyArray: readonly number[] = [1, 2, 3];
|