When attempting to utilize the Compiler API for processing JavaScript code and implementing Type inference to predict types of 'object' in a 'object.property' PropertyAccessExpression node, I encountered some issues. Some simple examples worked as expected, like the first sample below, but most failed. I am unsure if this is the intended way of using TypeScript type inference or if there might be an issue with the code I have written. Any insights would be greatly appreciated. Thank you!
var obj={prop: ''};
var h=obj;
h.prop = ''; //works! 'h' shows as 'obj' type
function fx(arg) {return arg;}
var i=fx(obj);
i.prop = ''; //failed! type of 'i' shows 'any', should be 'obj'
Below is the source code utilizing checker to print inferred type:
var ts = require('typescript');
function visit(node) {
ts.forEachChild(node, visit);
console.log( checker.getSymbolAtLocation(node.name));
}
var program = ts.createProgram([process.argv[2]], {lib: ['DOM'], allowJs: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.None});
var checker = program.getTypeChecker();
ts.forEachChild(program.getSourceFiles()[0], visit);