How do you expose class in a module to the global namespace in TypeScript?

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:

  1. Expose the class type to the TypeScript compiler (no code generated)
  2. Expose the class object to the TypeScript compiler (so there is an object to call new on; no code generated)
  3. Extend the window object to expose the class in the global namespace (generates code).