-
Notifications
You must be signed in to change notification settings - Fork 679
/
schema.ts
329 lines (281 loc) · 9.51 KB
/
schema.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* Contains interfaces which describe the JSON output. Each interface is related to a specific type of serializer.
*
* ## Plugins
* Plugins which modify the serialization process can use declaration merging
* to add custom properties to the exported interfaces.
* For example, if your custom serializer adds a property to all {@link Reflection} objects:
* ```ts
* declare module 'typedoc/dist/lib/serialization/schema' {
* export interface AbstractReflection {
* myCustomProp: boolean
* }
* }
* ```
*
* If a plugin defines a new Model type, {@link ModelToObject} will not pick up the serializer type and
* the resulting type will not be included in the return type of {@link Serializer.toObject}.
* To fix this, use declaration merging to augment the {@link Serializer} class.
* ```ts
* declare module 'typedoc/dist/lib/serialization/serializer' {
* export interface Serializer {
* toObject(value: CustomModel, obj?: Partial<CustomModel>): CustomOutput
* }
* }
* ```
*
* For documentation on the JSON output properties, view the corresponding model.
* @module
*/
import type * as M from "../models";
/**
* Describes the mapping from Model types to the corresponding JSON output type.
*/
export type ModelToObject<T> = T extends Array<infer U>
? _ModelToObject<U>[]
: _ModelToObject<T>;
// Order matters here. Some types are subtypes of other types.
type _ModelToObject<T> =
// Reflections
T extends Primitive
? T
: T extends M.ReflectionGroup
? ReflectionGroup
: T extends M.ReflectionCategory
? ReflectionCategory
: T extends M.SignatureReflection
? SignatureReflection
: T extends M.ParameterReflection
? ParameterReflection
: T extends M.DeclarationReflection
? DeclarationReflection
: T extends M.TypeParameterReflection
? TypeParameterReflection
: T extends M.ProjectReflection
? ProjectReflection
: T extends M.ContainerReflection
? ContainerReflection
: T extends M.ReferenceReflection
? ReferenceReflection
: T extends M.Reflection
? Reflection
: // Types
T extends M.SomeType
? TypeKindMap[T["type"]]
: T extends M.Type
? SomeType
: // Miscellaneous
T extends M.Comment
? Comment
: T extends M.CommentTag
? CommentTag
: T extends M.CommentDisplayPart
? CommentDisplayPart
: T extends M.SourceReference
? SourceReference
: never;
type Primitive = string | number | undefined | null | boolean;
// Separate helper so that we can trigger distribution.
type ToSerialized<T> = T extends Primitive
? T
: T extends bigint
? { value: string; negative: boolean }
: ModelToObject<T>;
/**
* Helper to describe a set of serialized properties. Primitive types are returned
* directly, while other models are first passed through ModelToObject.
* This helper removes the readonly modifier from properties since the result of serialization
* is a plain object that consumers may modify as they choose, TypeDoc doesn't care.
*/
type S<T, K extends keyof T> = {
-readonly [K2 in K]: ToSerialized<T[K2]>;
};
// Reflections
export interface ReflectionGroup
extends S<M.ReflectionGroup, "title" | "categories"> {
children?: M.ReflectionGroup["children"][number]["id"][];
}
export interface ReflectionCategory extends S<M.ReflectionCategory, "title"> {
children?: M.ReflectionCategory["children"][number]["id"][];
}
export interface ReferenceReflection
extends DeclarationReflection,
S<M.ReferenceReflection, never> {
/**
* -1 if the reference refers to a symbol that does not exist in the documentation.
* Otherwise, the reflection ID.
*/
target: number;
}
export interface SignatureReflection
extends Reflection,
S<
M.SignatureReflection,
| "parameters"
| "type"
| "overwrites"
| "inheritedFrom"
| "implementationOf"
> {
// Weird not to call this typeParameters... preserving backwards compatibility for now.
typeParameter?: ModelToObject<M.SignatureReflection["typeParameters"]>;
}
export interface ParameterReflection
extends Reflection,
S<M.ParameterReflection, "type" | "defaultValue"> {}
export interface DeclarationReflection
extends ContainerReflection,
S<
M.DeclarationReflection,
| "type"
| "signatures"
| "indexSignature"
| "defaultValue"
| "overwrites"
| "inheritedFrom"
| "implementationOf"
| "extendedTypes"
| "extendedBy"
| "implementedTypes"
| "implementedBy"
| "getSignature"
| "setSignature"
| "typeParameters"
> {}
export interface TypeParameterReflection
extends Reflection,
S<M.TypeParameterReflection, "type" | "default" | "varianceModifier"> {}
// Nothing extra yet.
export interface ProjectReflection extends ContainerReflection {}
export interface ContainerReflection
extends Reflection,
S<
M.ContainerReflection,
"children" | "groups" | "categories" | "sources"
> {}
export interface Reflection
extends S<M.Reflection, "id" | "name" | "kind" | "comment"> {
flags: ReflectionFlags;
}
// Types
export type SomeType = ModelToObject<M.SomeType>;
export type TypeKindMap = {
array: ArrayType;
conditional: ConditionalType;
indexedAccess: IndexedAccessType;
inferred: InferredType;
intersection: IntersectionType;
intrinsic: IntrinsicType;
literal: LiteralType;
mapped: MappedType;
optional: OptionalType;
predicate: PredicateType;
query: QueryType;
reference: ReferenceType;
reflection: ReflectionType;
rest: RestType;
"template-literal": TemplateLiteralType;
tuple: TupleType;
"named-tuple-member": NamedTupleMemberType;
typeOperator: TypeOperatorType;
union: UnionType;
unknown: UnknownType;
};
export interface ArrayType
extends Type,
S<M.ArrayType, "type" | "elementType"> {}
export interface ConditionalType
extends Type,
S<
M.ConditionalType,
"type" | "checkType" | "extendsType" | "trueType" | "falseType"
> {}
export interface IndexedAccessType
extends Type,
S<M.IndexedAccessType, "type" | "indexType" | "objectType"> {}
export interface InferredType
extends Type,
S<M.InferredType, "type" | "name" | "constraint"> {}
export interface IntersectionType
extends Type,
S<M.IntersectionType, "type" | "types"> {}
export interface IntrinsicType
extends Type,
S<M.IntrinsicType, "type" | "name"> {}
export interface OptionalType
extends Type,
S<M.OptionalType, "type" | "elementType"> {}
export interface PredicateType
extends Type,
S<M.PredicateType, "type" | "name" | "asserts" | "targetType"> {}
export interface QueryType extends Type, S<M.QueryType, "type" | "queryType"> {}
export interface ReferenceType
extends Type,
S<
M.ReferenceType,
"type" | "name" | "typeArguments" | "package" | "qualifiedName"
> {
id?: number;
}
export interface ReflectionType extends Type, S<M.ReflectionType, "type"> {
declaration?: ModelToObject<M.ReflectionType["declaration"]>;
}
export interface RestType extends Type, S<M.RestType, "type" | "elementType"> {}
export interface LiteralType extends Type, S<M.LiteralType, "type" | "value"> {}
export interface TupleType extends Type, S<M.TupleType, "type"> {
elements?: ModelToObject<M.TupleType["elements"]>;
}
export interface NamedTupleMemberType
extends Type,
S<M.NamedTupleMember, "type" | "name" | "isOptional" | "element"> {}
export interface TemplateLiteralType
extends Type,
S<M.TemplateLiteralType, "type" | "head"> {
tail: [SomeType, string][];
}
export interface MappedType
extends Type,
S<
M.MappedType,
| "type"
| "parameter"
| "parameterType"
| "templateType"
| "readonlyModifier"
| "optionalModifier"
| "nameType"
> {}
export interface TypeOperatorType
extends Type,
S<M.TypeOperatorType, "type" | "operator" | "target"> {}
export interface UnionType extends Type, S<M.UnionType, "type" | "types"> {}
export interface UnknownType extends Type, S<M.UnknownType, "type" | "name"> {}
export interface Type {}
// Miscellaneous
type BoolKeys<T> = {
[K in keyof T]-?: T[K] extends boolean ? K : never;
}[keyof T];
export interface ReflectionFlags
extends Partial<S<M.ReflectionFlags, BoolKeys<M.ReflectionFlags>>> {}
export interface Comment extends Partial<S<M.Comment, "blockTags" | "label">> {
summary: CommentDisplayPart[];
modifierTags?: string[];
}
export interface CommentTag extends S<M.CommentTag, "tag" | "name"> {
content: CommentDisplayPart[];
}
/**
* If `target` is a number, it is a reflection ID. If a string, it is a URL.
* `target` will only be set for `@link`, `@linkcode`, and `@linkplain` tags.
*/
export type CommentDisplayPart =
| { kind: "text"; text: string }
| { kind: "code"; text: string }
| {
kind: "inline-tag";
tag: `@${string}`;
text: string;
target?: string | number;
};
export interface SourceReference
extends S<M.SourceReference, "fileName" | "line" | "character" | "url"> {}