如何从TypeScript编译器API中的TypeReferenceType获取声明的类型?

时间:2017-11-16 23:25:55

标签: typescript typescript-compiler-api

我目前正在使用TypeScript 2.5.3编译器API。如果有帮助,我可以更新到2.6.x.我试图弄清楚如何获得父类的TypeNode。我以为我可以使用TypeChecker,但是没有想到它。以下是Mocha index.d.ts中的一些相关部分:

    export class Base {
        constructor(runner: IRunner);
    }

    export class Doc extends Base { }

在阅读ClassDeclaration时,我可以访问其HeritageClause。每个HeritageClause都是ExpressionWithTypeArguments,即TypeReferenceType。如何获得ClassDeclaration的{​​{1}}?

这适用于https://github.com/fable-compiler/ts2fable/issues/83

2017-11-17周五更新,这里是TypeScript示例。这输出:

Base
heritage clause: extends Base
contextual type: undefined

1 个答案:

答案 0 :(得分:0)

tsutils库的作者向我展示了如何使用isTypeReferencetp.target.symbol.declarations来获取我正在寻找的类声明。这是在TypeScript issue

import * as ts from "typescript";
import * as fs from "fs";
import {isTypeReference, isClassDeclaration} from "tsutils";

const tsPath = "node_modules/@types/mocha/index.d.ts"
const options: ts.CompilerOptions = { target: ts.ScriptTarget.ES2015 }
const host = ts.createCompilerHost(options, true);
const program = ts.createProgram([tsPath], options, host)
const checker = program.getTypeChecker()
const sourceFile = program.getSourceFile(tsPath)
// console.log(sourceFile)

function visitNode(node: ts.Node){
    switch (node.kind) {
        case ts.SyntaxKind.ClassDeclaration:
            const cd = (<ts.ClassDeclaration>node);
            // console.log(cd.name.getText())
            if(cd.name.getText() === "Doc"){
                printBaseClass(cd)
            }
    }
    ts.forEachChild(node, visitNode)
}

ts.forEachChild(sourceFile, visitNode);

function getFullTypeName (tp: ts.Type){
    if(tp.symbol){
        return checker.getFullyQualifiedName(tp.symbol)
    } else {
        return ""
    }
}

function getFullNodeName (nd: ts.Node){
    const tp =checker.getTypeAtLocation(nd)
    return getFullTypeName(tp)
}

function printBaseClass(cd: ts.ClassDeclaration){
    // console.log(cd)
    for(const hc of cd.heritageClauses) {
        console.log("heritage clause: " + hc.getText())
        console.log("hc name " + getFullNodeName(hc))
        for(const hctp of hc.types){
            let tp = checker.getTypeFromTypeNode(hctp)
            console.log(getFullTypeName(tp))

            if (isTypeReference(tp)) {
                let dcs = tp.target.symbol.declarations
                console.log(dcs.length + " declarations")
                for(const dc of dcs){
                    console.log(getFullNodeName(dc))
                    if(isClassDeclaration(dc)){
                        for(const mb of dc.members){
                            console.log("  member " + mb.getText())
                        }
                    }
                }
            }
        }
    }
}