Currently, I'm grappling with the challenge of creating TypeScript code using the compiler API. Regrettably, official documentation on this subject is scarce, leaving me stranded on a seemingly straightforward task:
All I want to do is generate a basic function call like foo()
. Yes, just a simple invocation of a global function.
After much exploration, my progress has brought me here:
import ts from 'typescript'
ts.createCall(
ts.createPropertyAccess(
*some expression*,
'foo'
),
undefined, //generics
[], //parameters
);
From what I can gather, it seems necessary to provide an expression to createPropertyAccess
to act as the 'owner' of the accessed property (similar to foo
in foo.bar
). However, in this scenario, there is no explicit 'owner' since the function exists in the global scope.
Is there a viable approach to generating such a function call? I attempted utilizing ts.createOmittedExpression
, but the result was unexpected:
().bar();