UNPKG

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