UNPKG

17.8 kBTypeScriptView Raw
1import * as ts from "typescript";
2import type { Context } from "../converter";
3import type { Reflection } from "./reflections/abstract";
4import type { DeclarationReflection } from "./reflections/declaration";
5import type { ProjectReflection } from "./reflections/project";
6import type { Serializer, JSONOutput, Deserializer } from "../serialization";
7import { ReflectionSymbolId } from "./reflections/ReflectionSymbolId";
8import type { DeclarationReference } from "../converter/comments/declarationReference";
9import { type CommentDisplayPart } from "./comments";
10/**
11 * Base class of all type definitions.
12 * @category Types
13 */
14export declare abstract class Type {
15 /**
16 * The type name identifier.
17 */
18 abstract readonly type: keyof TypeKindMap;
19 /**
20 * Return a string representation of this type.
21 */
22 toString(): string;
23 /**
24 * Visit this type, returning the value returned by the visitor.
25 */
26 visit<T>(visitor: TypeVisitor<T>): T;
27 visit<T>(visitor: Partial<TypeVisitor<T>>): T | undefined;
28 stringify(context: TypeContext): string;
29 abstract toObject(serializer: Serializer): JSONOutput.SomeType;
30 fromObject(_de: Deserializer, _obj: JSONOutput.SomeType): void;
31 abstract needsParenthesis(context: TypeContext): boolean;
32 /**
33 * Implementation method for `toString`. `needsParenthesis` will be used to determine if
34 * the returned string should be wrapped in parenthesis.
35 */
36 protected abstract getTypeString(): string;
37}
38/** @category Types */
39export interface TypeKindMap {
40 array: ArrayType;
41 conditional: ConditionalType;
42 indexedAccess: IndexedAccessType;
43 inferred: InferredType;
44 intersection: IntersectionType;
45 intrinsic: IntrinsicType;
46 literal: LiteralType;
47 mapped: MappedType;
48 optional: OptionalType;
49 predicate: PredicateType;
50 query: QueryType;
51 reference: ReferenceType;
52 reflection: ReflectionType;
53 rest: RestType;
54 templateLiteral: TemplateLiteralType;
55 tuple: TupleType;
56 namedTupleMember: NamedTupleMember;
57 typeOperator: TypeOperatorType;
58 union: UnionType;
59 unknown: UnknownType;
60}
61export type TypeVisitor<T = void> = {
62 [K in TypeKind]: (type: TypeKindMap[K]) => T;
63};
64export declare function makeRecursiveVisitor(visitor: Partial<TypeVisitor>): TypeVisitor;
65/** @category Types */
66export type TypeKind = keyof TypeKindMap;
67/** @category Types */
68export type SomeType = TypeKindMap[keyof TypeKindMap];
69/**
70 * Enumeration that can be used when traversing types to track the location of recursion.
71 * Used by TypeDoc internally to track when to output parenthesis when rendering.
72 * @enum
73 */
74export declare const TypeContext: {
75 readonly none: "none";
76 readonly templateLiteralElement: "templateLiteralElement";
77 readonly arrayElement: "arrayElement";
78 readonly indexedAccessElement: "indexedAccessElement";
79 readonly conditionalCheck: "conditionalCheck";
80 readonly conditionalExtends: "conditionalExtends";
81 readonly conditionalTrue: "conditionalTrue";
82 readonly conditionalFalse: "conditionalFalse";
83 readonly indexedIndex: "indexedIndex";
84 readonly indexedObject: "indexedObject";
85 readonly inferredConstraint: "inferredConstraint";
86 readonly intersectionElement: "intersectionElement";
87 readonly mappedName: "mappedName";
88 readonly mappedParameter: "mappedParameter";
89 readonly mappedTemplate: "mappedTemplate";
90 readonly optionalElement: "optionalElement";
91 readonly predicateTarget: "predicateTarget";
92 readonly queryTypeTarget: "queryTypeTarget";
93 readonly typeOperatorTarget: "typeOperatorTarget";
94 readonly referenceTypeArgument: "referenceTypeArgument";
95 readonly restElement: "restElement";
96 readonly tupleElement: "tupleElement";
97 readonly unionElement: "unionElement";
98};
99export type TypeContext = (typeof TypeContext)[keyof typeof TypeContext];
100/**
101 * Represents an array type.
102 *
103 * ```ts
104 * let value: string[];
105 * ```
106 * @category Types
107 */
108export declare class ArrayType extends Type {
109 elementType: SomeType;
110 readonly type = "array";
111 /**
112 * @param elementType The type of the elements in the array.
113 */
114 constructor(elementType: SomeType);
115 protected getTypeString(): string;
116 needsParenthesis(): boolean;
117 toObject(serializer: Serializer): JSONOutput.ArrayType;
118}
119/**
120 * Represents a conditional type.
121 *
122 * ```ts
123 * let value: Check extends Extends ? True : False;
124 * ```
125 * @category Types
126 */
127export declare class ConditionalType extends Type {
128 checkType: SomeType;
129 extendsType: SomeType;
130 trueType: SomeType;
131 falseType: SomeType;
132 readonly type = "conditional";
133 constructor(checkType: SomeType, extendsType: SomeType, trueType: SomeType, falseType: SomeType);
134 protected getTypeString(): string;
135 needsParenthesis(context: TypeContext): boolean;
136 toObject(serializer: Serializer): JSONOutput.ConditionalType;
137}
138/**
139 * Represents an indexed access type.
140 * @category Types
141 */
142export declare class IndexedAccessType extends Type {
143 objectType: SomeType;
144 indexType: SomeType;
145 readonly type = "indexedAccess";
146 constructor(objectType: SomeType, indexType: SomeType);
147 protected getTypeString(): string;
148 needsParenthesis(): boolean;
149 toObject(serializer: Serializer): JSONOutput.IndexedAccessType;
150}
151/**
152 * Represents an inferred type, U in the example below.
153 *
154 * ```ts
155 * type Z = Promise<string> extends Promise<infer U> : never
156 * ```
157 * @category Types
158 */
159export declare class InferredType extends Type {
160 name: string;
161 constraint?: SomeType | undefined;
162 readonly type = "inferred";
163 constructor(name: string, constraint?: SomeType | undefined);
164 protected getTypeString(): string;
165 needsParenthesis(context: TypeContext): boolean;
166 toObject(serializer: Serializer): JSONOutput.InferredType;
167}
168/**
169 * Represents an intersection type.
170 *
171 * ```ts
172 * let value: A & B;
173 * ```
174 * @category Types
175 */
176export declare class IntersectionType extends Type {
177 types: SomeType[];
178 readonly type = "intersection";
179 constructor(types: SomeType[]);
180 protected getTypeString(): string;
181 needsParenthesis(context: TypeContext): boolean;
182 toObject(serializer: Serializer): JSONOutput.IntersectionType;
183}
184/**
185 * Represents an intrinsic type like `string` or `boolean`.
186 *
187 * ```ts
188 * let value: number;
189 * ```
190 * @category Types
191 */
192export declare class IntrinsicType extends Type {
193 name: string;
194 readonly type = "intrinsic";
195 constructor(name: string);
196 protected getTypeString(): string;
197 toObject(): JSONOutput.IntrinsicType;
198 needsParenthesis(): boolean;
199}
200/**
201 * Represents a literal type.
202 *
203 * ```ts
204 * type A = "A"
205 * type B = 1
206 * ```
207 * @category Types
208 */
209export declare class LiteralType extends Type {
210 value: string | number | boolean | null | bigint;
211 readonly type = "literal";
212 constructor(value: string | number | boolean | null | bigint);
213 /**
214 * Return a string representation of this type.
215 */
216 protected getTypeString(): string;
217 needsParenthesis(): boolean;
218 toObject(): JSONOutput.LiteralType;
219}
220/**
221 * Represents a mapped type.
222 *
223 * ```ts
224 * { -readonly [K in Parameter as Name]?: Template }
225 * ```
226 * @category Types
227 */
228export declare class MappedType extends Type {
229 parameter: string;
230 parameterType: SomeType;
231 templateType: SomeType;
232 readonlyModifier?: ("+" | "-") | undefined;
233 optionalModifier?: ("+" | "-") | undefined;
234 nameType?: SomeType | undefined;
235 readonly type = "mapped";
236 constructor(parameter: string, parameterType: SomeType, templateType: SomeType, readonlyModifier?: ("+" | "-") | undefined, optionalModifier?: ("+" | "-") | undefined, nameType?: SomeType | undefined);
237 protected getTypeString(): string;
238 needsParenthesis(): boolean;
239 toObject(serializer: Serializer): JSONOutput.MappedType;
240}
241/**
242 * Represents an optional type
243 * ```ts
244 * type Z = [1, 2?]
245 * // ^^
246 * ```
247 * @category Types
248 */
249export declare class OptionalType extends Type {
250 elementType: SomeType;
251 readonly type = "optional";
252 constructor(elementType: SomeType);
253 protected getTypeString(): string;
254 needsParenthesis(): boolean;
255 toObject(serializer: Serializer): JSONOutput.OptionalType;
256}
257/**
258 * Represents a type predicate.
259 *
260 * ```ts
261 * function isString(x: unknown): x is string {}
262 * function assert(condition: boolean): asserts condition {}
263 * ```
264 * @category Types
265 */
266export declare class PredicateType extends Type {
267 name: string;
268 asserts: boolean;
269 targetType?: SomeType | undefined;
270 readonly type = "predicate";
271 /**
272 * Create a new PredicateType instance.
273 *
274 * @param name The identifier name which is tested by the predicate.
275 * @param asserts True if the type is of the form `asserts val is string`,
276 * false if the type is of the form `val is string`
277 * @param targetType The type that the identifier is tested to be.
278 * May be undefined if the type is of the form `asserts val`.
279 * Will be defined if the type is of the form `asserts val is string` or `val is string`.
280 */
281 constructor(name: string, asserts: boolean, targetType?: SomeType | undefined);
282 /**
283 * Return a string representation of this type.
284 */
285 protected getTypeString(): string;
286 needsParenthesis(): boolean;
287 toObject(serializer: Serializer): JSONOutput.PredicateType;
288}
289/**
290 * Represents a type that is constructed by querying the type of a reflection.
291 * ```ts
292 * const x = 1
293 * type Z = typeof x // query on reflection for x
294 * ```
295 * @category Types
296 */
297export declare class QueryType extends Type {
298 queryType: ReferenceType;
299 readonly type = "query";
300 constructor(queryType: ReferenceType);
301 protected getTypeString(): string;
302 /**
303 * @privateRemarks
304 * An argument could be made that this ought to return true for indexedObject
305 * since precedence is different than on the value side... if someone really cares
306 * they can easily use a custom theme to change this.
307 */
308 needsParenthesis(): boolean;
309 toObject(serializer: Serializer): JSONOutput.QueryType;
310}
311/**
312 * Represents a type that refers to another reflection like a class, interface or enum.
313 *
314 * ```ts
315 * let value: MyClass<T>;
316 * ```
317 * @category Types
318 */
319export declare class ReferenceType extends Type {
320 readonly type = "reference";
321 /**
322 * The name of the referenced type.
323 *
324 * If the symbol cannot be found because it's not part of the documentation this
325 * can be used to represent the type.
326 */
327 name: string;
328 /**
329 * The type arguments of this reference.
330 */
331 typeArguments?: SomeType[];
332 /**
333 * The resolved reflection.
334 */
335 get reflection(): Reflection | undefined;
336 /**
337 * If not resolved, the symbol id of the reflection, otherwise undefined.
338 */
339 get symbolId(): ReflectionSymbolId | undefined;
340 /**
341 * Checks if this type is a reference type because it uses a name, but is intentionally not pointing
342 * to a reflection. This happens for type parameters and when representing a mapped type.
343 */
344 isIntentionallyBroken(): boolean;
345 /**
346 * Convert this reference type to a declaration reference used for resolution of external types.
347 */
348 toDeclarationReference(): DeclarationReference;
349 /**
350 * The fully qualified name of the referenced type, relative to the file it is defined in.
351 * This will usually be the same as `name`, unless namespaces are used.
352 */
353 qualifiedName: string;
354 /**
355 * The package that this type is referencing.
356 */
357 package?: string;
358 /**
359 * If this reference type refers to a reflection defined by a project not being rendered,
360 * points to the url that this type should be linked to.
361 */
362 externalUrl?: string;
363 /**
364 * If set, no warnings about something not being exported should be created
365 * since this may be referring to a type created with `infer X` which will not
366 * be registered on the project.
367 */
368 refersToTypeParameter: boolean;
369 /**
370 * If set, will prefer reflections with {@link ReflectionKind | ReflectionKinds} which represent
371 * values rather than those which represent types.
372 */
373 preferValues: boolean;
374 private _target;
375 private _project;
376 private constructor();
377 static createResolvedReference(name: string, target: Reflection | number, project: ProjectReflection | null): ReferenceType;
378 static createSymbolReference(symbol: ts.Symbol, context: Context, name?: string): ReferenceType;
379 /**
380 * This is used for type parameters, which don't actually point to something,
381 * and also for temporary references which will be cleaned up with real references
382 * later during conversion.
383 * @internal
384 */
385 static createBrokenReference(name: string, project: ProjectReflection): ReferenceType;
386 protected getTypeString(): string;
387 needsParenthesis(): boolean;
388 toObject(serializer: Serializer): JSONOutput.ReferenceType;
389 fromObject(de: Deserializer, obj: JSONOutput.ReferenceType): void;
390}
391/**
392 * Represents a type which has it's own reflection like literal types.
393 * This type will likely go away at some point and be replaced by a dedicated
394 * `ObjectType`. Allowing reflections to be nested within types causes much
395 * pain in the rendering code.
396 *
397 * ```ts
398 * let value: { a: string, b: number };
399 * ```
400 * @category Types
401 */
402export declare class ReflectionType extends Type {
403 declaration: DeclarationReflection;
404 readonly type = "reflection";
405 constructor(declaration: DeclarationReflection);
406 protected getTypeString(): "Function" | "Object";
407 needsParenthesis(): boolean;
408 toObject(serializer: Serializer): JSONOutput.ReflectionType;
409}
410/**
411 * Represents a rest type
412 * ```ts
413 * type Z = [1, ...2[]]
414 * // ^^^^^^
415 * ```
416 * @category Types
417 */
418export declare class RestType extends Type {
419 elementType: SomeType;
420 readonly type = "rest";
421 constructor(elementType: SomeType);
422 protected getTypeString(): string;
423 needsParenthesis(): boolean;
424 toObject(serializer: Serializer): JSONOutput.RestType;
425}
426/**
427 * TS 4.1 template literal types
428 * ```ts
429 * type Z = `${'a' | 'b'}${'a' | 'b'}`
430 * ```
431 * @category Types
432 */
433export declare class TemplateLiteralType extends Type {
434 head: string;
435 tail: [SomeType, string][];
436 readonly type = "templateLiteral";
437 constructor(head: string, tail: [SomeType, string][]);
438 protected getTypeString(): string;
439 needsParenthesis(): boolean;
440 toObject(serializer: Serializer): JSONOutput.TemplateLiteralType;
441}
442/**
443 * Represents a tuple type.
444 *
445 * ```ts
446 * let value: [string, boolean];
447 * ```
448 * @category Types
449 */
450export declare class TupleType extends Type {
451 elements: SomeType[];
452 readonly type = "tuple";
453 /**
454 * @param elements The ordered type elements of the tuple type.
455 */
456 constructor(elements: SomeType[]);
457 protected getTypeString(): string;
458 needsParenthesis(): boolean;
459 toObject(serializer: Serializer): JSONOutput.TupleType;
460}
461/**
462 * Represents a named member of a tuple type.
463 *
464 * ```ts
465 * let value: [name: string];
466 * ```
467 * @category Types
468 */
469export declare class NamedTupleMember extends Type {
470 name: string;
471 isOptional: boolean;
472 element: SomeType;
473 readonly type = "namedTupleMember";
474 constructor(name: string, isOptional: boolean, element: SomeType);
475 /**
476 * Return a string representation of this type.
477 */
478 protected getTypeString(): string;
479 needsParenthesis(): boolean;
480 toObject(serializer: Serializer): JSONOutput.NamedTupleMemberType;
481}
482/**
483 * Represents a type operator type.
484 *
485 * ```ts
486 * class A {}
487 * class B<T extends keyof A> {}
488 * ```
489 * @category Types
490 */
491export declare class TypeOperatorType extends Type {
492 target: SomeType;
493 operator: "keyof" | "unique" | "readonly";
494 readonly type = "typeOperator";
495 constructor(target: SomeType, operator: "keyof" | "unique" | "readonly");
496 protected getTypeString(): string;
497 needsParenthesis(context: TypeContext): boolean;
498 toObject(serializer: Serializer): JSONOutput.TypeOperatorType;
499}
500/**
501 * Represents an union type.
502 *
503 * ```ts
504 * let value: string | string[];
505 * ```
506 * @category Types
507 */
508export declare class UnionType extends Type {
509 types: SomeType[];
510 readonly type = "union";
511 /**
512 * If present, there should be as many items in this array as there are items in the {@link types} array.
513 *
514 * This member is only valid on unions which are on {@link DeclarationReflection.type | DeclarationReflection.type} with a
515 * {@link ReflectionKind} `kind` of `TypeAlias`. Specifying it on any other union is undefined behavior.
516 */
517 elementSummaries?: CommentDisplayPart[][];
518 constructor(types: SomeType[]);
519 protected getTypeString(): string;
520 needsParenthesis(context: TypeContext): boolean;
521 fromObject(de: Deserializer, obj: JSONOutput.UnionType): void;
522 toObject(serializer: Serializer): JSONOutput.UnionType;
523}
524/**
525 * Represents all unknown types that cannot be converted by TypeDoc.
526 * @category Types
527 */
528export declare class UnknownType extends Type {
529 readonly type = "unknown";
530 /**
531 * A string representation of the type as returned from TypeScript compiler.
532 */
533 name: string;
534 constructor(name: string);
535 protected getTypeString(): string;
536 /**
537 * Always returns true if not at the root level, we have no idea what's in here, so wrap it in parenthesis
538 * to be extra safe.
539 */
540 needsParenthesis(context: TypeContext): boolean;
541 toObject(): JSONOutput.UnknownType;
542}
543
\No newline at end of file