2017-06-07 13 views
0

Ich versuche, ein Code-Analyse-Tool nach dem Compiler API zu bauen.Wie erkennt man den Typ des Symbols im TypeScript-Compiler?

Im Augenblick ist die App unten kann aus p, drucken Person, age, walk.

Aber wie zu wissen Person ist Schnittstelle, walk ist Funktion, etc.?

Dank

// app.ts

import * as ts from 'typescript'; 

const userAppFile = './user-app.ts'; 
const apiFile = './api.d.ts'; 

const program = ts.createProgram([userAppFile, apiFile], ts.getDefaultCompilerOptions()); 
const checker = program.getTypeChecker(); 
const sourceFile = program.getSourceFile(userAppFile); 

function printApi(node) { 
    const symbol = checker.getSymbolAtLocation(node); 

    if (symbol) console.log(symbol.name); 

    ts.forEachChild(node, printApi); 
} 

printApi(sourceFile); 

// api.d.ts

interface Person { 
    age: number; 
} 

declare function walk(speed: number): void; 

// user-app.ts

const p: Person = { age: 10 }; 
walk(3); 

Antwort

1

Sie überprüfen th Die Flaggen auf dem Symbol.

d.h .:

if(symbol.flags & ts.SymbolFlags.Class) { 
    // this symbol is a class 
} else if (symbol.flags & ts.SymbolFlags.Interface) { 
    // this is an interface 
} 
+0

Danke, funktioniert perfekt! –

Verwandte Themen