Typescript error “An export assignment cannot be used in a module with other exported elements.” while extending typescript definitions

后端 未结 2 2109
陌清茗
陌清茗 2021-01-11 19:16

I have downloaded the type definition of a module, let\'s say ModuleA, from @types/module-a.

The module-a.d.ts file lo

2条回答
  •  抹茶落季
    2021-01-11 20:04

    I found that I could combine export = syntax with namespace to export types from the interface. export = is necessary (as far as I understand) to indicate that an external module uses CommonJS-style exports rather than ES6 exports. If you try to both use export = and export in the same module, you will receive the following error:

    TS2309: An export assignment cannot be used in a module with other exported elements.

    However, if you declare a declare a namespace with the same name as the variable used in the exports = expression, you can place types inside of that namespace, making them accessible to consuming modules.

    Here is an example of a module type definition using this technique:

    declare module 'some-module' {
      namespace SomeClass {
        export interface IMyInterface {
          x:string;
        };
      }
      class SomeClass {
        constructor(p:SomeClass.IMyInterface);
      }
      export = SomeClass;
    }
    

提交回复
热议问题