type-fest 2.16.0 → 2.18.1
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 +3 -1
- package/package.json +1 -1
- package/readme.md +6 -39
- package/source/camel-case.d.ts +1 -1
- package/source/delimiter-case.d.ts +1 -1
- package/source/get.d.ts +1 -1
- package/source/internal.d.ts +6 -0
- package/source/jsonify.d.ts +20 -21
- package/source/partial-deep.d.ts +48 -16
- package/source/partial-on-undefined-deep.d.ts +70 -0
- package/source/spread.d.ts +85 -0
- package/source/utilities.d.ts +0 -5
package/index.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ export {RequireAtLeastOne} from './source/require-at-least-one';
|
|
|
14
14
|
export {RequireExactlyOne} from './source/require-exactly-one';
|
|
15
15
|
export {RequireAllOrNone} from './source/require-all-or-none';
|
|
16
16
|
export {RemoveIndexSignature} from './source/remove-index-signature';
|
|
17
|
-
export {PartialDeep} from './source/partial-deep';
|
|
17
|
+
export {PartialDeep, PartialDeepOptions} from './source/partial-deep';
|
|
18
|
+
export {PartialOnUndefinedDeep, PartialOnUndefinedDeepOptions} from './source/partial-on-undefined-deep';
|
|
18
19
|
export {ReadonlyDeep} from './source/readonly-deep';
|
|
19
20
|
export {LiteralUnion} from './source/literal-union';
|
|
20
21
|
export {Promisable} from './source/promisable';
|
|
@@ -61,6 +62,7 @@ export {OptionalKeysOf} from './source/optional-keys-of';
|
|
|
61
62
|
export {HasOptionalKeys} from './source/has-optional-keys';
|
|
62
63
|
export {RequiredKeysOf} from './source/required-keys-of';
|
|
63
64
|
export {HasRequiredKeys} from './source/has-required-keys';
|
|
65
|
+
export {Spread} from './source/spread';
|
|
64
66
|
|
|
65
67
|
// Template literal types
|
|
66
68
|
export {CamelCase} from './source/camel-case';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -40,41 +40,6 @@
|
|
|
40
40
|
<br>
|
|
41
41
|
<br>
|
|
42
42
|
<br>
|
|
43
|
-
<a href="https://neverinstall.com/spaces/devtools?utm_source=github&utm_medium=sponsor&utm_campaign=sindre#gh-light-mode-only">
|
|
44
|
-
<div>
|
|
45
|
-
<img src="https://sindresorhus.com/assets/thanks/neverinstall-logo-light.svg" width="200" alt="neverinstall">
|
|
46
|
-
</div>
|
|
47
|
-
<br>
|
|
48
|
-
<b>All your favourite IDE's now available on the cloud</b>
|
|
49
|
-
<div>
|
|
50
|
-
<sub>
|
|
51
|
-
Neverinstall gives you an uninterrupted development experience and improved accessibility,
|
|
52
|
-
<br>
|
|
53
|
-
allowing you to code faster, better and on-the-go on your favourite IDEs like
|
|
54
|
-
<br>
|
|
55
|
-
Android Studio, VS Code, Jupyter and PyCharm using your browser.
|
|
56
|
-
</sub>
|
|
57
|
-
</div>
|
|
58
|
-
</a>
|
|
59
|
-
<a href="https://neverinstall.com/spaces/devtools?utm_source=github&utm_medium=sponsor&utm_campaign=sindre#gh-dark-mode-only">
|
|
60
|
-
<div>
|
|
61
|
-
<img src="https://sindresorhus.com/assets/thanks/neverinstall-logo-dark.svg" width="200" alt="neverinstall">
|
|
62
|
-
</div>
|
|
63
|
-
<br>
|
|
64
|
-
<b>All your favourite IDE's now available on the cloud</b>
|
|
65
|
-
<div>
|
|
66
|
-
<sub>
|
|
67
|
-
Neverinstall gives you an uninterrupted development experience and improved accessibility,
|
|
68
|
-
<br>
|
|
69
|
-
allowing you to code faster, better and on-the-go on your favourite IDEs like
|
|
70
|
-
<br>
|
|
71
|
-
Android Studio, VS Code, Jupyter and PyCharm using your browser.
|
|
72
|
-
</sub>
|
|
73
|
-
</div>
|
|
74
|
-
</a>
|
|
75
|
-
<br>
|
|
76
|
-
<br>
|
|
77
|
-
<br>
|
|
78
43
|
<a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-light-mode-only">
|
|
79
44
|
<div>
|
|
80
45
|
<img src="https://sindresorhus.com/assets/thanks/anvil-logo-light.svg" width="200" alt="Anvil">
|
|
@@ -169,6 +134,7 @@ Click the type names for complete docs.
|
|
|
169
134
|
- [`RequireAllOrNone`](source/require-all-or-none.d.ts) - Create a type that requires all of the given keys or none of the given keys.
|
|
170
135
|
- [`RemoveIndexSignature`](source/remove-index-signature.d.ts) - Create a type that only has explicitly defined properties, absent of any index signatures.
|
|
171
136
|
- [`PartialDeep`](source/partial-deep.d.ts) - Create a deeply optional version of another type. Use [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) if you only need one level deep.
|
|
137
|
+
- [`PartialOnUndefinedDeep`](source/partial-on-undefined-deep.d.ts) - Create a deep version of another type where all keys accepting `undefined` type are set to optional.
|
|
172
138
|
- [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of an `object`/`Map`/`Set`/`Array` type. Use [`Readonly<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) if you only need one level deep.
|
|
173
139
|
- [`LiteralUnion`](source/literal-union.d.ts) - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729).
|
|
174
140
|
- [`Opaque`](source/opaque.d.ts) - Create an [opaque type](https://codemix.com/opaque-types-in-javascript/).
|
|
@@ -196,6 +162,7 @@ Click the type names for complete docs.
|
|
|
196
162
|
- [`HasOptionalKeys`](source/has-optional-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any optional fields.
|
|
197
163
|
- [`RequiredKeysOf`](source/required-keys-of.d.ts) - Extract all required keys from the given type.
|
|
198
164
|
- [`HasRequiredKeys`](source/has-required-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any required fields.
|
|
165
|
+
- [`Spread`](source/spread.d.ts) - Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
|
|
199
166
|
|
|
200
167
|
### JSON
|
|
201
168
|
|
|
@@ -839,7 +806,7 @@ There are many advanced types most users don't know about.
|
|
|
839
806
|
type T2 = Uppercase<'foo' | 'bar'>; // 'FOO' | 'BAR'
|
|
840
807
|
|
|
841
808
|
type T3<S extends string> = Uppercase<`aB${S}`>;
|
|
842
|
-
type T4 =
|
|
809
|
+
type T4 = T3<'xYz'>; // 'ABXYZ'
|
|
843
810
|
|
|
844
811
|
type T5 = Uppercase<string>; // string
|
|
845
812
|
type T6 = Uppercase<any>; // any
|
|
@@ -860,7 +827,7 @@ There are many advanced types most users don't know about.
|
|
|
860
827
|
type T2 = Lowercase<'FOO' | 'BAR'>; // 'foo' | 'bar'
|
|
861
828
|
|
|
862
829
|
type T3<S extends string> = Lowercase<`aB${S}`>;
|
|
863
|
-
type T4 =
|
|
830
|
+
type T4 = T3<'xYz'>; // 'abxyz'
|
|
864
831
|
|
|
865
832
|
type T5 = Lowercase<string>; // string
|
|
866
833
|
type T6 = Lowercase<any>; // any
|
|
@@ -881,7 +848,7 @@ There are many advanced types most users don't know about.
|
|
|
881
848
|
type T2 = Capitalize<'foo' | 'bar'>; // 'Foo' | 'Bar'
|
|
882
849
|
|
|
883
850
|
type T3<S extends string> = Capitalize<`aB${S}`>;
|
|
884
|
-
type T4 =
|
|
851
|
+
type T4 = T3<'xYz'>; // 'ABxYz'
|
|
885
852
|
|
|
886
853
|
type T5 = Capitalize<string>; // string
|
|
887
854
|
type T6 = Capitalize<any>; // any
|
|
@@ -902,7 +869,7 @@ There are many advanced types most users don't know about.
|
|
|
902
869
|
type T2 = Uncapitalize<'Foo' | 'Bar'>; // 'foo' | 'bar'
|
|
903
870
|
|
|
904
871
|
type T3<S extends string> = Uncapitalize<`AB${S}`>;
|
|
905
|
-
type T4 =
|
|
872
|
+
type T4 = T3<'xYz'>; // 'aBxYz'
|
|
906
873
|
|
|
907
874
|
type T5 = Uncapitalize<string>; // string
|
|
908
875
|
type T6 = Uncapitalize<any>; // any
|
package/source/camel-case.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {UpperCaseCharacters, WordSeparators} from '../source/
|
|
1
|
+
import type {UpperCaseCharacters, WordSeparators} from '../source/internal';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Unlike a simpler split, this one includes the delimiter splitted on in the resulting array literal. This is to enable splitting on, for example, upper-case characters.
|
package/source/get.d.ts
CHANGED
package/source/internal.d.ts
CHANGED
|
@@ -51,3 +51,9 @@ The reason a simple `keyof Union` does not work is because `keyof` always return
|
|
|
51
51
|
@link https://stackoverflow.com/a/49402091
|
|
52
52
|
*/
|
|
53
53
|
export type KeysOfUnion<T> = T extends T ? keyof T : never;
|
|
54
|
+
|
|
55
|
+
export type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
|
|
56
|
+
|
|
57
|
+
export type WordSeparators = '-' | '_' | ' ';
|
|
58
|
+
|
|
59
|
+
export type StringDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
package/source/jsonify.d.ts
CHANGED
|
@@ -67,25 +67,24 @@ type Jsonify<T> =
|
|
|
67
67
|
// Note: The use of tuples in this first condition side-steps distributive conditional types
|
|
68
68
|
// (see https://github.com/microsoft/TypeScript/issues/29368#issuecomment-453529532)
|
|
69
69
|
[Extract<T, NotJsonable | bigint>] extends [never]
|
|
70
|
-
? T extends PositiveInfinity | NegativeInfinity
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
: T extends
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
: never // Otherwise any other non-object is removed
|
|
70
|
+
? T extends PositiveInfinity | NegativeInfinity ? null
|
|
71
|
+
: T extends JsonPrimitive ? T // Primitive is acceptable
|
|
72
|
+
: T extends object
|
|
73
|
+
// Any object with toJSON is special case
|
|
74
|
+
? T extends {toJSON(): infer J} ? (() => J) extends (() => JsonValue) // Is J assignable to JsonValue?
|
|
75
|
+
? J // Then T is Jsonable and its Jsonable value is J
|
|
76
|
+
: never // Not Jsonable because its toJSON() method does not return JsonValue
|
|
77
|
+
// Instanced primitives are objects
|
|
78
|
+
: T extends Number ? number
|
|
79
|
+
: T extends String ? string
|
|
80
|
+
: T extends Boolean ? boolean
|
|
81
|
+
: T extends Map<any, any> | Set<any> ? {}
|
|
82
|
+
: T extends TypedArray ? Record<string, number>
|
|
83
|
+
: T extends any[]
|
|
84
|
+
? {[I in keyof T]: T[I] extends NotJsonable ? null : Jsonify<T[I]>}
|
|
85
|
+
: {[P in keyof T as P extends symbol ? never
|
|
86
|
+
: T[P] extends NotJsonable ? never
|
|
87
|
+
: P
|
|
88
|
+
]: Jsonify<Required<T>[P]>} // Recursive call for its children
|
|
89
|
+
: never // Otherwise any other non-object is removed
|
|
91
90
|
: never; // Otherwise non-JSONable type union was found not empty
|
package/source/partial-deep.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import type {BuiltIns} from './internal';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
@see PartialDeep
|
|
5
|
+
*/
|
|
6
|
+
export interface PartialDeepOptions {
|
|
7
|
+
/**
|
|
8
|
+
Whether to affect the individual elements of arrays and tuples.
|
|
9
|
+
|
|
10
|
+
@default true
|
|
11
|
+
*/
|
|
12
|
+
readonly recurseIntoArrays?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
3
15
|
/**
|
|
4
16
|
Create a type from another type with all keys and nested keys set to optional.
|
|
5
17
|
|
|
@@ -28,54 +40,74 @@ const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {
|
|
|
28
40
|
settings = applySavedSettings({textEditor: {fontWeight: 500}});
|
|
29
41
|
```
|
|
30
42
|
|
|
43
|
+
By default, this also affects array and tuple types:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
import type {PartialDeep} from 'type-fest';
|
|
47
|
+
|
|
48
|
+
interface Settings {
|
|
49
|
+
languages: string[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const partialSettings: PartialDeep<Settings> = {
|
|
53
|
+
languages: [undefined]
|
|
54
|
+
};
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
If this is undesirable, you can pass `{recurseIntoArrays: false}` as the second type argument.
|
|
58
|
+
|
|
31
59
|
@category Object
|
|
32
60
|
@category Array
|
|
33
61
|
@category Set
|
|
34
62
|
@category Map
|
|
35
63
|
*/
|
|
36
|
-
export type PartialDeep<T> = T extends BuiltIns
|
|
64
|
+
export type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIns
|
|
37
65
|
? T
|
|
38
66
|
: T extends Map<infer KeyType, infer ValueType>
|
|
39
|
-
? PartialMapDeep<KeyType, ValueType>
|
|
67
|
+
? PartialMapDeep<KeyType, ValueType, Options>
|
|
40
68
|
: T extends Set<infer ItemType>
|
|
41
|
-
? PartialSetDeep<ItemType>
|
|
69
|
+
? PartialSetDeep<ItemType, Options>
|
|
42
70
|
: T extends ReadonlyMap<infer KeyType, infer ValueType>
|
|
43
|
-
? PartialReadonlyMapDeep<KeyType, ValueType>
|
|
71
|
+
? PartialReadonlyMapDeep<KeyType, ValueType, Options>
|
|
44
72
|
: T extends ReadonlySet<infer ItemType>
|
|
45
|
-
? PartialReadonlySetDeep<ItemType>
|
|
73
|
+
? PartialReadonlySetDeep<ItemType, Options>
|
|
46
74
|
: T extends ((...arguments: any[]) => unknown)
|
|
47
75
|
? T | undefined
|
|
48
76
|
: T extends object
|
|
49
|
-
? T extends
|
|
50
|
-
?
|
|
51
|
-
?
|
|
52
|
-
:
|
|
53
|
-
|
|
77
|
+
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
|
78
|
+
? Options['recurseIntoArrays'] extends false // If they opt out of array testing, just use the original type
|
|
79
|
+
? T
|
|
80
|
+
: ItemType[] extends T // Test for arrays (non-tuples) specifically
|
|
81
|
+
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
|
|
82
|
+
? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
|
|
83
|
+
: Array<PartialDeep<ItemType | undefined, Options>>
|
|
84
|
+
: PartialObjectDeep<T, Options> // Tuples behave properly
|
|
85
|
+
: PartialObjectDeep<T, Options>
|
|
54
86
|
: unknown;
|
|
55
87
|
|
|
56
88
|
/**
|
|
57
89
|
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
|
|
58
90
|
*/
|
|
59
|
-
interface PartialMapDeep<KeyType, ValueType> extends Map<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
|
|
91
|
+
interface PartialMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> extends Map<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>> {}
|
|
60
92
|
|
|
61
93
|
/**
|
|
62
94
|
Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
|
|
63
95
|
*/
|
|
64
|
-
interface PartialSetDeep<T> extends Set<PartialDeep<T>> {}
|
|
96
|
+
interface PartialSetDeep<T, Options extends PartialDeepOptions> extends Set<PartialDeep<T, Options>> {}
|
|
65
97
|
|
|
66
98
|
/**
|
|
67
99
|
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
|
|
68
100
|
*/
|
|
69
|
-
interface PartialReadonlyMapDeep<KeyType, ValueType> extends ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
|
|
101
|
+
interface PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> extends ReadonlyMap<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>> {}
|
|
70
102
|
|
|
71
103
|
/**
|
|
72
104
|
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
|
|
73
105
|
*/
|
|
74
|
-
interface PartialReadonlySetDeep<T> extends ReadonlySet<PartialDeep<T>> {}
|
|
106
|
+
interface PartialReadonlySetDeep<T, Options extends PartialDeepOptions> extends ReadonlySet<PartialDeep<T, Options>> {}
|
|
75
107
|
|
|
76
108
|
/**
|
|
77
109
|
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
|
|
78
110
|
*/
|
|
79
|
-
type PartialObjectDeep<ObjectType extends object> = {
|
|
80
|
-
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType]>
|
|
111
|
+
type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions> = {
|
|
112
|
+
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
|
|
81
113
|
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type {BuiltIns} from './internal';
|
|
2
|
+
import type {Merge} from './merge';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
@see PartialOnUndefinedDeep
|
|
6
|
+
*/
|
|
7
|
+
export interface PartialOnUndefinedDeepOptions {
|
|
8
|
+
/**
|
|
9
|
+
Whether to affect the individual elements of arrays and tuples.
|
|
10
|
+
|
|
11
|
+
@default false
|
|
12
|
+
*/
|
|
13
|
+
readonly recurseIntoArrays?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
Create a deep version of another type where all keys accepting `undefined` type are set to optional.
|
|
18
|
+
|
|
19
|
+
This utility type is recursive, transforming at any level deep. By default, it does not affect arrays and tuples items unless you explicitly pass `{recurseIntoArrays: true}` as the second type argument.
|
|
20
|
+
|
|
21
|
+
Use-cases:
|
|
22
|
+
- Make all properties of a type that can be undefined optional to not have to specify keys with undefined value.
|
|
23
|
+
|
|
24
|
+
@example
|
|
25
|
+
```
|
|
26
|
+
import type {PartialOnUndefinedDeep} from 'type-fest';
|
|
27
|
+
|
|
28
|
+
interface Settings {
|
|
29
|
+
optionA: string;
|
|
30
|
+
optionB: number | undefined;
|
|
31
|
+
subOption: {
|
|
32
|
+
subOptionA: boolean;
|
|
33
|
+
subOptionB: boolean | undefined;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const testSettings: PartialOnUndefinedDeep<Settings> = {
|
|
38
|
+
optionA: 'foo',
|
|
39
|
+
// 👉 optionB is now optional and can be omitted
|
|
40
|
+
subOption: {
|
|
41
|
+
subOptionA: true,
|
|
42
|
+
// 👉 subOptionB is now optional as well and can be omitted
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
@category Object
|
|
48
|
+
*/
|
|
49
|
+
export type PartialOnUndefinedDeep<T, Options extends PartialOnUndefinedDeepOptions = {}> = T extends Record<any, any> | undefined
|
|
50
|
+
? {[KeyType in keyof T as undefined extends T[KeyType] ? KeyType : never]?: PartialOnUndefinedDeepValue<T[KeyType], Options>} extends infer U // Make a partial type with all value types accepting undefined (and set them optional)
|
|
51
|
+
? Merge<{[KeyType in keyof T as KeyType extends keyof U ? never : KeyType]: PartialOnUndefinedDeepValue<T[KeyType], Options>}, U> // Join all remaining keys not treated in U
|
|
52
|
+
: never // Should not happen
|
|
53
|
+
: T;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
Utility type to get the value type by key and recursively call `PartialOnUndefinedDeep` to transform sub-objects.
|
|
57
|
+
*/
|
|
58
|
+
type PartialOnUndefinedDeepValue<T, Options extends PartialOnUndefinedDeepOptions> = T extends BuiltIns | ((...arguments: any[]) => unknown)
|
|
59
|
+
? T
|
|
60
|
+
: T extends ReadonlyArray<infer U> // Test if type is array or tuple
|
|
61
|
+
? Options['recurseIntoArrays'] extends true // Check if option is activated
|
|
62
|
+
? U[] extends T // Check if array not tuple
|
|
63
|
+
? readonly U[] extends T
|
|
64
|
+
? ReadonlyArray<PartialOnUndefinedDeep<U, Options>> // Readonly array treatment
|
|
65
|
+
: Array<PartialOnUndefinedDeep<U, Options>> // Mutable array treatment
|
|
66
|
+
: PartialOnUndefinedDeep<{[Key in keyof T]: PartialOnUndefinedDeep<T[Key], Options>}, Options> // Tuple treatment
|
|
67
|
+
: T
|
|
68
|
+
: T extends Record<any, any> | undefined
|
|
69
|
+
? PartialOnUndefinedDeep<T, Options>
|
|
70
|
+
: unknown;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {RequiredKeysOf} from './required-keys-of';
|
|
3
|
+
import type {Simplify} from './simplify';
|
|
4
|
+
|
|
5
|
+
type SpreadObject<FirstType extends object, SecondType extends object> = {
|
|
6
|
+
[Key in keyof FirstType]: Key extends keyof SecondType
|
|
7
|
+
? FirstType[Key] | Required<SecondType>[Key]
|
|
8
|
+
: FirstType[Key];
|
|
9
|
+
} & Pick<
|
|
10
|
+
SecondType,
|
|
11
|
+
RequiredKeysOf<SecondType> | Exclude<keyof SecondType, keyof FirstType>
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
type TupleOrArray = readonly [...unknown[]];
|
|
15
|
+
|
|
16
|
+
type SpreadTupleOrArray<
|
|
17
|
+
FirstType extends TupleOrArray,
|
|
18
|
+
SecondType extends TupleOrArray,
|
|
19
|
+
> = Array<FirstType[number] | SecondType[number]>;
|
|
20
|
+
|
|
21
|
+
type Spreadable = object | TupleOrArray;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
|
|
25
|
+
|
|
26
|
+
@example
|
|
27
|
+
```
|
|
28
|
+
import type {Spread} from 'type-fest';
|
|
29
|
+
|
|
30
|
+
type Foo = {
|
|
31
|
+
a: number;
|
|
32
|
+
b?: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type Bar = {
|
|
36
|
+
b?: number;
|
|
37
|
+
c: boolean;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const foo = {a: 1, b: '2'};
|
|
41
|
+
const bar = {c: false};
|
|
42
|
+
const fooBar = {...foo, ...bar};
|
|
43
|
+
|
|
44
|
+
type FooBar = Spread<Foo, Bar>;
|
|
45
|
+
// type FooBar = {
|
|
46
|
+
// a: number;
|
|
47
|
+
// b?: string | number | undefined;
|
|
48
|
+
// c: boolean;
|
|
49
|
+
// }
|
|
50
|
+
|
|
51
|
+
const baz = (argument: FooBar) => {
|
|
52
|
+
// Do something
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
baz(fooBar);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
@example
|
|
59
|
+
```
|
|
60
|
+
import type {Spread} from 'type-fest';
|
|
61
|
+
|
|
62
|
+
const foo = [1, 2, 3];
|
|
63
|
+
const bar = ['4', '5', '6'];
|
|
64
|
+
|
|
65
|
+
const fooBar = [...foo, ...bar];
|
|
66
|
+
type FooBar = Spread<typeof foo, typeof bar>;
|
|
67
|
+
// FooBar = (string | number)[]
|
|
68
|
+
|
|
69
|
+
const baz = (argument: FooBar) => {
|
|
70
|
+
// Do something
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
baz(fooBar);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
@category Object
|
|
77
|
+
*/
|
|
78
|
+
export type Spread<
|
|
79
|
+
FirstType extends Spreadable,
|
|
80
|
+
SecondType extends Spreadable,
|
|
81
|
+
> = FirstType extends TupleOrArray
|
|
82
|
+
? SecondType extends TupleOrArray
|
|
83
|
+
? SpreadTupleOrArray<FirstType, SecondType>
|
|
84
|
+
: Simplify<SpreadObject<FirstType, SecondType>>
|
|
85
|
+
: Simplify<SpreadObject<FirstType, SecondType>>;
|
package/source/utilities.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
|
|
2
|
-
|
|
3
|
-
export type WordSeparators = '-' | '_' | ' ';
|
|
4
|
-
|
|
5
|
-
export type StringDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|