UNPKG

11.4 kBTypeScriptView Raw
1/**
2 * Contains interfaces which describe the JSON output. Each interface is related to a specific type of serializer.
3 *
4 * ## Plugins
5 * Plugins which modify the serialization process can use declaration merging
6 * to add custom properties to the exported interfaces.
7 * For example, if your custom serializer adds a property to all {@link Reflection} objects:
8 * ```ts
9 * declare module 'typedoc/dist/lib/serialization/schema' {
10 * export interface AbstractReflection {
11 * myCustomProp: boolean
12 * }
13 * }
14 * ```
15 *
16 * If a plugin defines a new Model type, {@link ModelToObject} will not pick up the serializer type and
17 * the resulting type will not be included in the return type of {@link Serializer.toObject}.
18 * To fix this, use declaration merging to augment the {@link Serializer} class.
19 * ```ts
20 * declare module 'typedoc/dist/lib/serialization/serializer' {
21 * export interface Serializer {
22 * toObject(value: CustomModel, obj?: Partial<CustomModel>): CustomOutput
23 * }
24 * }
25 * ```
26 *
27 * For documentation on the JSON output properties, view the corresponding model.
28 * @module
29 */
30import type * as M from "../models";
31import type { IfInternal } from "../utils";
32/**
33 * Describes the mapping from Model types to the corresponding JSON output type.
34 */
35export type ModelToObject<T> = [T] extends [Array<infer U>] ? ModelToObject<U>[] : [M.SomeType] extends [T] ? SomeType : _ModelToObject<T>;
36type _ModelToObject<T> = T extends Primitive ? T : Required<T> extends Required<M.ReflectionGroup> ? ReflectionGroup : Required<T> extends Required<M.ReflectionCategory> ? ReflectionCategory : T extends M.ReflectionVariant[keyof M.ReflectionVariant] ? ReflectionVariantMap[T["variant"]] : T extends M.SomeType ? TypeKindMap[T["type"]] : T extends M.Type ? SomeType : T extends M.Comment ? Comment : T extends M.CommentTag ? CommentTag : T extends M.CommentDisplayPart ? CommentDisplayPart : T extends M.SourceReference ? SourceReference : T extends M.FileRegistry ? FileRegistry : never;
37type Primitive = string | number | undefined | null | boolean;
38type ToSerialized<T> = T extends Primitive ? T : T extends bigint ? {
39 value: string;
40 negative: boolean;
41} : ModelToObject<T>;
42/**
43 * Helper to describe a set of serialized properties. Primitive types are returned
44 * directly, while other models are first passed through ModelToObject.
45 * This helper removes the readonly modifier from properties since the result of serialization
46 * is a plain object that consumers may modify as they choose, TypeDoc doesn't care.
47 */
48type S<T, K extends keyof T> = {
49 -readonly [K2 in K]: ToSerialized<T[K2]>;
50};
51export interface ReflectionSymbolId {
52 sourceFileName: string;
53 qualifiedName: string;
54}
55export interface ReflectionGroup extends S<M.ReflectionGroup, "title" | "description" | "categories"> {
56 children?: M.ReflectionGroup["children"][number]["id"][];
57}
58export interface ReflectionCategory extends S<M.ReflectionCategory, "title" | "description"> {
59 children?: M.ReflectionCategory["children"][number]["id"][];
60}
61/** @category Reflections */
62export interface ReflectionVariantMap {
63 declaration: DeclarationReflection;
64 param: ParameterReflection;
65 project: ProjectReflection;
66 reference: ReferenceReflection;
67 signature: SignatureReflection;
68 typeParam: TypeParameterReflection;
69 document: DocumentReflection;
70}
71/** @category Reflections */
72export type SomeReflection = ReflectionVariantMap[keyof ReflectionVariantMap];
73/** @category Reflections */
74export interface DocumentReflection extends Omit<Reflection, "variant">, S<M.DocumentReflection, "variant" | "content" | "relevanceBoost" | "children"> {
75 frontmatter: Record<string, unknown>;
76}
77/** @category Reflections */
78export interface ReferenceReflection extends Omit<DeclarationReflection, "variant">, S<M.ReferenceReflection, "variant"> {
79 /**
80 * -1 if the reference refers to a symbol that does not exist in the documentation.
81 * Otherwise, the reflection ID.
82 */
83 target: number;
84}
85/** @category Reflections */
86export interface SignatureReflection extends Omit<Reflection, "variant">, S<M.SignatureReflection, "variant" | "sources" | "parameters" | "typeParameters" | "type" | "overwrites" | "inheritedFrom" | "implementationOf"> {
87 /** @deprecated in 0.26, replaced with {@link typeParameters} */
88 typeParameter?: ModelToObject<M.TypeParameterReflection[]>;
89}
90/** @category Reflections */
91export interface ParameterReflection extends Omit<Reflection, "variant">, S<M.ParameterReflection, "variant" | "type" | "defaultValue"> {
92 variant: "param";
93}
94/** @category Reflections */
95export interface DeclarationReflection extends Omit<ContainerReflection, "variant">, S<M.DeclarationReflection, "variant" | "packageVersion" | "sources" | "relevanceBoost" | "type" | "signatures" | "indexSignatures" | "defaultValue" | "overwrites" | "inheritedFrom" | "implementationOf" | "extendedTypes" | "extendedBy" | "implementedTypes" | "implementedBy" | "getSignature" | "setSignature" | "typeParameters" | "readme"> {
96 /** @deprecated moved to {@link indexSignatures} with 0.26. */
97 indexSignature?: SignatureReflection;
98}
99/** @category Reflections */
100export interface TypeParameterReflection extends Omit<Reflection, "variant">, S<M.TypeParameterReflection, "variant" | "type" | "default" | "varianceModifier"> {
101}
102/** @category Reflections */
103export interface ProjectReflection extends Omit<ContainerReflection, "variant">, S<M.ProjectReflection, "variant" | "packageName" | "packageVersion" | "readme"> {
104 symbolIdMap: Record<number, ReflectionSymbolId> | IfInternal<undefined, never>;
105 files: FileRegistry;
106}
107/** @category Reflections */
108export interface ContainerReflection extends Reflection, S<M.ContainerReflection, "children" | "documents" | "groups" | "categories"> {
109 childrenIncludingDocuments?: number[];
110}
111/** @category Reflections */
112export interface Reflection extends S<M.Reflection, "id" | "variant" | "name" | "kind" | "comment"> {
113 flags: ReflectionFlags;
114}
115/** @category Types */
116export type SomeType = TypeKindMap[keyof TypeKindMap];
117/** @category Types */
118export type TypeKindMap = {
119 array: ArrayType;
120 conditional: ConditionalType;
121 indexedAccess: IndexedAccessType;
122 inferred: InferredType;
123 intersection: IntersectionType;
124 intrinsic: IntrinsicType;
125 literal: LiteralType;
126 mapped: MappedType;
127 optional: OptionalType;
128 predicate: PredicateType;
129 query: QueryType;
130 reference: ReferenceType;
131 reflection: ReflectionType;
132 rest: RestType;
133 templateLiteral: TemplateLiteralType;
134 tuple: TupleType;
135 namedTupleMember: NamedTupleMemberType;
136 typeOperator: TypeOperatorType;
137 union: UnionType;
138 unknown: UnknownType;
139};
140/** @category Types */
141export interface ArrayType extends Type, S<M.ArrayType, "type" | "elementType"> {
142}
143/** @category Types */
144export interface ConditionalType extends Type, S<M.ConditionalType, "type" | "checkType" | "extendsType" | "trueType" | "falseType"> {
145}
146/** @category Types */
147export interface IndexedAccessType extends Type, S<M.IndexedAccessType, "type" | "indexType" | "objectType"> {
148}
149/** @category Types */
150export interface InferredType extends Type, S<M.InferredType, "type" | "name" | "constraint"> {
151}
152/** @category Types */
153export interface IntersectionType extends Type, S<M.IntersectionType, "type" | "types"> {
154}
155/** @category Types */
156export interface IntrinsicType extends Type, S<M.IntrinsicType, "type" | "name"> {
157}
158/** @category Types */
159export interface OptionalType extends Type, S<M.OptionalType, "type" | "elementType"> {
160}
161/** @category Types */
162export interface PredicateType extends Type, S<M.PredicateType, "type" | "name" | "asserts" | "targetType"> {
163}
164/** @category Types */
165export interface QueryType extends Type, S<M.QueryType, "type" | "queryType"> {
166}
167/** @category Types */
168export interface ReferenceType extends Type, S<M.ReferenceType, "type" | "name" | "typeArguments" | "package" | "externalUrl"> {
169 target: number | ReflectionSymbolId;
170 qualifiedName?: string;
171 refersToTypeParameter?: boolean;
172 preferValues?: boolean;
173}
174/** @category Types */
175export interface ReflectionType extends Type, S<M.ReflectionType, "type" | "declaration"> {
176}
177/** @category Types */
178export interface RestType extends Type, S<M.RestType, "type" | "elementType"> {
179}
180/** @category Types */
181export interface LiteralType extends Type, S<M.LiteralType, "type" | "value"> {
182}
183/** @category Types */
184export interface TupleType extends Type, S<M.TupleType, "type"> {
185 elements?: ModelToObject<M.TupleType["elements"]>;
186}
187/** @category Types */
188export interface NamedTupleMemberType extends Type, S<M.NamedTupleMember, "type" | "name" | "isOptional" | "element"> {
189}
190/** @category Types */
191export interface TemplateLiteralType extends Type, S<M.TemplateLiteralType, "type" | "head"> {
192 tail: [SomeType, string][];
193}
194/** @category Types */
195export interface MappedType extends Type, S<M.MappedType, "type" | "parameter" | "parameterType" | "templateType" | "readonlyModifier" | "optionalModifier" | "nameType"> {
196}
197/** @category Types */
198export interface TypeOperatorType extends Type, S<M.TypeOperatorType, "type" | "operator" | "target"> {
199}
200/** @category Types */
201export interface UnionType extends Type, S<M.UnionType, "type" | "types" | "elementSummaries"> {
202}
203/** @category Types */
204export interface UnknownType extends Type, S<M.UnknownType, "type" | "name"> {
205}
206/** @category Types */
207export interface Type {
208}
209type BoolKeys<T> = {
210 [K in keyof T]-?: T[K] extends boolean ? K : never;
211}[keyof T];
212export interface ReflectionFlags extends Partial<S<M.ReflectionFlags, BoolKeys<M.ReflectionFlags>>> {
213}
214/** @category Comments */
215export interface Comment extends Partial<S<M.Comment, "blockTags" | "label">> {
216 summary: CommentDisplayPart[];
217 modifierTags?: `@${string}`[];
218}
219/** @category Comments */
220export interface CommentTag extends S<M.CommentTag, "tag" | "name"> {
221 content: CommentDisplayPart[];
222}
223/**
224 * @see {@link M.CommentDisplayPart}
225 * @category Comments
226 */
227export type CommentDisplayPart = {
228 kind: "text";
229 text: string;
230} | {
231 kind: "code";
232 text: string;
233} | InlineTagDisplayPart | RelativeLinkDisplayPart;
234/**
235 * If `target` is a number, it is a reflection ID. If a string, it is a URL.
236 * `target` will only be set for `@link`, `@linkcode`, and `@linkplain` tags.
237 * @category Comments
238 */
239export interface InlineTagDisplayPart {
240 kind: "inline-tag";
241 tag: `@${string}`;
242 text: string;
243 target?: string | number | ReflectionSymbolId;
244 tsLinkText?: string;
245}
246/**
247 * This is used for relative links within comments/documents.
248 * It is used to mark pieces of text which need to be replaced
249 * to make links work properly.
250 */
251export interface RelativeLinkDisplayPart {
252 kind: "relative-link";
253 /**
254 * The original relative text from the parsed comment.
255 */
256 text: string;
257 /**
258 * File ID, if present
259 */
260 target?: number;
261}
262export interface SourceReference extends S<M.SourceReference, "fileName" | "line" | "character" | "url"> {
263}
264export interface FileRegistry {
265 /** Relative path according to the serialization root */
266 entries: Record<number, string>;
267 /** File ID to reflection ID */
268 reflections: Record<number, number>;
269}
270export {};