I can interact with the typescript compiler API to compile *.ts to *.js
import * as ts from "typescript";
const source = "function foo(xs: Array<number>) { let total=0; for (const x of xs) { total += x; } return total; }";
let result = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS }});
console.log(JSON.stringify(result, null, 4));
Is there any way I can get the inferred return type of foo?
A possible way of obtaining the return type is using the compiler API's type checker:
createProgramgetTypeCheckergetSignatureFromDeclarationgetReturnType(this will get a ts.Type object)There might be an easier way to obtain what you require, but this one worked for me.
for example, if your function is in fun.ts: