Solved: How to export a class from a package written in Typescript?

I’m trying to write a new package in Typescript and can’t figure out how to export a class for use in another package.

Here’s a part from my package.js:

Package.onUse(function (api) {
  api.versionsFrom("1.10");
  api.use(["typescript"], "server");
  api.addFiles(["meteor-typescript-compiler.ts"], "server");
  api.export(["MeteorTypescriptCompiler"], "server");
});

and the .ts file itself has:

export class MeteorTypescriptCompiler {
  constructor() {
    console.log("MeteorTypescriptCompiler constructor called");
  }
...
}

If I then try to use MeteorTypescriptCompiler in another package, it is undefined and the code will crash.

If I instead use javascript in the package, it works just fine:

Package.onUse(function (api) {
  api.versionsFrom("1.10");
  api.use(["typescript"], "server");
  api.addFiles(["meteor-typescript-compiler.js"], "server");
  api.export(["MeteorTypescriptCompiler"], "server");
});
MeteorTypescriptCompiler = class MeteorTypescriptCompiler {
  constructor() {
    console.log("MeteorTypescriptCompiler constructor called");
  }

I also tried using api.mainModule("meteor-typescript-compiler.ts", "server"); but that did not help.
I’m not sure the typescript even gets compiled for the package TBH but I did follow the instructions on https://atmospherejs.com/meteor/typescript

I found a cludge to make it work. I guess it makes sense since Meteor was designed so long ago.
The only way I found was to put the class into the global namespace.

In Typescript, this is deliberately hard since they would rather that you use globalThis or proper modules.

First add a file “global.d.ts”

import type { MeteorTypescriptCompilerImpl } from "./meteor-typescript-compiler";

declare global {
  var MeteorTypescriptCompiler: typeof MeteorTypescriptCompilerImpl;
}

Then use this in the implementation file:

export class MeteorTypescriptCompilerImpl {
...
}

MeteorTypescriptCompiler = MeteorTypescriptCompilerImpl;

The last statement installs the class into the global namespace and now it can be used by other packages.