forked from wix-incubator/as-typed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.d.ts
196 lines (179 loc) · 5.98 KB
/
index.d.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
declare namespace AsTypedInternal {
type ConstSchema<ConstType> = {
const?: ConstType;
enum?: ConstType[];
} & (ConstType extends number
? { type: "number" | "integer" }
: ConstType extends string
? { type: "string" }
: ConstType extends boolean
? { type: "boolean" }
: never);
interface TupleSchema<
TupleAsArray extends any[],
AdditionalItemsSchema = null
> {
type: "array";
items: TupleAsArray;
additionalItems?: AdditionalItemsSchema;
}
type ResolveObjectRequiredProps<Props, RequiredPropNames> = [
RequiredPropNames
] extends [keyof Props]
? {
[name in RequiredPropNames]: Resolve<Props[name]>;
}
: unknown;
type ResolveObjectOptionalProps<Props, RequiredPropNames> = Props extends null
? unknown
: unknown extends Props
? unknown
: [RequiredPropNames] extends [keyof Props]
? [keyof Props] extends [RequiredPropNames]
? unknown
: {
[K in Exclude<keyof Props, RequiredPropNames>]?: Resolve<Props[K]>;
}
: {
[K in keyof Props]?: Resolve<Props[K]>;
};
type ResolveObjectAdditionalProps<AdditionalProperties> =
unknown extends AdditionalProperties
? unknown
: AdditionalProperties extends false | undefined
? unknown
: AdditionalProperties extends true
? { [key: string]: unknown }
: { [key: string]: Resolve<AdditionalProperties> };
type ResolveObject<Props, RequiredPropNames, AdditionalProperties> =
ResolveObjectRequiredProps<Props, RequiredPropNames> &
ResolveObjectOptionalProps<Props, RequiredPropNames> &
ResolveObjectAdditionalProps<AdditionalProperties>;
type AsTypedTupleSchema<Tuple extends unknown[]> = Tuple extends []
? []
: Tuple extends [infer A, ...infer Rest]
? [Resolve<A>, ...AsTypedTupleSchema<Rest>]
: never;
type AsTypedTupleSchemaWithAdditional<
Tuple extends unknown[],
Additional
> = unknown extends Additional
? AsTypedTupleSchema<Tuple>
: [...AsTypedTupleSchema<Tuple>, ...Array<Resolve<Additional>>];
type ResolveNot<ValueType> =
// TODO: allow Not() for array/object types of specific schemas. Not easy.
| object
| any[]
| (ValueType extends { type: "null" } ? never : null)
| (ValueType extends { type: "number" | "integer" } ? never : number)
| (ValueType extends { type: "string" } ? never : string)
| (ValueType extends { type: "boolean" } ? never : boolean);
type Resolve<SchemaType> = SchemaType extends {
nullable: true;
}
? Resolve<Omit<SchemaType, "nullable">> | null
: SchemaType extends { type: "null" }
? null
: SchemaType extends ConstSchema<infer Value>
? Value
: SchemaType extends { type: "string" }
? string
: SchemaType extends { type: "boolean" }
? boolean
: SchemaType extends { type: "number" | "integer" }
? number
: SchemaType extends TupleSchema<infer TupleType, infer Additional>
? Additional extends null
? AsTypedTupleSchema<TupleType>
: AsTypedTupleSchemaWithAdditional<TupleType, Additional>
: SchemaType extends { not: infer T }
? ResolveNot<T>
: SchemaType extends { type: "array"; items: infer ValueType }
? Array<Resolve<ValueType>>
: SchemaType extends {
type: [infer Type];
}
? Resolve<Omit<SchemaType, "type"> & { type: Type }>
: SchemaType extends {
type: [infer Type, ...infer Rest];
}
?
| Resolve<Omit<SchemaType, "type"> & { type: Type }>
| Resolve<Omit<SchemaType, "type"> & { type: Rest }>
: SchemaType extends { oneOf: Array<infer Inner> }
? Resolve<Inner>
: SchemaType extends { anyOf: Array<infer Inner> }
? Resolve<Inner>
: SchemaType extends { allOf: [...infer Inner] }
? ResolveAllOf<Inner>
: SchemaType extends { if: infer If; then: infer Then; else?: infer Else }
? (Resolve<If> & Resolve<Then>) | Resolve<Else>
: SchemaType extends {
type?: "object";
required?: infer Required;
properties?: infer Props;
additionalProperties?: infer AdditionalProperties;
}
? ResolveObject<
Props,
Required extends undefined
? unknown
: Required extends string[]
? string extends Required[number]
? unknown
: Required[number]
: unknown,
AdditionalProperties
>
: never;
type ResolveAllOf<Tuple> = Tuple extends []
? unknown
: Tuple extends [infer Head, ...infer Tail]
? Resolve<Head> & ResolveAllOf<Tail>
: unknown;
type ResolvePath<Schema, Path> =
Path extends `${infer Prop}/${infer PathRest}`
? Prop extends keyof Schema
? ResolvePath<Schema[Prop], PathRest>
: never
: Path extends keyof Schema
? Schema[Path]
: never;
type LocateId<Candidates, Id> = Candidates extends { $id: Id }
? Candidates
: never;
type ResolveRefs<RootSchema, SchemaToResolve> = SchemaToResolve extends {
$ref: infer RefId;
}
? RefId extends `#/${infer Path}`
? ResolveRefs<RootSchema, ResolvePath<RootSchema, Path>>
: RootSchema extends { definitions: infer Definitions }
? ResolveRefs<RootSchema, LocateId<Definitions[keyof Definitions], RefId>>
: never
: SchemaToResolve extends object
? {
[P in keyof SchemaToResolve]: ResolveRefs<
RootSchema,
SchemaToResolve[P]
>;
}
: SchemaToResolve extends [...infer Tuple]
? [...ResolveRefs<RootSchema, Tuple>]
: SchemaToResolve;
type ResolveRootSchema<RootSchema> = Resolve<
ResolveRefs<RootSchema, RootSchema>
>;
type DeepUnReadonly<T> = T extends object
? {
-readonly [P in keyof T]: DeepUnReadonly<T[P]>;
}
: T;
type ExpandRecursively<T> = T extends object
? T extends infer O
? { [K in keyof O]: ExpandRecursively<O[K]> }
: never
: T;
}
export type AsTyped<Schema> = AsTypedInternal.ExpandRecursively<
AsTypedInternal.ResolveRootSchema<AsTypedInternal.DeepUnReadonly<Schema>>
>;