import ts from "typescript"; import { Config } from "../Config"; import { Context, NodeParser } from "../NodeParser"; import { SubNodeParser } from "../SubNodeParser"; import { AliasType } from "../Type/AliasType"; import { BaseType } from "../Type/BaseType"; import { ReferenceType } from "../Type/ReferenceType"; import { extendKey } from "../Utils/extendKey"; import { getKey } from "../Utils/nodeKey"; import { NeverType } from "../Type/NeverType"; export class TypeAliasNodeParser implements SubNodeParser { public constructor( protected typeChecker: ts.TypeChecker, protected childNodeParser: NodeParser, private config: Config ) {} public supportsNode(node: ts.TypeAliasDeclaration): boolean { return node.kind === ts.SyntaxKind.TypeAliasDeclaration; } public createType(node: ts.TypeAliasDeclaration, context: Context, reference?: ReferenceType): BaseType { if (node.typeParameters?.length) { for (const typeParam of node.typeParameters) { const nameSymbol = this.typeChecker.getSymbolAtLocation(typeParam.name)!; context.pushParameter(nameSymbol.name); if (typeParam.default) { const type = this.childNodeParser.createType(typeParam.default, context); context.setDefault(nameSymbol.name, type); } } } const id = this.getTypeId(node, context); const name = this.getTypeName(node, context); if (reference) { reference.setId(id); reference.setName(name); } const type = this.childNodeParser.createType(node.type, context); // My Code ------ if (type === undefined) { return new NeverType(); } // --------------- if (type instanceof NeverType) { return new NeverType(); } return new AliasType(id, type); } protected getTypeId(node: ts.TypeAliasDeclaration, context: Context): string { return extendKey(`alias-${getKey(node, context)}`, node, context, this.config); } protected getTypeName(node: ts.TypeAliasDeclaration, context: Context): string { const argumentIds = context.getArguments().map((arg) => arg?.getName()); const fullName = node.name.getText(); return argumentIds.length ? `${fullName}<${argumentIds.join(",")}>` : fullName; } }