@depup/type-fest 5.4.4-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 +25 -0
- package/index.d.ts +211 -0
- package/license-cc0 +121 -0
- package/license-mit +9 -0
- package/package.json +85 -0
- package/readme.md +1052 -0
- package/source/all-extend.d.ts +120 -0
- package/source/all-union-fields.d.ts +91 -0
- package/source/and.d.ts +81 -0
- package/source/array-element.d.ts +46 -0
- package/source/array-indices.d.ts +25 -0
- package/source/array-reverse.d.ts +84 -0
- package/source/array-slice.d.ts +132 -0
- package/source/array-splice.d.ts +104 -0
- package/source/array-tail.d.ts +70 -0
- package/source/array-values.d.ts +24 -0
- package/source/arrayable.d.ts +31 -0
- package/source/async-return-type.d.ts +28 -0
- package/source/asyncify.d.ts +25 -0
- package/source/basic.d.ts +38 -0
- package/source/camel-case.d.ts +92 -0
- package/source/camel-cased-properties-deep.d.ts +99 -0
- package/source/camel-cased-properties.d.ts +45 -0
- package/source/characters.d.ts +67 -0
- package/source/conditional-except.d.ts +47 -0
- package/source/conditional-keys.d.ts +63 -0
- package/source/conditional-pick-deep.d.ts +120 -0
- package/source/conditional-pick.d.ts +46 -0
- package/source/conditional-simplify-deep.d.ts +73 -0
- package/source/conditional-simplify.d.ts +50 -0
- package/source/delimiter-case.d.ts +77 -0
- package/source/delimiter-cased-properties-deep.d.ts +108 -0
- package/source/delimiter-cased-properties.d.ts +48 -0
- package/source/distributed-omit.d.ts +96 -0
- package/source/distributed-pick.d.ts +92 -0
- package/source/empty-object.d.ts +51 -0
- package/source/entries.d.ts +64 -0
- package/source/entry.d.ts +67 -0
- package/source/exact.d.ts +73 -0
- package/source/except.d.ts +112 -0
- package/source/exclude-rest-element.d.ts +40 -0
- package/source/exclude-strict.d.ts +51 -0
- package/source/exclusify-union.d.ts +147 -0
- package/source/extends-strict.d.ts +44 -0
- package/source/extract-rest-element.d.ts +30 -0
- package/source/extract-strict.d.ts +51 -0
- package/source/find-global-type.d.ts +68 -0
- package/source/fixed-length-array.d.ts +97 -0
- package/source/get.d.ts +227 -0
- package/source/global-this.d.ts +24 -0
- package/source/globals/index.d.ts +3 -0
- package/source/globals/observable-like.d.ts +78 -0
- package/source/greater-than-or-equal.d.ts +30 -0
- package/source/greater-than.d.ts +58 -0
- package/source/has-optional-keys.d.ts +23 -0
- package/source/has-readonly-keys.d.ts +23 -0
- package/source/has-required-keys.d.ts +61 -0
- package/source/has-writable-keys.d.ts +23 -0
- package/source/if-any.d.ts +28 -0
- package/source/if-empty-object.d.ts +30 -0
- package/source/if-never.d.ts +28 -0
- package/source/if-null.d.ts +28 -0
- package/source/if-unknown.d.ts +28 -0
- package/source/if.d.ts +102 -0
- package/source/includes.d.ts +24 -0
- package/source/int-closed-range.d.ts +47 -0
- package/source/int-range.d.ts +69 -0
- package/source/internal/array.d.ts +151 -0
- package/source/internal/characters.d.ts +65 -0
- package/source/internal/enforce-optional.d.ts +49 -0
- package/source/internal/index.d.ts +11 -0
- package/source/internal/keys.d.ts +100 -0
- package/source/internal/numeric.d.ts +151 -0
- package/source/internal/object.d.ts +292 -0
- package/source/internal/string.d.ts +202 -0
- package/source/internal/tuple.d.ts +79 -0
- package/source/internal/type.d.ts +164 -0
- package/source/invariant-of.d.ts +85 -0
- package/source/is-any.d.ts +31 -0
- package/source/is-equal.d.ts +42 -0
- package/source/is-float.d.ts +43 -0
- package/source/is-integer.d.ts +60 -0
- package/source/is-literal.d.ts +315 -0
- package/source/is-lowercase.d.ts +38 -0
- package/source/is-never.d.ts +56 -0
- package/source/is-null.d.ts +22 -0
- package/source/is-nullable.d.ts +30 -0
- package/source/is-optional-key-of.d.ts +51 -0
- package/source/is-optional.d.ts +28 -0
- package/source/is-readonly-key-of.d.ts +55 -0
- package/source/is-required-key-of.d.ts +51 -0
- package/source/is-tuple.d.ts +92 -0
- package/source/is-undefined.d.ts +22 -0
- package/source/is-union.d.ts +40 -0
- package/source/is-unknown.d.ts +43 -0
- package/source/is-uppercase.d.ts +38 -0
- package/source/is-writable-key-of.d.ts +51 -0
- package/source/iterable-element.d.ts +66 -0
- package/source/join.d.ts +79 -0
- package/source/json-value.d.ts +33 -0
- package/source/jsonifiable.d.ts +37 -0
- package/source/jsonify.d.ts +127 -0
- package/source/kebab-case.d.ts +46 -0
- package/source/kebab-cased-properties-deep.d.ts +65 -0
- package/source/kebab-cased-properties.d.ts +42 -0
- package/source/key-as-string.d.ts +27 -0
- package/source/keys-of-union.d.ts +44 -0
- package/source/last-array-element.d.ts +36 -0
- package/source/less-than-or-equal.d.ts +24 -0
- package/source/less-than.d.ts +28 -0
- package/source/literal-to-primitive-deep.d.ts +71 -0
- package/source/literal-to-primitive.d.ts +38 -0
- package/source/literal-union.d.ts +39 -0
- package/source/merge-deep.d.ts +495 -0
- package/source/merge-exclusive.d.ts +45 -0
- package/source/merge.d.ts +62 -0
- package/source/multidimensional-array.d.ts +38 -0
- package/source/multidimensional-readonly-array.d.ts +38 -0
- package/source/non-empty-object.d.ts +38 -0
- package/source/non-empty-string.d.ts +32 -0
- package/source/non-empty-tuple.d.ts +24 -0
- package/source/numeric.d.ts +226 -0
- package/source/object-merge.d.ts +194 -0
- package/source/omit-deep.d.ts +154 -0
- package/source/omit-index-signature.d.ts +98 -0
- package/source/opaque.d.ts +3 -0
- package/source/optional-keys-of.d.ts +46 -0
- package/source/or.d.ts +89 -0
- package/source/override-properties.d.ts +43 -0
- package/source/package-json.d.ts +710 -0
- package/source/partial-deep.d.ts +155 -0
- package/source/partial-on-undefined-deep.d.ts +81 -0
- package/source/pascal-case.d.ts +51 -0
- package/source/pascal-cased-properties-deep.d.ts +72 -0
- package/source/pascal-cased-properties.d.ts +42 -0
- package/source/paths.d.ts +241 -0
- package/source/pick-deep.d.ts +152 -0
- package/source/pick-index-signature.d.ts +52 -0
- package/source/primitive.d.ts +15 -0
- package/source/promisable.d.ts +27 -0
- package/source/readonly-deep.d.ts +116 -0
- package/source/readonly-keys-of.d.ts +38 -0
- package/source/readonly-tuple.d.ts +34 -0
- package/source/remove-prefix.d.ts +132 -0
- package/source/replace.d.ts +87 -0
- package/source/require-all-or-none.d.ts +54 -0
- package/source/require-at-least-one.d.ts +50 -0
- package/source/require-exactly-one.d.ts +48 -0
- package/source/require-one-or-none.d.ts +49 -0
- package/source/required-deep.d.ts +76 -0
- package/source/required-keys-of.d.ts +38 -0
- package/source/schema.d.ts +119 -0
- package/source/screaming-snake-case.d.ts +30 -0
- package/source/set-field-type.d.ts +67 -0
- package/source/set-non-nullable-deep.d.ts +85 -0
- package/source/set-non-nullable.d.ts +41 -0
- package/source/set-optional.d.ts +46 -0
- package/source/set-parameter-type.d.ts +125 -0
- package/source/set-readonly.d.ts +44 -0
- package/source/set-required-deep.d.ts +64 -0
- package/source/set-required.d.ts +79 -0
- package/source/set-return-type.d.ts +31 -0
- package/source/shared-union-fields-deep.d.ts +180 -0
- package/source/shared-union-fields.d.ts +79 -0
- package/source/simplify-deep.d.ts +117 -0
- package/source/simplify.d.ts +61 -0
- package/source/single-key-object.d.ts +28 -0
- package/source/snake-case.d.ts +47 -0
- package/source/snake-cased-properties-deep.d.ts +65 -0
- package/source/snake-cased-properties.d.ts +42 -0
- package/source/split-on-rest-element.d.ts +106 -0
- package/source/split.d.ts +90 -0
- package/source/spread.d.ts +82 -0
- package/source/string-repeat.d.ts +49 -0
- package/source/string-slice.d.ts +39 -0
- package/source/stringified.d.ts +25 -0
- package/source/structured-cloneable.d.ts +89 -0
- package/source/subtract.d.ts +86 -0
- package/source/sum.d.ts +81 -0
- package/source/tagged-union.d.ts +53 -0
- package/source/tagged.d.ts +263 -0
- package/source/trim.d.ts +29 -0
- package/source/tsconfig-json.d.ts +1324 -0
- package/source/tuple-of.d.ts +80 -0
- package/source/tuple-to-object.d.ts +47 -0
- package/source/tuple-to-union.d.ts +54 -0
- package/source/typed-array.d.ts +19 -0
- package/source/undefined-on-partial-deep.d.ts +83 -0
- package/source/union-to-intersection.d.ts +35 -0
- package/source/union-to-tuple.d.ts +58 -0
- package/source/unknown-array.d.ts +27 -0
- package/source/unknown-map.d.ts +26 -0
- package/source/unknown-record.d.ts +33 -0
- package/source/unknown-set.d.ts +26 -0
- package/source/unwrap-partial.d.ts +33 -0
- package/source/value-of.d.ts +24 -0
- package/source/words.d.ts +122 -0
- package/source/writable-deep.d.ts +84 -0
- package/source/writable-keys-of.d.ts +34 -0
- package/source/writable.d.ts +72 -0
- package/source/xor.d.ts +83 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type {_DefaultDelimiterCaseOptions} from './delimiter-case.d.ts';
|
|
2
|
+
import type {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep.d.ts';
|
|
3
|
+
import type {ApplyDefaultOptions} from './internal/index.d.ts';
|
|
4
|
+
import type {WordsOptions} from './words.d.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
Convert object properties to kebab case recursively.
|
|
8
|
+
|
|
9
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
10
|
+
|
|
11
|
+
@see {@link KebabCase}
|
|
12
|
+
@see {@link KebabCasedProperties}
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import type {KebabCasedPropertiesDeep} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
type User = {
|
|
19
|
+
userId: number;
|
|
20
|
+
userName: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type UserWithFriends = {
|
|
24
|
+
userInfo: User;
|
|
25
|
+
userFriends: User[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const result: KebabCasedPropertiesDeep<UserWithFriends> = {
|
|
29
|
+
'user-info': {
|
|
30
|
+
'user-id': 1,
|
|
31
|
+
'user-name': 'Tom',
|
|
32
|
+
},
|
|
33
|
+
'user-friends': [
|
|
34
|
+
{
|
|
35
|
+
'user-id': 2,
|
|
36
|
+
'user-name': 'Jerry',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
'user-id': 3,
|
|
40
|
+
'user-name': 'Spike',
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const splitOnNumbers: KebabCasedPropertiesDeep<{line1: {line2: [{line3: string}]}}, {splitOnNumbers: true}> = {
|
|
46
|
+
'line-1': {
|
|
47
|
+
'line-2': [
|
|
48
|
+
{
|
|
49
|
+
'line-3': 'string',
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
@category Change case
|
|
57
|
+
@category Template literal
|
|
58
|
+
@category Object
|
|
59
|
+
*/
|
|
60
|
+
export type KebabCasedPropertiesDeep<
|
|
61
|
+
Value,
|
|
62
|
+
Options extends WordsOptions = {},
|
|
63
|
+
> = DelimiterCasedPropertiesDeep<Value, '-', ApplyDefaultOptions<WordsOptions, _DefaultDelimiterCaseOptions, Options>>;
|
|
64
|
+
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type {_DefaultDelimiterCaseOptions} from './delimiter-case.d.ts';
|
|
2
|
+
import type {DelimiterCasedProperties} from './delimiter-cased-properties.d.ts';
|
|
3
|
+
import type {ApplyDefaultOptions} from './internal/index.d.ts';
|
|
4
|
+
import type {WordsOptions} from './words.d.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
Convert object properties to kebab case but not recursively.
|
|
8
|
+
|
|
9
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
10
|
+
|
|
11
|
+
@see {@link KebabCase}
|
|
12
|
+
@see {@link KebabCasedPropertiesDeep}
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import type {KebabCasedProperties} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
type User = {
|
|
19
|
+
userId: number;
|
|
20
|
+
userName: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const result: KebabCasedProperties<User> = {
|
|
24
|
+
'user-id': 1,
|
|
25
|
+
'user-name': 'Tom',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const splitOnNumbers: KebabCasedProperties<{line1: string}, {splitOnNumbers: true}> = {
|
|
29
|
+
'line-1': 'string',
|
|
30
|
+
};
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
@category Change case
|
|
34
|
+
@category Template literal
|
|
35
|
+
@category Object
|
|
36
|
+
*/
|
|
37
|
+
export type KebabCasedProperties<
|
|
38
|
+
Value,
|
|
39
|
+
Options extends WordsOptions = {},
|
|
40
|
+
> = DelimiterCasedProperties<Value, '-', ApplyDefaultOptions<WordsOptions, _DefaultDelimiterCaseOptions, Options>>;
|
|
41
|
+
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Get keys of the given type as strings.
|
|
3
|
+
|
|
4
|
+
Number keys are converted to strings.
|
|
5
|
+
|
|
6
|
+
Use-cases:
|
|
7
|
+
- Get string keys from a type which may have number keys.
|
|
8
|
+
- Makes it possible to index using strings retrieved from template types.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {KeyAsString} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type Foo = {
|
|
15
|
+
1: number;
|
|
16
|
+
stringKey: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type StringKeysOfFoo = KeyAsString<Foo>;
|
|
20
|
+
//=> 'stringKey' | '1'
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
@category Object
|
|
24
|
+
*/
|
|
25
|
+
export type KeyAsString<BaseType> = `${Extract<keyof BaseType, string | number>}`;
|
|
26
|
+
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type {UnionToIntersection} from './union-to-intersection.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Create a union of all keys from a given type, even those exclusive to specific union members.
|
|
5
|
+
|
|
6
|
+
Unlike the native `keyof` keyword, which returns keys present in **all** union members, this type returns keys from **any** member.
|
|
7
|
+
|
|
8
|
+
@link https://stackoverflow.com/a/49402091
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {KeysOfUnion} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type A = {
|
|
15
|
+
common: string;
|
|
16
|
+
a: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type B = {
|
|
20
|
+
common: string;
|
|
21
|
+
b: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type C = {
|
|
25
|
+
common: string;
|
|
26
|
+
c: boolean;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type Union = A | B | C;
|
|
30
|
+
|
|
31
|
+
type CommonKeys = keyof Union;
|
|
32
|
+
//=> 'common'
|
|
33
|
+
|
|
34
|
+
type AllKeys = KeysOfUnion<Union>;
|
|
35
|
+
//=> 'common' | 'a' | 'b' | 'c'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
@category Object
|
|
39
|
+
*/
|
|
40
|
+
export type KeysOfUnion<ObjectType> =
|
|
41
|
+
// Hack to fix https://github.com/sindresorhus/type-fest/issues/1008
|
|
42
|
+
keyof UnionToIntersection<ObjectType extends unknown ? Record<keyof ObjectType, never> : never>;
|
|
43
|
+
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Extract the type of the last element of an array.
|
|
3
|
+
|
|
4
|
+
Use-case: Defining the return type of functions that extract the last element of an array, for example [`lodash.last`](https://lodash.com/docs/4.17.15#last).
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {LastArrayElement} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
declare function lastOf<const V extends readonly any[]>(array: V): LastArrayElement<V>;
|
|
11
|
+
|
|
12
|
+
const last1 = lastOf(['foo', 'bar']);
|
|
13
|
+
//=> 'bar'
|
|
14
|
+
|
|
15
|
+
const last2 = lastOf([true, false, 'baz', 10]);
|
|
16
|
+
//=> 10
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
@category Array
|
|
20
|
+
@category Template literal
|
|
21
|
+
*/
|
|
22
|
+
export type LastArrayElement<Elements extends readonly unknown[], ElementBeforeTailingSpreadElement = never> =
|
|
23
|
+
// If the last element of an array is a spread element, the `LastArrayElement` result should be `'the type of the element before the spread element' | 'the type of the spread element'`.
|
|
24
|
+
Elements extends readonly []
|
|
25
|
+
? ElementBeforeTailingSpreadElement
|
|
26
|
+
: Elements extends readonly [...infer U, infer V]
|
|
27
|
+
? V
|
|
28
|
+
: Elements extends readonly [infer U, ...infer V]
|
|
29
|
+
// If we return `V[number] | U` directly, it would be wrong for `[[string, boolean, object, ...number[]]`.
|
|
30
|
+
// So we need to recurse type `V` and carry over the type of the element before the spread element.
|
|
31
|
+
? LastArrayElement<V, U>
|
|
32
|
+
: Elements extends ReadonlyArray<infer U>
|
|
33
|
+
? U | ElementBeforeTailingSpreadElement
|
|
34
|
+
: never;
|
|
35
|
+
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type {GreaterThan} from './greater-than.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether a given number is less than or equal to another number.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {LessThanOrEqual} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type A = LessThanOrEqual<1, -5>;
|
|
11
|
+
//=> false
|
|
12
|
+
|
|
13
|
+
type B = LessThanOrEqual<1, 1>;
|
|
14
|
+
//=> true
|
|
15
|
+
|
|
16
|
+
type C = LessThanOrEqual<1, 5>;
|
|
17
|
+
//=> true
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export type LessThanOrEqual<A extends number, B extends number> = number extends A | B
|
|
21
|
+
? never
|
|
22
|
+
: GreaterThan<A, B> extends true ? false : true;
|
|
23
|
+
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {GreaterThanOrEqual} from './greater-than-or-equal.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether a given number is less than another number.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {LessThan} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type A = LessThan<1, -5>;
|
|
11
|
+
//=> false
|
|
12
|
+
|
|
13
|
+
type B = LessThan<1, 1>;
|
|
14
|
+
//=> false
|
|
15
|
+
|
|
16
|
+
type C = LessThan<1, 5>;
|
|
17
|
+
//=> true
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export type LessThan<A extends number, B extends number> = number extends A | B
|
|
21
|
+
? never
|
|
22
|
+
: GreaterThanOrEqual<A, B> extends infer Result
|
|
23
|
+
? Result extends true
|
|
24
|
+
? false
|
|
25
|
+
: true
|
|
26
|
+
: never; // Should never happen
|
|
27
|
+
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type {LiteralToPrimitive} from './literal-to-primitive.d.ts';
|
|
2
|
+
import type {OmitIndexSignature} from './omit-index-signature.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Like `LiteralToPrimitive` except it converts literal types inside an object or array deeply.
|
|
6
|
+
|
|
7
|
+
For example, given a constant object, it returns a new object type with the same keys but with all the values converted to primitives.
|
|
8
|
+
|
|
9
|
+
@see {@link LiteralToPrimitive}
|
|
10
|
+
|
|
11
|
+
Use-case: Deal with data that is imported from a JSON file.
|
|
12
|
+
|
|
13
|
+
@example
|
|
14
|
+
```
|
|
15
|
+
import type {LiteralToPrimitiveDeep} from 'type-fest';
|
|
16
|
+
|
|
17
|
+
const config = {
|
|
18
|
+
appName: 'MyApp',
|
|
19
|
+
version: '1.0.0',
|
|
20
|
+
maxRetries: 3,
|
|
21
|
+
enableLogging: true,
|
|
22
|
+
apiUrl: 'https://api.myapp.com/v1',
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
declare function updateConfig(newConfig: typeof config): void;
|
|
26
|
+
|
|
27
|
+
updateConfig({
|
|
28
|
+
// @ts-expect-error
|
|
29
|
+
appName: 'MyUpdatedApp',
|
|
30
|
+
// Error: Type '"MyUpdatedApp"' is not assignable to type '"MyApp"'.
|
|
31
|
+
|
|
32
|
+
// @ts-expect-error
|
|
33
|
+
version: '2.0.0',
|
|
34
|
+
// Error: Type '"2.0.0"' is not assignable to type '"1.0.0"'.
|
|
35
|
+
|
|
36
|
+
// @ts-expect-error
|
|
37
|
+
maxRetries: 1,
|
|
38
|
+
// Error: Type '1' is not assignable to type '3'.
|
|
39
|
+
|
|
40
|
+
// @ts-expect-error
|
|
41
|
+
enableLogging: false,
|
|
42
|
+
// Error: Type 'false' is not assignable to type 'true'.
|
|
43
|
+
|
|
44
|
+
// @ts-expect-error
|
|
45
|
+
apiUrl: 'https://api.myapp.com/v2',
|
|
46
|
+
// Error: Type '"https://api.myapp.com/v2"' is not assignable to type '"https://api.myapp.com/v1"'.
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
declare function updateConfigFixed(newConfig: LiteralToPrimitiveDeep<typeof config>): void;
|
|
50
|
+
|
|
51
|
+
updateConfigFixed({
|
|
52
|
+
appName: 'MyUpdatedApp',
|
|
53
|
+
version: '2.0.0',
|
|
54
|
+
maxRetries: 1,
|
|
55
|
+
enableLogging: false,
|
|
56
|
+
apiUrl: 'https://api.myapp.com/v2',
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
@category Type
|
|
61
|
+
@category Object
|
|
62
|
+
*/
|
|
63
|
+
export type LiteralToPrimitiveDeep<T> = T extends object
|
|
64
|
+
? T extends Array<infer U>
|
|
65
|
+
? Array<LiteralToPrimitiveDeep<U>>
|
|
66
|
+
: {
|
|
67
|
+
[K in keyof OmitIndexSignature<T>]: LiteralToPrimitiveDeep<T[K]>;
|
|
68
|
+
}
|
|
69
|
+
: LiteralToPrimitive<T>;
|
|
70
|
+
|
|
71
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Given a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) return the {@link Primitive | primitive type} it belongs to, or `never` if it's not a primitive.
|
|
3
|
+
|
|
4
|
+
Use-case: Working with generic types that may be literal types.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {LiteralToPrimitive} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
// No overloads needed to get the correct return type
|
|
11
|
+
function plus<T extends number | bigint | string>(x: T, y: T): LiteralToPrimitive<T> {
|
|
12
|
+
return x + (y as any);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
plus('a', 'b'); // string
|
|
16
|
+
plus(1, 2); // number
|
|
17
|
+
plus(1n, 2n); // bigint
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
@category Type
|
|
21
|
+
*/
|
|
22
|
+
export type LiteralToPrimitive<T> = T extends number
|
|
23
|
+
? number
|
|
24
|
+
: T extends bigint
|
|
25
|
+
? bigint
|
|
26
|
+
: T extends string
|
|
27
|
+
? string
|
|
28
|
+
: T extends boolean
|
|
29
|
+
? boolean
|
|
30
|
+
: T extends symbol
|
|
31
|
+
? symbol
|
|
32
|
+
: T extends null
|
|
33
|
+
? null
|
|
34
|
+
: T extends undefined
|
|
35
|
+
? undefined
|
|
36
|
+
: never;
|
|
37
|
+
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type {Primitive} from './primitive.d.ts';
|
|
2
|
+
|
|
3
|
+
export type _LiteralStringUnion<T> = LiteralUnion<T, string>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
|
|
7
|
+
|
|
8
|
+
Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
|
|
9
|
+
|
|
10
|
+
This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
|
|
11
|
+
|
|
12
|
+
@example
|
|
13
|
+
```
|
|
14
|
+
import type {LiteralUnion} from 'type-fest';
|
|
15
|
+
|
|
16
|
+
// Before
|
|
17
|
+
|
|
18
|
+
type Pet = 'dog' | 'cat' | string;
|
|
19
|
+
|
|
20
|
+
const petWithoutAutocomplete: Pet = '';
|
|
21
|
+
// Start typing in your TypeScript-enabled IDE.
|
|
22
|
+
// You **will not** get auto-completion for `dog` and `cat` literals.
|
|
23
|
+
|
|
24
|
+
// After
|
|
25
|
+
|
|
26
|
+
type Pet2 = LiteralUnion<'dog' | 'cat', string>;
|
|
27
|
+
|
|
28
|
+
const petWithAutoComplete: Pet2 = '';
|
|
29
|
+
// You **will** get auto-completion for `dog` and `cat` literals.
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
@category Type
|
|
33
|
+
*/
|
|
34
|
+
export type LiteralUnion<
|
|
35
|
+
LiteralType,
|
|
36
|
+
BaseType extends Primitive,
|
|
37
|
+
> = LiteralType | (BaseType & Record<never, never>);
|
|
38
|
+
|
|
39
|
+
export {};
|