Why can't an export and a module declaration exist in the same TypeScript declaration file?

177 Views Asked by At

I'm working on a small Jest library, and with it I would like to both extend Jest's matchers, as well as provide some utility functions.

Take this code that consumes the library as an example:

import { loadPlugin } from "jest-helpers/utils";

it("loads client modules", async () => {
  const plugin = await loadPlugin();
  expect(plugin).toLoadClientModules();
});

It uses a utility function (loadPlugin) from the package, as well as a custom Jest matcher (toLoadClientModules).

To achieve the types on this I've augmented Jest's Matchers interface (according to this post), as well as defined a custom sub-module for my utility, which looks like this:

// types.d.ts

// Extend Jest's matchers
declare global {
  namespace jest {
    interface Matchers<R> {
      toLoadClientModules(...expected: string[]): R;
    }
  }
}

export {};

// Declare sub-module for test utilities
declare module "jest-helpers/utils" {
  export const loadPlugin: () => any;
}

On their own these two work, but as soon as the export {} and declare module lines exist in the same declaration file, only the matchers are recognized by TypeScript, and the module declaration is not recognized.

Can someone help me understand why this is the case, and how these two things could co-exist in a package?

0

There are 0 best solutions below