What's the difference between declaring a variable and a namespace in a TypeScript declaration file

22 views Asked by At

I'm creating TypeScript declarations for a module. The module has a default export with 2 functions: foo and bar and is used as follows:

import myModule from 'my-module';

myModule.foo();
myModule.bar();

I have created a .d.ts file, inside which I can add types to the module by declaring a variable:

declare var myModule: {
    foo(): void;
    bar(): void;
};

export default myModule;

or by declaring a namespace:

declare namespace MyModule {
    function foo(): void;
    function bar(): void;
}

export default MyModule;

What is the difference between these options? Is one of them preferred, or should I be using something else entirely?

0

There are 0 answers