参考:https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API,我想在AST中获得类型的节点。
它对于angular项目是成功的,因为它是使用类型记录编写的,并且存在tsconfig.json。
当我试图分析使用javascript编写的react app时
const program = ts.createProgram({
rootNames: [fileName],
options: {
strict: true,
target: ts.ScriptTarget.ES2015,
allowJs: true,
checkJs: true
}
})
const typeChecker = program.getTypeChecker();
... ...我明白了:typechecker.getTypeAtLocation(node).getSymbol()是undefined。
我认为options of ts.createProgram是错的。
发布于 2019-05-16 06:04:28
选项:moduleResolution: ts.ModuleResolutionKind.NodeJs是关键。
const program = ts.createProgram({
rootNames: [file1],
options: {
strict: true,
target: ts.ScriptTarget.ES2015,
allowJs: true,
checkJs: true,
moduleResolution: ts.ModuleResolutionKind.NodeJs
}
})上面的工作是反应JS。
https://stackoverflow.com/questions/56099880
复制相似问题