U A*_*los 2 abstract-syntax-tree typescript typescript1.6 typescript1.7
我正在使用内置的解析器从源代码生成AST:
const ts = require('typescript')
//...
const ast = ts.createSourceFile(filename, fs.readFileSync(filename).toString(), ts.ScriptTarget.ES6, true)
Run Code Online (Sandbox Code Playgroud)
有没有办法从AST中获取变量的推断类型?例如,在下面的代码中,bar类型为IBar。编译器知道该类型- bar.foo()不编译-我如何以编程方式获取该类型?
interface IBar { bar() }
const foo : IBar = //...
export const bar = foo
Run Code Online (Sandbox Code Playgroud)
编译器知道该类型--- bar.foo()无法编译---如何以编程方式获取类型?
这AST不是TypeChecking 的完整故事。您需要一个TypeChecker。
最简单的解决方案是创建一个程序(某些文档https://basarat.gitbooks.io/typescript/content/docs/compiler/program.html)然后使用program.getTypeChecker(更多文档https://basarat.gitbooks.io/typescript /content/docs/compiler/checker.html)
| 归档时间: |
|
| 查看次数: |
850 次 |
| 最近记录: |