-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
42 lines (36 loc) · 767 Bytes
/
index.ts
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
// https://www.youtube.com/watch?v=AhzjPAtzGTs
type DeepPartial<T> = T extends Function
? T
: T extends Array<infer InferredArrayMember>
? DeepPartialArray<InferredArrayMember>
: T extends object
? DeepPartialObject<T>
: T | undefined;
interface DeepPartialArray<T> extends Array<DeepPartial<T>> {}
type DeepPartialObject<T> = {
[Key in keyof T]?: DeepPartial<T[Key]>;
};
interface Post {
id: string;
comments: { value: string }[];
meta: {
name: string;
description: string;
};
}
const post: DeepPartial<Post> = {
id: "1",
meta: {
description: "123",
},
};
/**
* TypeScript Partial works only 1 level deep
*/
const secondPost: Partial<Post> = {
id: "1",
//@ts-expect-error
meta: {
description: "123",
},
};