MixerOID MixerOID - 1 month ago 15
TypeScript Question

Typescript megring namespaces in d.ts files

Now I`m trying merge namespaces from d.ts
Example:

When I tried megre namespaces in one file, all is well.

declare namespace tst {
export interface info {
info1: number;
}
var a: info;
}

declare namespace tst {
export interface info {
info2: number;
}
}

tst.a.info1 = 1;
tst.a.info2 = 1;


But when I moved first namespace to test.d.ts - all is brokes

test.d.ts

declare namespace tst {
export interface info {
info1: number;
}
var a: info;
}


index.ts

/// <reference path="test.d.ts" />

declare namespace tst {
export interface info {
info2: number;
}
}

// Module to control application life.
tst.a.info1 = 1;
tst.a.info2 = 1; // Error:(31, 7) TS2339: Property 'info2' does not exist on type 'info'.


I met this problem when I added new method to Electron, Angular2 and etc types.
Example:

in electron/index.d.ts

declare namespace Electron {
interface App extends NodeJS.EventEmitter {
...
}
}


in my file test.ts

declare namespace Electron {
interface App extends NodeJS.EventEmitter {
isQuiting?: boolean;
}
}


I got this error: TS2339: Property 'isQuiting' does not exist on type 'App'.

Do I can merge custom namespaces with d.ts?

Answer

I found two solutions:

1) Remove all import/export from *.ts (You can read about this here)

2) Create new file *.d.ts (example test.extend.d.ts), write all cnanges into *.d.ts file, import this file: /// <reference path="*.d.ts" />

Example:

File: test.d.ts

declare namespace Electron {
    interface App extends NodeJS.EventEmitter {
        isQuiting?: boolean;
    }
}

File: test.ts

/// <reference path="test.d.ts" />
import {...} from "..."; // Any import/export
app.isQuiting = false; // IT WORKS!!!
app.quit(); // types of electron work too!