polkovnikov.ph
polkovnikov.ph

Reputation: 6632

Merging declarations of namespace and variable

I'm trying to create declaration file for a third-party library node-tap. The simplified problem is: for a library

// node_modules/a/index.js
function A() { /* ... */ }
module.exports = new A();
module.exports.A = A;

what would be a correct *.d.ts declaration file to allow the following code to compile successfully?

// test.ts
import * as a from 'a';
import {A} from 'a';
function f(): A {
    return a;
}

Mention that having A as type is important, even though it could be omitted in this simple example.

Upvotes: 1

Views: 80

Answers (1)

polkovnikov.ph
polkovnikov.ph

Reputation: 6632

If you need to be able to call new a.A(), do:

declare class A {
    A: typeof A;
}
declare const a: A;
declare namespace a {
    export type A = A;
}
export = a;

If you only need the type a.A accessible, do:

declare const a: a.A;
declare namespace a {
    interface A {}
}
export = a

Upvotes: 2

Related Questions