UNPKG

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