UNPKG

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