5

I am trying to write TypeScript declarations for some existing AMD modules written in JavaScript.

The structure I am trying to represent is a "plugin" model where one module declares an interface and another module extends that interface with "plugins". I am trying to follow the example for module augmentation here, but that doesn't seem to allow me to create two separate modules.

Example:

  • Module "foo" defines the Foo interface and exports a singleton constant of that interface
  • Module "foo-plugin" adds a new "plugin" function to the Foo interface

From the consumer side, I want the behavior to be as follows:

// by importing this, I can use the foo singleton as type Foo with only the Foo
// functionality defined in "foo"
import { foo } from "foo";
// by also importing this, I can now use the additional Foo.plugin function defined by "foo-plugin" on the foo singleton
import "foo-plugin";

// this should compile if and only if the "foo-plugin" module is imported
foo.plugin();

Is this behavior possible? What would the declarations look like?

ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152

1 Answers1

0

I had the same problem with fastify plugin declaration.

declare module "foo-plugin" {

    module "foo" {
        function plugin(): void;
    }
}
Szopinski
  • 790
  • 1
  • 10
  • 18