I want to introspect a function at runtime and generate a ReturnType string similar to the hover functionality in VS Code. I know VS Code is just running a typescript server in the background so theoretically this should be possible.
import * as ts from "typescript"
I've already been able to import typescript and crawl the AST to grab the exact Node for the function declaration (although I'd like this to work for both arrow and regular functions).
example
const foo = () => ({ bar: "baz" }); // function.ts
getReturnType("function.ts","foo") // returns "{bar:string}"
One of many notable caveats is that the function may call other functions declared in different files and ideally the type string should resolve everything to primitives, i.e.,
type complex = {
users: User[]
}
// what I want
type primitive = {
users: { name: string; role: string }[]
}
VS Code example
