Creating a universal function in a file that is not a module:
example.ts:
function example() {}
You can easily call this function from another file, say test.ts, without needing to import the function from example.ts:
test.ts:
example(); // calling universal function example
However, as soon as you introduce another module into example.ts, it becomes a module itself and the global definition vanishes. This leads to an error in test.ts when trying to execute example.
Is there a way to define a universal function within a module that applies across the entire project without requiring imports?
UPDATE:
I am aware of the declare global
syntax, but this method necessitates duplicating each function signature: once for global declaration and again for binding to globalThis
. I am seeking a more streamlined solution with less redundancy.