/
partialUtil.ts
78 lines (75 loc) · 2.52 KB
/
partialUtil.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type {
ZodArray,
ZodNullable,
ZodObject,
ZodOptional,
ZodRawShape,
ZodTuple,
ZodTupleItems,
ZodTypeAny,
} from "../index.ts";
export namespace partialUtil {
// export type DeepPartial<T extends AnyZodObject> = T extends AnyZodObject
// ? ZodObject<
// { [k in keyof T["_shape"]]: InternalDeepPartial<T["_shape"][k]> },
// T["_unknownKeys"],
// T["_catchall"]
// >
// : T extends ZodArray<infer Type, infer Card>
// ? ZodArray<InternalDeepPartial<Type>, Card>
// : ZodOptional<T>;
// {
// // optional: T extends ZodOptional<ZodTypeAny> ? T : ZodOptional<T>;
// // array: T extends ZodArray<infer Type> ? ZodArray<DeepPartial<Type>> : never;
// object: T extends AnyZodObject
// ? ZodObject<
// { [k in keyof T["_shape"]]: DeepPartial<T["_shape"][k]> },
// T["_unknownKeys"],
// T["_catchall"]
// >
// : never;
// rest: ReturnType<T["optional"]>; // ZodOptional<T>;
// }[T extends AnyZodObject
// ? "object" // T extends ZodOptional<any> // ? 'optional' // :
// : "rest"];
export type DeepPartial<T extends ZodTypeAny> =
T extends ZodObject<ZodRawShape>
? ZodObject<
{ [k in keyof T["shape"]]: ZodOptional<DeepPartial<T["shape"][k]>> },
T["_def"]["unknownKeys"],
T["_def"]["catchall"]
>
: T extends ZodArray<infer Type, infer Card>
? ZodArray<DeepPartial<Type>, Card>
: T extends ZodOptional<infer Type>
? ZodOptional<DeepPartial<Type>>
: T extends ZodNullable<infer Type>
? ZodNullable<DeepPartial<Type>>
: T extends ZodTuple<infer Items>
? {
[k in keyof Items]: Items[k] extends ZodTypeAny
? DeepPartial<Items[k]>
: never;
} extends infer PI
? PI extends ZodTupleItems
? ZodTuple<PI>
: never
: never
: T;
// {
// // optional: T extends ZodOptional<ZodTypeAny> ? T : ZodOptional<T>;
// // array: T extends ZodArray<infer Type> ? ZodArray<DeepPartial<Type>> : never;
// object: T extends ZodObject<infer Shape, infer Params, infer Catchall>
// ? ZodOptional<
// ZodObject<
// { [k in keyof Shape]: DeepPartial<Shape[k]> },
// Params,
// Catchall
// >
// >
// : never;
// rest: ReturnType<T["optional"]>;
// }[T extends ZodObject<any>
// ? "object" // T extends ZodOptional<any> // ? 'optional' // :
// : "rest"];
}