Given
export default
before class gamma {}
in gamma.tsgamma
into the global namespace (i.e the window object) so it can be accessed by the existing global and module scripts. I have tried a bunch of things around global.d.ts
e.g.
import gammaAlias from "../scripts/gamma";
export as namespace NS_Global;
declare global {
var qqq: {n:string};
namespace ZZZ {
export var gamma: gammaAlias;
}
var gamma: gammaAlias;
}
But none has worked
i.e. TypeScript tells me one of the following
Cannot use 'new' with an expression whose type lacks a call or construct signature.
I have a github repo set up to investigate these questions. https://github.com/penguin020/expose-typescript-modules-globally
ConvertToModulesPOC_original is the working "before" case.
ConvertToModulesPOC is a broken attempt to convert gamma .ts to a module and expose it globally.
The question remains, how do you expose a module to the global namespace?
Any answers using the examples in the github would be particularly appreciated!
I found an answer here in "TypeScript: exposing module types in the global context and why to avoid it";
I have updated the github repo.
The most salient changes I found were:
in tsconfig:
"module": "system",
in yMod.ts
import gammaAlias from "../scripts/gamma.js";
declare global {
export type gamma = gammaAlias;
export const gamma: typeof gammaAlias;
}
(window as any).gamma = gammaAlias;
note: You could have the global ambient declaration in another file, even a *.d.ts file.
The link above breaks down the reasons for having three references for each symbol to be exposed globally. It is pretty well explained on that page, so I will only summarize here: