type-fest 5.2.0 → 5.3.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/package.json +5 -2
- package/source/all-union-fields.d.ts +8 -7
- package/source/array-slice.d.ts +29 -8
- package/source/array-splice.d.ts +8 -6
- package/source/arrayable.d.ts +1 -1
- package/source/async-return-type.d.ts +6 -3
- package/source/asyncify.d.ts +7 -16
- package/source/camel-case.d.ts +3 -3
- package/source/camel-cased-properties-deep.d.ts +4 -4
- package/source/camel-cased-properties.d.ts +2 -2
- package/source/characters.d.ts +8 -3
- package/source/conditional-except.d.ts +6 -6
- package/source/conditional-pick-deep.d.ts +2 -2
- package/source/conditional-pick.d.ts +6 -6
- package/source/delimiter-case.d.ts +3 -3
- package/source/delimiter-cased-properties-deep.d.ts +6 -6
- package/source/delimiter-cased-properties.d.ts +2 -2
- package/source/distributed-omit.d.ts +15 -10
- package/source/distributed-pick.d.ts +12 -7
- package/source/empty-object.d.ts +7 -4
- package/source/entries.d.ts +3 -3
- package/source/entry.d.ts +3 -3
- package/source/exact.d.ts +5 -3
- package/source/except.d.ts +5 -3
- package/source/exclude-rest-element.d.ts +1 -1
- package/source/exclude-strict.d.ts +4 -0
- package/source/extract-rest-element.d.ts +1 -1
- package/source/extract-strict.d.ts +4 -0
- package/source/find-global-type.d.ts +7 -5
- package/source/fixed-length-array.d.ts +8 -0
- package/source/get.d.ts +27 -28
- package/source/global-this.d.ts +2 -1
- package/source/greater-than-or-equal.d.ts +3 -3
- package/source/greater-than.d.ts +3 -3
- package/source/has-optional-keys.d.ts +2 -2
- package/source/has-readonly-keys.d.ts +2 -2
- package/source/has-required-keys.d.ts +10 -10
- package/source/has-writable-keys.d.ts +2 -2
- package/source/if.d.ts +3 -3
- package/source/int-closed-range.d.ts +17 -7
- package/source/int-range.d.ts +17 -7
- package/source/internal/array.d.ts +4 -4
- package/source/internal/numeric.d.ts +7 -7
- package/source/internal/object.d.ts +13 -12
- package/source/internal/string.d.ts +15 -15
- package/source/internal/tuple.d.ts +5 -5
- package/source/internal/type.d.ts +6 -6
- package/source/invariant-of.d.ts +26 -19
- package/source/is-any.d.ts +2 -2
- package/source/is-equal.d.ts +4 -8
- package/source/is-integer.d.ts +1 -1
- package/source/is-literal.d.ts +13 -13
- package/source/is-lowercase.d.ts +3 -3
- package/source/is-never.d.ts +35 -23
- package/source/is-null.d.ts +1 -1
- package/source/is-optional-key-of.d.ts +4 -4
- package/source/is-readonly-key-of.d.ts +4 -4
- package/source/is-required-key-of.d.ts +4 -4
- package/source/is-unknown.d.ts +12 -23
- package/source/is-uppercase.d.ts +3 -3
- package/source/is-writable-key-of.d.ts +4 -4
- package/source/iterable-element.d.ts +3 -3
- package/source/join.d.ts +27 -18
- package/source/jsonifiable.d.ts +5 -5
- package/source/jsonify.d.ts +7 -8
- package/source/kebab-case.d.ts +3 -3
- package/source/kebab-cased-properties-deep.d.ts +6 -6
- package/source/kebab-cased-properties.d.ts +2 -2
- package/source/last-array-element.d.ts +5 -9
- package/source/less-than-or-equal.d.ts +3 -3
- package/source/less-than.d.ts +3 -3
- package/source/literal-to-primitive-deep.d.ts +40 -7
- package/source/literal-union.d.ts +2 -2
- package/source/merge-deep.d.ts +6 -8
- package/source/merge-exclusive.d.ts +7 -4
- package/source/merge.d.ts +2 -2
- package/source/multidimensional-array.d.ts +7 -15
- package/source/multidimensional-readonly-array.d.ts +7 -19
- package/source/non-empty-object.d.ts +1 -0
- package/source/non-empty-string.d.ts +3 -1
- package/source/non-empty-tuple.d.ts +1 -0
- package/source/numeric.d.ts +6 -4
- package/source/omit-deep.d.ts +13 -12
- package/source/omit-index-signature.d.ts +17 -20
- package/source/optional-keys-of.d.ts +4 -4
- package/source/override-properties.d.ts +11 -6
- package/source/package-json.d.ts +12 -0
- package/source/partial-on-undefined-deep.d.ts +2 -2
- package/source/pascal-case.d.ts +1 -1
- package/source/pascal-cased-properties-deep.d.ts +4 -4
- package/source/pascal-cased-properties.d.ts +2 -2
- package/source/paths.d.ts +12 -0
- package/source/pick-deep.d.ts +2 -2
- package/source/promisable.d.ts +2 -2
- package/source/readonly-deep.d.ts +45 -12
- package/source/readonly-keys-of.d.ts +3 -3
- package/source/readonly-tuple.d.ts +5 -3
- package/source/remove-prefix.d.ts +2 -0
- package/source/replace.d.ts +3 -3
- package/source/require-all-or-none.d.ts +2 -2
- package/source/require-at-least-one.d.ts +1 -1
- package/source/require-exactly-one.d.ts +1 -1
- package/source/require-one-or-none.d.ts +3 -3
- package/source/required-keys-of.d.ts +10 -4
- package/source/set-field-type.d.ts +2 -2
- package/source/set-non-nullable.d.ts +1 -1
- package/source/set-optional.d.ts +1 -1
- package/source/set-parameter-type.d.ts +12 -6
- package/source/set-readonly.d.ts +1 -1
- package/source/set-required-deep.d.ts +4 -4
- package/source/set-required.d.ts +1 -1
- package/source/set-return-type.d.ts +3 -3
- package/source/shared-union-fields-deep.d.ts +5 -5
- package/source/shared-union-fields.d.ts +5 -5
- package/source/simplify-deep.d.ts +2 -3
- package/source/simplify.d.ts +2 -1
- package/source/single-key-object.d.ts +4 -8
- package/source/snake-case.d.ts +3 -3
- package/source/snake-cased-properties-deep.d.ts +5 -5
- package/source/snake-cased-properties.d.ts +2 -2
- package/source/split.d.ts +4 -4
- package/source/spread.d.ts +2 -6
- package/source/string-repeat.d.ts +2 -2
- package/source/string-slice.d.ts +6 -6
- package/source/stringified.d.ts +2 -2
- package/source/structured-cloneable.d.ts +11 -16
- package/source/subtract.d.ts +7 -7
- package/source/sum.d.ts +6 -6
- package/source/tagged-union.d.ts +2 -2
- package/source/tagged.d.ts +15 -13
- package/source/trim.d.ts +1 -1
- package/source/tuple-of.d.ts +2 -0
- package/source/tuple-to-object.d.ts +2 -0
- package/source/tuple-to-union.d.ts +3 -2
- package/source/undefined-on-partial-deep.d.ts +5 -4
- package/source/union-to-intersection.d.ts +0 -28
- package/source/union-to-tuple.d.ts +3 -3
- package/source/value-of.d.ts +6 -26
- package/source/words.d.ts +2 -0
- package/source/writable-keys-of.d.ts +2 -2
- package/source/writable.d.ts +2 -0
|
@@ -51,16 +51,16 @@ Returns a boolean for whether the given string `S` starts with the given string
|
|
|
51
51
|
|
|
52
52
|
@example
|
|
53
53
|
```
|
|
54
|
-
StartsWith<'abcde', 'abc'>;
|
|
54
|
+
type A = StartsWith<'abcde', 'abc'>;
|
|
55
55
|
//=> true
|
|
56
56
|
|
|
57
|
-
StartsWith<'abcde', 'bc'>;
|
|
57
|
+
type B = StartsWith<'abcde', 'bc'>;
|
|
58
58
|
//=> false
|
|
59
59
|
|
|
60
|
-
StartsWith<string, 'bc'>;
|
|
60
|
+
type C = StartsWith<string, 'bc'>;
|
|
61
61
|
//=> never
|
|
62
62
|
|
|
63
|
-
StartsWith<'abcde', string>;
|
|
63
|
+
type D = StartsWith<'abcde', string>;
|
|
64
64
|
//=> never
|
|
65
65
|
```
|
|
66
66
|
|
|
@@ -78,10 +78,10 @@ Returns an array of the characters of the string.
|
|
|
78
78
|
|
|
79
79
|
@example
|
|
80
80
|
```
|
|
81
|
-
StringToArray<'abcde'>;
|
|
81
|
+
type A = StringToArray<'abcde'>;
|
|
82
82
|
//=> ['a', 'b', 'c', 'd', 'e']
|
|
83
83
|
|
|
84
|
-
StringToArray<string>;
|
|
84
|
+
type B = StringToArray<string>;
|
|
85
85
|
//=> never
|
|
86
86
|
```
|
|
87
87
|
|
|
@@ -98,10 +98,10 @@ Returns the length of the given string.
|
|
|
98
98
|
|
|
99
99
|
@example
|
|
100
100
|
```
|
|
101
|
-
StringLength<'abcde'>;
|
|
101
|
+
type A = StringLength<'abcde'>;
|
|
102
102
|
//=> 5
|
|
103
103
|
|
|
104
|
-
StringLength<string>;
|
|
104
|
+
type B = StringLength<string>;
|
|
105
105
|
//=> never
|
|
106
106
|
```
|
|
107
107
|
|
|
@@ -137,10 +137,10 @@ Returns a boolean for whether `A` represents a number greater than `B`, where `A
|
|
|
137
137
|
|
|
138
138
|
@example
|
|
139
139
|
```
|
|
140
|
-
SameLengthPositiveNumericStringGt<'50', '10'>;
|
|
140
|
+
type A = SameLengthPositiveNumericStringGt<'50', '10'>;
|
|
141
141
|
//=> true
|
|
142
142
|
|
|
143
|
-
SameLengthPositiveNumericStringGt<'10', '10'>;
|
|
143
|
+
type B = SameLengthPositiveNumericStringGt<'10', '10'>;
|
|
144
144
|
//=> false
|
|
145
145
|
```
|
|
146
146
|
*/
|
|
@@ -159,13 +159,13 @@ Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are bot
|
|
|
159
159
|
|
|
160
160
|
@example
|
|
161
161
|
```
|
|
162
|
-
PositiveNumericStringGt<'500', '1'>;
|
|
162
|
+
type A = PositiveNumericStringGt<'500', '1'>;
|
|
163
163
|
//=> true
|
|
164
164
|
|
|
165
|
-
PositiveNumericStringGt<'1', '1'>;
|
|
165
|
+
type B = PositiveNumericStringGt<'1', '1'>;
|
|
166
166
|
//=> false
|
|
167
167
|
|
|
168
|
-
PositiveNumericStringGt<'1', '500'>;
|
|
168
|
+
type C = PositiveNumericStringGt<'1', '500'>;
|
|
169
169
|
//=> false
|
|
170
170
|
```
|
|
171
171
|
*/
|
|
@@ -184,10 +184,10 @@ Returns a boolean for whether `A` represents a number greater than `B`, where `A
|
|
|
184
184
|
|
|
185
185
|
@example
|
|
186
186
|
```
|
|
187
|
-
PositiveNumericCharacterGt<'5', '1'>;
|
|
187
|
+
type A = PositiveNumericCharacterGt<'5', '1'>;
|
|
188
188
|
//=> true
|
|
189
189
|
|
|
190
|
-
PositiveNumericCharacterGt<'1', '1'>;
|
|
190
|
+
type B = PositiveNumericCharacterGt<'1', '1'>;
|
|
191
191
|
//=> false
|
|
192
192
|
```
|
|
193
193
|
*/
|
|
@@ -17,7 +17,7 @@ type Array = TupleLength<string[]>;
|
|
|
17
17
|
//=> never
|
|
18
18
|
|
|
19
19
|
// Supports union types.
|
|
20
|
-
type Union = TupleLength<[] | [1, 2, 3] |
|
|
20
|
+
type Union = TupleLength<[] | [1, 2, 3] | number[]>;
|
|
21
21
|
//=> 1 | 3
|
|
22
22
|
```
|
|
23
23
|
*/
|
|
@@ -38,10 +38,10 @@ Note:
|
|
|
38
38
|
|
|
39
39
|
@example
|
|
40
40
|
```
|
|
41
|
-
|
|
41
|
+
type A = TupleMax<[1, 2, 5, 3]>;
|
|
42
42
|
//=> 5
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
type B = TupleMax<[1, 2, 5, 3, 99, -1]>;
|
|
45
45
|
//=> 99
|
|
46
46
|
```
|
|
47
47
|
*/
|
|
@@ -61,10 +61,10 @@ Note:
|
|
|
61
61
|
|
|
62
62
|
@example
|
|
63
63
|
```
|
|
64
|
-
|
|
64
|
+
type A = TupleMin<[1, 2, 5, 3]>;
|
|
65
65
|
//=> 1
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
type B = TupleMin<[1, 2, 5, 3, -5]>;
|
|
68
68
|
//=> -5
|
|
69
69
|
```
|
|
70
70
|
*/
|
|
@@ -12,7 +12,7 @@ export type BuiltIns = Primitive | void | Date | RegExp;
|
|
|
12
12
|
/**
|
|
13
13
|
Matches non-recursive types.
|
|
14
14
|
*/
|
|
15
|
-
export type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown)
|
|
15
|
+
export type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown) | Promise<unknown>;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
Matches maps, sets, or arrays.
|
|
@@ -55,13 +55,13 @@ Returns a boolean for whether the given type is primitive value or primitive typ
|
|
|
55
55
|
|
|
56
56
|
@example
|
|
57
57
|
```
|
|
58
|
-
IsPrimitive<'string'
|
|
58
|
+
type A = IsPrimitive<'string'>;
|
|
59
59
|
//=> true
|
|
60
60
|
|
|
61
|
-
IsPrimitive<string
|
|
61
|
+
type B = IsPrimitive<string>;
|
|
62
62
|
//=> true
|
|
63
63
|
|
|
64
|
-
IsPrimitive<Object
|
|
64
|
+
type C = IsPrimitive<Object>;
|
|
65
65
|
//=> false
|
|
66
66
|
```
|
|
67
67
|
*/
|
|
@@ -72,10 +72,10 @@ Returns a boolean for whether A is false.
|
|
|
72
72
|
|
|
73
73
|
@example
|
|
74
74
|
```
|
|
75
|
-
Not<true>;
|
|
75
|
+
type A = Not<true>;
|
|
76
76
|
//=> false
|
|
77
77
|
|
|
78
|
-
Not<false>;
|
|
78
|
+
type B = Not<false>;
|
|
79
79
|
//=> true
|
|
80
80
|
```
|
|
81
81
|
*/
|
package/source/invariant-of.d.ts
CHANGED
|
@@ -12,23 +12,28 @@ Use-case:
|
|
|
12
12
|
import type {InvariantOf} from 'type-fest';
|
|
13
13
|
|
|
14
14
|
class Animal {
|
|
15
|
-
constructor(public name: string){}
|
|
15
|
+
constructor(public name: string) {}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
class Cat extends Animal {
|
|
19
|
-
meow() {
|
|
19
|
+
meow() {
|
|
20
|
+
// do something
|
|
21
|
+
}
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
let animalArray: Animal[] = [
|
|
23
|
-
|
|
24
|
+
let animalArray: Animal[] = [new Animal('jerry')];
|
|
25
|
+
const catArray: Cat[] = [new Cat('tom')];
|
|
24
26
|
|
|
25
27
|
animalArray = catArray; // Okay if covariant
|
|
26
28
|
animalArray.push(new Animal('another animal')); // Pushed an animal into catArray
|
|
27
|
-
|
|
29
|
+
for (const c of catArray) {
|
|
30
|
+
c.meow();
|
|
31
|
+
} // Allowed but, error at runtime
|
|
28
32
|
|
|
29
|
-
let invariantAnimalArray: InvariantOf<Animal
|
|
30
|
-
|
|
33
|
+
let invariantAnimalArray: Array<InvariantOf<Animal>> = [new Animal('jerry')] as Array<InvariantOf<Animal>>;
|
|
34
|
+
const invariantCatArray: Array<InvariantOf<Cat>> = [new Cat('tom')] as Array<InvariantOf<Cat>>;
|
|
31
35
|
|
|
36
|
+
// @ts-expect-error
|
|
32
37
|
invariantAnimalArray = invariantCatArray; // Error: Type 'InvariantOf<Cat>[]' is not assignable to type 'InvariantOf<Animal>[]'.
|
|
33
38
|
```
|
|
34
39
|
|
|
@@ -38,24 +43,24 @@ import type {InvariantOf} from 'type-fest';
|
|
|
38
43
|
|
|
39
44
|
// In covariance (default)
|
|
40
45
|
|
|
41
|
-
|
|
46
|
+
type FooBar = {
|
|
42
47
|
foo: number;
|
|
43
|
-
bar: string
|
|
44
|
-
}
|
|
48
|
+
bar: string;
|
|
49
|
+
};
|
|
45
50
|
|
|
46
|
-
|
|
47
|
-
baz: boolean
|
|
48
|
-
}
|
|
51
|
+
type FooBarBaz = {
|
|
52
|
+
baz: boolean;
|
|
53
|
+
} & FooBar;
|
|
49
54
|
|
|
50
|
-
declare const fooBar: FooBar
|
|
51
|
-
declare const fooBarBaz: FooBarBaz
|
|
55
|
+
declare const fooBar: FooBar;
|
|
56
|
+
declare const fooBarBaz: FooBarBaz;
|
|
52
57
|
|
|
53
58
|
function keyOfFooBar(fooBar: FooBar) {
|
|
54
|
-
return Object.keys(fooBar) as
|
|
59
|
+
return Object.keys(fooBar) as Array<keyof FooBar>;
|
|
55
60
|
}
|
|
56
61
|
|
|
57
|
-
keyOfFooBar(fooBar) //=> (keyof FooBar)[]
|
|
58
|
-
keyOfFooBar(fooBarBaz) //=> (keyof FooBar)[] but, (keyof FooBarBaz)[] at runtime
|
|
62
|
+
keyOfFooBar(fooBar); //=> (keyof FooBar)[]
|
|
63
|
+
keyOfFooBar(fooBarBaz); //=> (keyof FooBar)[] but, (keyof FooBarBaz)[] at runtime
|
|
59
64
|
|
|
60
65
|
// In invariance
|
|
61
66
|
|
|
@@ -64,10 +69,12 @@ export function invariantOf<Type>(value: Type): InvariantOf<Type> {
|
|
|
64
69
|
}
|
|
65
70
|
|
|
66
71
|
function keyOfInvariantFooBar(fooBar: InvariantOf<FooBar>) {
|
|
67
|
-
return Object.keys(fooBar) as
|
|
72
|
+
return Object.keys(fooBar) as Array<keyof FooBar>;
|
|
68
73
|
}
|
|
69
74
|
|
|
70
75
|
keyOfInvariantFooBar(invariantOf(fooBar)); // (keyof FooBar)[]
|
|
76
|
+
|
|
77
|
+
// @ts-expect-error
|
|
71
78
|
keyOfInvariantFooBar(invariantOf(fooBarBaz)); // Error: Argument of type 'InvariantOf<FooBarBaz>' is not assignable to parameter of type 'InvariantOf<FooBar>'.
|
|
72
79
|
```
|
|
73
80
|
|
package/source/is-any.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ import type {IsAny} from 'type-fest';
|
|
|
12
12
|
const typedObject = {a: 1, b: 2} as const;
|
|
13
13
|
const anyObject: any = {a: 1, b: 2};
|
|
14
14
|
|
|
15
|
-
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(
|
|
16
|
-
return
|
|
15
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(object: O, key: K) {
|
|
16
|
+
return object[key];
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
const typedA = get(typedObject, 'a');
|
package/source/is-equal.d.ts
CHANGED
|
@@ -26,14 +26,10 @@ type Includes<Value extends readonly any[], Item> =
|
|
|
26
26
|
@category Utilities
|
|
27
27
|
*/
|
|
28
28
|
export type IsEqual<A, B> =
|
|
29
|
-
[A
|
|
30
|
-
? [
|
|
31
|
-
?
|
|
32
|
-
|
|
33
|
-
: false
|
|
34
|
-
: [BB] extends [never]
|
|
35
|
-
? false
|
|
36
|
-
: _IsEqual<AA, BB>
|
|
29
|
+
[A] extends [B]
|
|
30
|
+
? [B] extends [A]
|
|
31
|
+
? _IsEqual<A, B>
|
|
32
|
+
: false
|
|
37
33
|
: false;
|
|
38
34
|
|
|
39
35
|
// This version fails the `equalWrappedTupleIntersectionToBeNeverAndNeverExpanded` test in `test-d/is-equal.ts`.
|
package/source/is-integer.d.ts
CHANGED
package/source/is-literal.d.ts
CHANGED
|
@@ -11,13 +11,13 @@ Returns a boolean for whether the given type `T` is the specified `LiteralType`.
|
|
|
11
11
|
|
|
12
12
|
@example
|
|
13
13
|
```
|
|
14
|
-
LiteralCheck<1, number
|
|
14
|
+
type A = LiteralCheck<1, number>;
|
|
15
15
|
//=> true
|
|
16
16
|
|
|
17
|
-
LiteralCheck<number, number
|
|
17
|
+
type B = LiteralCheck<number, number>;
|
|
18
18
|
//=> false
|
|
19
19
|
|
|
20
|
-
LiteralCheck<1, string
|
|
20
|
+
type C = LiteralCheck<1, string>;
|
|
21
21
|
//=> false
|
|
22
22
|
```
|
|
23
23
|
*/
|
|
@@ -38,13 +38,13 @@ Returns a boolean for whether the given type `T` is one of the specified literal
|
|
|
38
38
|
|
|
39
39
|
@example
|
|
40
40
|
```
|
|
41
|
-
LiteralChecks<1, Numeric
|
|
41
|
+
type A = LiteralChecks<1, Numeric>;
|
|
42
42
|
//=> true
|
|
43
43
|
|
|
44
|
-
LiteralChecks<1n, Numeric
|
|
44
|
+
type B = LiteralChecks<1n, Numeric>;
|
|
45
45
|
//=> true
|
|
46
46
|
|
|
47
|
-
LiteralChecks<bigint, Numeric
|
|
47
|
+
type C = LiteralChecks<bigint, Numeric>;
|
|
48
48
|
//=> false
|
|
49
49
|
```
|
|
50
50
|
*/
|
|
@@ -136,7 +136,7 @@ Useful for:
|
|
|
136
136
|
|
|
137
137
|
@example
|
|
138
138
|
```
|
|
139
|
-
import type {IsNumericLiteral} from 'type-fest';
|
|
139
|
+
import type {IsNumericLiteral, IsStringLiteral} from 'type-fest';
|
|
140
140
|
|
|
141
141
|
// https://github.com/inocan-group/inferred-types/blob/master/modules/types/src/boolean-logic/operators/EndsWith.ts
|
|
142
142
|
type EndsWith<TValue, TEndsWith extends string> =
|
|
@@ -161,7 +161,7 @@ function endsWith<Input extends string | number, End extends string>(input: Inpu
|
|
|
161
161
|
endsWith('abc', 'c');
|
|
162
162
|
//=> true
|
|
163
163
|
|
|
164
|
-
endsWith(
|
|
164
|
+
endsWith(123_456, '456');
|
|
165
165
|
//=> true
|
|
166
166
|
|
|
167
167
|
const end = '123' as string;
|
|
@@ -226,17 +226,17 @@ Useful for:
|
|
|
226
226
|
```
|
|
227
227
|
import type {IsSymbolLiteral} from 'type-fest';
|
|
228
228
|
|
|
229
|
-
type Get<
|
|
229
|
+
type Get<Object_ extends Record<symbol, number>, Key extends keyof Object_> =
|
|
230
230
|
IsSymbolLiteral<Key> extends true
|
|
231
|
-
?
|
|
231
|
+
? Object_[Key]
|
|
232
232
|
: number;
|
|
233
233
|
|
|
234
|
-
function get<
|
|
235
|
-
return o[key] as Get<
|
|
234
|
+
function get<Object_ extends Record<symbol, number>, Key extends keyof Object_>(o: Object_, key: Key) {
|
|
235
|
+
return o[key] as Get<Object_, Key>;
|
|
236
236
|
}
|
|
237
237
|
|
|
238
238
|
const symbolLiteral = Symbol('literal');
|
|
239
|
-
const symbolValue
|
|
239
|
+
const symbolValue = Symbol('value');
|
|
240
240
|
|
|
241
241
|
get({[symbolLiteral]: 1} as const, symbolLiteral);
|
|
242
242
|
//=> 1
|
package/source/is-lowercase.d.ts
CHANGED
|
@@ -7,13 +7,13 @@ Returns a boolean for whether the given string literal is lowercase.
|
|
|
7
7
|
```
|
|
8
8
|
import type {IsLowercase} from 'type-fest';
|
|
9
9
|
|
|
10
|
-
IsLowercase<'abc'>;
|
|
10
|
+
type A = IsLowercase<'abc'>;
|
|
11
11
|
//=> true
|
|
12
12
|
|
|
13
|
-
IsLowercase<'Abc'>;
|
|
13
|
+
type B = IsLowercase<'Abc'>;
|
|
14
14
|
//=> false
|
|
15
15
|
|
|
16
|
-
IsLowercase<string>;
|
|
16
|
+
type C = IsLowercase<string>;
|
|
17
17
|
//=> boolean
|
|
18
18
|
```
|
|
19
19
|
*/
|
package/source/is-never.d.ts
CHANGED
|
@@ -11,29 +11,41 @@ Useful in type utilities, such as checking if something does not occur.
|
|
|
11
11
|
```
|
|
12
12
|
import type {IsNever, And} from 'type-fest';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
14
|
+
type A = IsNever<never>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type B = IsNever<any>;
|
|
18
|
+
//=> false
|
|
19
|
+
|
|
20
|
+
type C = IsNever<unknown>;
|
|
21
|
+
//=> false
|
|
22
|
+
|
|
23
|
+
type D = IsNever<never[]>;
|
|
24
|
+
//=> false
|
|
25
|
+
|
|
26
|
+
type E = IsNever<object>;
|
|
27
|
+
//=> false
|
|
28
|
+
|
|
29
|
+
type F = IsNever<string>;
|
|
30
|
+
//=> false
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
@example
|
|
34
|
+
```
|
|
35
|
+
import type {IsNever} from 'type-fest';
|
|
36
|
+
|
|
37
|
+
type IsTrue<T> = T extends true ? true : false;
|
|
38
|
+
|
|
39
|
+
// When a distributive conditional is instantiated with `never`, the entire conditional results in `never`.
|
|
40
|
+
type A = IsTrue<never>;
|
|
41
|
+
// ^? type A = never
|
|
42
|
+
|
|
43
|
+
// If you don't want that behaviour, you can explicitly add an `IsNever` check before the distributive conditional.
|
|
44
|
+
type IsTrueFixed<T> =
|
|
45
|
+
IsNever<T> extends true ? false : T extends true ? true : false;
|
|
46
|
+
|
|
47
|
+
type B = IsTrueFixed<never>;
|
|
48
|
+
// ^? type B = false
|
|
37
49
|
```
|
|
38
50
|
|
|
39
51
|
@category Type Guard
|
package/source/is-null.d.ts
CHANGED
|
@@ -9,17 +9,17 @@ This is useful when writing utility types or schema validators that need to diff
|
|
|
9
9
|
```
|
|
10
10
|
import type {IsOptionalKeyOf} from 'type-fest';
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
type User = {
|
|
13
13
|
name: string;
|
|
14
14
|
surname: string;
|
|
15
15
|
|
|
16
16
|
luckyNumber?: number;
|
|
17
|
-
}
|
|
17
|
+
};
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
type Admin = {
|
|
20
20
|
name: string;
|
|
21
21
|
surname?: string;
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
|
|
24
24
|
type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
|
|
25
25
|
//=> true
|
|
@@ -10,17 +10,17 @@ This is useful when writing utility types or schema validators that need to diff
|
|
|
10
10
|
```
|
|
11
11
|
import type {IsReadonlyKeyOf} from 'type-fest';
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
type User = {
|
|
14
14
|
name: string;
|
|
15
15
|
surname: string;
|
|
16
16
|
|
|
17
17
|
readonly id: number;
|
|
18
|
-
}
|
|
18
|
+
};
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
type Admin = {
|
|
21
21
|
name: string;
|
|
22
22
|
id: string;
|
|
23
|
-
}
|
|
23
|
+
};
|
|
24
24
|
|
|
25
25
|
type T1 = IsReadonlyKeyOf<User, 'id'>;
|
|
26
26
|
//=> true
|
|
@@ -11,17 +11,17 @@ This is useful when writing utility types or schema validators that need to diff
|
|
|
11
11
|
```
|
|
12
12
|
import type {IsRequiredKeyOf} from 'type-fest';
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
type User = {
|
|
15
15
|
name: string;
|
|
16
16
|
surname: string;
|
|
17
17
|
|
|
18
18
|
luckyNumber?: number;
|
|
19
|
-
}
|
|
19
|
+
};
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
type Admin = {
|
|
22
22
|
name: string;
|
|
23
23
|
surname?: string;
|
|
24
|
-
}
|
|
24
|
+
};
|
|
25
25
|
|
|
26
26
|
type T1 = IsRequiredKeyOf<User, 'name'>;
|
|
27
27
|
//=> true
|
package/source/is-unknown.d.ts
CHANGED
|
@@ -11,34 +11,23 @@ Useful in type utilities, such as when dealing with unknown data from API calls.
|
|
|
11
11
|
```
|
|
12
12
|
import type {IsUnknown} from 'type-fest';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
IsUnknown<TPayload> extends true
|
|
17
|
-
? (state: TState) => TState,
|
|
18
|
-
: (state: TState, payload: TPayload) => TState;
|
|
14
|
+
type A = IsUnknown<unknown>;
|
|
15
|
+
//=> unknown
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
type B = IsUnknown<any>;
|
|
18
|
+
//=> false
|
|
22
19
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return this.state;
|
|
26
|
-
}
|
|
20
|
+
type C = IsUnknown<never>;
|
|
21
|
+
//=> false
|
|
27
22
|
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
type D = IsUnknown<unknown[]>;
|
|
24
|
+
//=> false
|
|
30
25
|
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
type E = IsUnknown<object>;
|
|
27
|
+
//=> false
|
|
33
28
|
|
|
34
|
-
|
|
35
|
-
//=>
|
|
36
|
-
|
|
37
|
-
store.execute((state, payload) => ({value: state.value + payload}), 5);
|
|
38
|
-
//=> `TPayload` is `5`
|
|
39
|
-
|
|
40
|
-
store.execute((state, payload) => ({value: state.value + payload}), someExternalData);
|
|
41
|
-
//=> Errors: `action` is `(state: TState) => TState`
|
|
29
|
+
type F = IsUnknown<string>;
|
|
30
|
+
//=> false
|
|
42
31
|
```
|
|
43
32
|
|
|
44
33
|
@category Utilities
|
package/source/is-uppercase.d.ts
CHANGED
|
@@ -7,13 +7,13 @@ Returns a boolean for whether the given string literal is uppercase.
|
|
|
7
7
|
```
|
|
8
8
|
import type {IsUppercase} from 'type-fest';
|
|
9
9
|
|
|
10
|
-
IsUppercase<'ABC'>;
|
|
10
|
+
type A = IsUppercase<'ABC'>;
|
|
11
11
|
//=> true
|
|
12
12
|
|
|
13
|
-
IsUppercase<'Abc'>;
|
|
13
|
+
type B = IsUppercase<'Abc'>;
|
|
14
14
|
//=> false
|
|
15
15
|
|
|
16
|
-
IsUppercase<string>;
|
|
16
|
+
type C = IsUppercase<string>;
|
|
17
17
|
//=> boolean
|
|
18
18
|
```
|
|
19
19
|
*/
|
|
@@ -11,17 +11,17 @@ This is useful when writing utility types or schema validators that need to diff
|
|
|
11
11
|
```
|
|
12
12
|
import type {IsWritableKeyOf} from 'type-fest';
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
type User = {
|
|
15
15
|
name: string;
|
|
16
16
|
surname: string;
|
|
17
17
|
|
|
18
18
|
readonly id: number;
|
|
19
|
-
}
|
|
19
|
+
};
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
type Admin = {
|
|
22
22
|
name: string;
|
|
23
23
|
id: string;
|
|
24
|
-
}
|
|
24
|
+
};
|
|
25
25
|
|
|
26
26
|
type T1 = IsWritableKeyOf<User, 'name'>;
|
|
27
27
|
//=> true
|
|
@@ -16,7 +16,7 @@ function * iAmGenerator() {
|
|
|
16
16
|
yield 2;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
type MeNumber = IterableElement<ReturnType<typeof iAmGenerator
|
|
19
|
+
type MeNumber = IterableElement<ReturnType<typeof iAmGenerator>>;
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
And here is an example with an async generator:
|
|
@@ -30,7 +30,7 @@ async function * iAmGeneratorAsync() {
|
|
|
30
30
|
yield true;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
type MeStringOrBoolean = IterableElement<ReturnType<typeof iAmGeneratorAsync
|
|
33
|
+
type MeStringOrBoolean = IterableElement<ReturnType<typeof iAmGeneratorAsync>>;
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
Many types in JavaScript/TypeScript are iterables. This type works on all types that implement those interfaces.
|
|
@@ -41,7 +41,7 @@ An example with an array of strings:
|
|
|
41
41
|
```
|
|
42
42
|
import type {IterableElement} from 'type-fest';
|
|
43
43
|
|
|
44
|
-
type MeString = IterableElement<string[]
|
|
44
|
+
type MeString = IterableElement<string[]>;
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
@example
|