UNPKG

4.88 kBTypeScriptView Raw
1import type {BuiltIns} from './internal';
2
3/**
4@see {@link PartialDeep}
5*/
6export type PartialDeepOptions = {
7 /**
8 Whether to affect the individual elements of arrays and tuples.
9
10 @default false
11 */
12 readonly recurseIntoArrays?: boolean;
13
14 /**
15 Allows `undefined` values in non-tuple arrays.
16
17 - When set to `true`, elements of non-tuple arrays can be `undefined`.
18 - When set to `false`, only explicitly defined elements are allowed in non-tuple arrays, ensuring stricter type checking.
19
20 @default true
21
22 @example
23 You can prevent `undefined` values in non-tuple arrays by passing `{recurseIntoArrays: true; allowUndefinedInNonTupleArrays: false}` as the second type argument:
24
25 ```
26 import type {PartialDeep} from 'type-fest';
27
28 type Settings = {
29 languages: string[];
30 };
31
32 declare const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true; allowUndefinedInNonTupleArrays: false}>;
33
34 partialSettings.languages = [undefined]; // Error
35 partialSettings.languages = []; // Ok
36 ```
37 */
38 readonly allowUndefinedInNonTupleArrays?: boolean;
39};
40
41/**
42Create a type from another type with all keys and nested keys set to optional.
43
44Use-cases:
45- Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
46- Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
47
48@example
49```
50import type {PartialDeep} from 'type-fest';
51
52const settings: Settings = {
53 textEditor: {
54 fontSize: 14,
55 fontColor: '#000000',
56 fontWeight: 400
57 },
58 autocomplete: false,
59 autosave: true
60};
61
62const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {
63 return {...settings, ...savedSettings};
64}
65
66settings = applySavedSettings({textEditor: {fontWeight: 500}});
67```
68
69By default, this does not affect elements in array and tuple types. You can change this by passing `{recurseIntoArrays: true}` as the second type argument:
70
71```
72import type {PartialDeep} from 'type-fest';
73
74type Settings = {
75 languages: string[];
76}
77
78const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
79 languages: [undefined]
80};
81```
82
83@see {@link PartialDeepOptions}
84
85@category Object
86@category Array
87@category Set
88@category Map
89*/
90export type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIns | (((...arguments_: any[]) => unknown)) | (new (...arguments_: any[]) => unknown)
91 ? T
92 : T extends Map<infer KeyType, infer ValueType>
93 ? PartialMapDeep<KeyType, ValueType, Options>
94 : T extends Set<infer ItemType>
95 ? PartialSetDeep<ItemType, Options>
96 : T extends ReadonlyMap<infer KeyType, infer ValueType>
97 ? PartialReadonlyMapDeep<KeyType, ValueType, Options>
98 : T extends ReadonlySet<infer ItemType>
99 ? PartialReadonlySetDeep<ItemType, Options>
100 : T extends object
101 ? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
102 ? Options['recurseIntoArrays'] extends true
103 ? ItemType[] extends T // Test for arrays (non-tuples) specifically
104 ? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
105 ? ReadonlyArray<PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
106 : Array<PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
107 : PartialObjectDeep<T, Options> // Tuples behave properly
108 : T // If they don't opt into array testing, just use the original type
109 : PartialObjectDeep<T, Options>
110 : unknown;
111
112/**
113Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
114*/
115type PartialMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & Map<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
116
117/**
118Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
119*/
120type PartialSetDeep<T, Options extends PartialDeepOptions> = {} & Set<PartialDeep<T, Options>>;
121
122/**
123Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
124*/
125type PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & ReadonlyMap<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
126
127/**
128Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
129*/
130type PartialReadonlySetDeep<T, Options extends PartialDeepOptions> = {} & ReadonlySet<PartialDeep<T, Options>>;
131
132/**
133Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
134*/
135type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions> = {
136 [KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
137};
138
\No newline at end of file