Create Typescript TypeChecker without using ts.createProgram?

I am doing some reflection using typescript library. First of all, I use ts.createSourceFile and transferring files stored in memory. This works very well, and I can easily navigate the nodes to match the binding syntax (in html files) using ViewModels (typescript) to make sure the access is good.

My problem is that now I want to do some basic type checking with the resulting ts.SourceFile objects that I have. So far, I have managed to track the TypeChecker interface back to the Program class. Unfortunately, ts.createProgram accepts file paths while I want to transfer the SourceFile collection that I already have.

Is there a way to check the type with the ts.SourceFile objects that I already have? All I really need to know is that the right side is assigned on the left, where I have type nodes for both.

+5
source share
1 answer

You can use ts.createSourceFile() as follows:

 let sourceCode = fs.readFileSync(commander.file); let sourceFile = ts.createSourceFile(commander.file, sourceCode.toString(), ts.ScriptTarget.ES2015, true); visitNode(sourceFile); 

And then in the visitNode function visitNode you can access the NodeObject node, which displays TypeChecker info:

 function visitNode(node: NodeObect) { recognize(node); node.getChildren().forEach(c => visitNode(c)); } 
-1
source

Source: https://habr.com/ru/post/1258295/


All Articles