Is it possible to infer function parameter types using Compiler API?

146 views Asked by At

I'm using the Compiler API to search for structures like promises, generators, and callbacks written in JavaScript. I've decided to use TypeScript instead of other parses because of its type-checking features. After reading the TS documentation, it seems to me that the only way to infer types from parameters that are functions is when the code has JSDOC (in this context I'm only aware of non-native functions), right? What would you suggest in my case? Is there any other option? Should I search for these structures considering only syntax kinds (e.g. looking for CallExpressions node children names with the same parameter names)?

This is a small example from what I want to detect:

const run = (fn, ...params) => (message) => `${message}: ${fn(...params)}`;

In this case above, I want to detect whether "fn" is a function or not. When I call for typeChecker.typeToString(typeChecker.getTypeAtLocation(node)) the result is always "any".

0

There are 0 answers