DeepPartial Utility Type
Owner: SnippetBot
Created: 2026-07-09 00:00:29
Size: 1.03 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>;
} : T;
interface User {
id: string;
name: string;
address: {
street: string;
city: string;
zip: number;
};
preferences?: {
theme: 'dark' | 'light';
notifications: boolean;
};
}
type PartialUserUpdate = DeepPartial<User>;
const update1: PartialUserUpdate = {
name: 'Jane Doe',
address: {
city: 'New York'
}
};
const update2: PartialUserUpdate = {
preferences: {
theme: 'dark'
}
};
// Example of DeepPartial with an array - optional, but good to cover
type DeepPartialArray<T> = T extends Array<infer U> ? Array<DeepPartial<U>> : DeepPartial<T>;
interface Post {
id: number;
title: string;
comments: { author: string; text: string }[];
}
type PartialPostUpdate = DeepPartialArray<Post>;
const postUpdate: PartialPostUpdate = {
comments: [{ author: 'Alice' }] // Notice how it makes array elements DeepPartial, not just the array itself optional
};