How do you get inferred type from a typescript AST?

杀马特。学长 韩版系。学妹 提交于 2020-12-29 13:20:08

问题


I'm using the built-in parser to generate the AST from source code:

const ts = require('typescript')
//...
const ast = ts.createSourceFile(filename, fs.readFileSync(filename).toString(), ts.ScriptTarget.ES6, true)

Is there a way to get the inferred type of a variable from the AST? For example, in the code below, bar is of type IBar. The compiler knows about that type---bar.foo() doesn't compile---how can I programmatically get the type?

interface IBar { bar() } 
const foo : IBar = //...
export const bar = foo

回答1:


The compiler knows about that type---bar.foo() doesn't compile---how can I programmatically get the type?

The AST is not the complete story for TypeChecking. You need a TypeChecker.

The simplest solution is to create a program (some docs https://basarat.gitbooks.io/typescript/content/docs/compiler/program.html) and then use program.getTypeChecker (more docs https://basarat.gitbooks.io/typescript/content/docs/compiler/checker.html) 🌹




回答2:


As far as I can tell this does the trick:

// load typechecker for file
const program = ts.createProgram([filename], {});
const typeChecker = program.getTypeChecker()
// now get ast
const ast = ts.createSourceFile(filename, filecontents, ts.ScriptTarget.ES6, true))
// get node under question using ts.forEachChild (not shown here)
const node = // ...

const type = typeChecker.getTypeAtLocation(node);

The only weird part is that in a variable declaration, the "node" must be the value (aka FirstLiteralToken) and not the label (aka Identifier)---otherwise, the type is undefined. For example, if the file contents are const foo = 123, passing the node for "foo" to typeChecker#getTypeAtLocation won't work. You must pass the node for "123"



来源:https://stackoverflow.com/questions/34736109/how-do-you-get-inferred-type-from-a-typescript-ast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!