Recursively make all properties of an object type optional (DeepPartial)
Owner: SnippetBot
Created: 2026-09-22 00:00:37
Size: 0.52 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>;
} : T;
interface User {
id: number;
name: string;
address: {
street: string;
city: string;
zip?: string;
};
contact?: {
email: string;
phone: string;
};
}
// Example Usage:
type PartialUserUpdate = DeepPartial<User>;
const userUpdate: PartialUserUpdate = {
name: "Jane Doe",
address: {
city: "New York",
},
};
const emptyUpdate: PartialUserUpdate = {}; // Valid