Create .d.ts for both a module.exports = function and some interfaces

Viewed 70

I have a javascript code similar to this:

module.exports = function foo(language, platform, milliseconds) { 
   // some code
}

This code should be placed in a module ("mymodule") that will be imported through npm.

I want to create the corrisponding .d.ts file.

I have something like this:

declare module 'mymodule' {
     export type Language = 'it' | 'es' | 'de';
     export type Platform = 'linux' | 'winzoz';
     export function foo(language: Language, platform: Platform, milliseconds?: number): void;
}

But of course it did not work. I tried:

  1. declare module 'mymodule' { export default function foo(myparams: Types): void; }
    

    But this did not work, because I am using module.exports = function f() and not a default export

  2. declare module 'mymodule' { function foo(myparams: Types): void; export = foo; }
    

    But this did not work, because it says that I should use esModuleInterop or use an ugly import foo = require('mymodule'). It does not include the interfaces that I want to export, too.

How can I solve these problems?

0 Answers
Related