TypeScript编译器API获取导入名称的类型

Wic*_*koo 3 types typescript typescript-compiler-api

考虑以下import声明。

import { a } from "moduleName"
Run Code Online (Sandbox Code Playgroud)

如何获得的声明类型a。例如,在类或函数的情况下,ClassDeclaration或者,FunctionDeclration或者Namespace还有类型的名称?

在上面的示例中, ais是ImportSpecifier,但是当我尝试使用解析它时typeChecker.getSymbolAtLocationtypeChecker.getTypeAtLocation我只得到Identifiertype any

Tit*_*mir 5

您可以使用getTypeAtLocation获取导入类型的类型,然后使用type.getSymbol().getDeclarations()获取符号和声明。

给定以下文件:

// module.ts
export class A {
}

export function F(){    
}

// sample.ts
import { A, F} from './module';
Run Code Online (Sandbox Code Playgroud)

此代码将输出导入的声明和完整类型名称:

import * as ts from "typescript";

function compile(fileNames: string[], options: ts.CompilerOptions): void {
    let program = ts.createProgram(fileNames, options);
    let sample = program.getSourceFile("sample.ts");
    let checker = program.getTypeChecker();

    let list = sample.getChildAt(0) as ts.SyntaxList;
    let importStm = list.getChildAt(0);
    if (ts.isImportDeclaration(importStm)) { // get the declaration 
        let clauses = importStm.importClause;
        let namedImport = clauses.getChildAt(0); // get the named imports
        if (!ts.isNamedImports(namedImport)) return;
        for (let i = 0, n = namedImport.elements.length; i < n; i++) { // Iterate the named imports
            let imp = namedImport.elements[i];
            console.log(`Import: ${imp.getText()}`);
            // Get Type 
            let type = checker.getTypeAtLocation(imp);
            // Full name 
            console.log(`Name: ${checker.getFullyQualifiedName(type.getSymbol())}`);
            // Get the declarations (can be multiple), and print them 
            console.log(`Declarations: `)
            type.getSymbol().getDeclarations().forEach(d => console.log(d.getText()));
        }

    }
}

compile(["module.ts", "sample.ts"], {}); 
Run Code Online (Sandbox Code Playgroud)

对于接口,有一个复杂的问题,返回的类型是未知的,这是有意完成的,因为编译器中的此注释告诉我们,并且还建议解决方法:

// It only makes sense to get the type of a value symbol. If the result of resolving
// the alias is not a value, then it has no type. To get the type associated with a
// type symbol, call getDeclaredTypeOfSymbol.
Run Code Online (Sandbox Code Playgroud)

因此,对于接口,我们需要使用:

let symbol = checker.getSymbolAtLocation(imp.name)
// Get Type 
let type = checker.getDeclaredTypeOfSymbol(symbol);
Run Code Online (Sandbox Code Playgroud)