Hvordan får jeg den deklarerte typen fra en TypeReferenceType i TypeScript Compiler API?


Jeg bruker for øyeblikket TypeScript 2.5.3 Compiler API. Jeg kan oppdatere til 2.6.x hvis det hjelper. Jeg prøver å finne ut hvordan jeg kan få TypeNode for superklassen. Jeg trodde jeg kunne bruke TypeChecker, men har ikke funnet ut av det. Her er noen relevante deler fra Mocha index.d.ts:

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

    export class Doc extends Base { }

Mens jeg leser ClassDeclaration har jeg tilgang til dens HeritageClauses. Hver HeritageClause er et ExpressionWithTypeArguments, som er en TypeReferenceType. Hvordan får jeg ClassDeclaration for Base?

Dette er for https://github.com/fable-compiler/ts2fable/issues/83

2017-11-17 fredagoppdatering, her er et eksempel på TypeScript. Dette gir ut:

heritage clause: extends Base
contextual type: undefined
import * as ts from "typescript";
import * as fs from "fs";

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 printBaseClass(cd: ts.ClassDeclaration){
    // console.log(cd)
    for(const hc of cd.heritageClauses) {
        console.log("heritage clause: " + hc.getText())
        for(const hctp of hc.types){
            // hctp is a ts.ExpressionWithTypeArguments

            const ct = checker.getContextualType(hctp.expression)
            console.log("contextual type: " + ct) // undefined
        }

    }
}

Svar #1

Forfatteren av tsutils-biblioteket viste meg hvordan jeg kunne bruke isTypeReference sammen med tp.target.symbol.declarations for å få klassedeklarasjonene jeg lette etter. Dette var i TypeScript-problemet.

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())
                        }
                    }
                }
            }
        }
    }
}