Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 479
mixalbl4 Avatar asked Apr 10 '26 01:04

mixalbl4


2 Answers

I think the problem is root import\export in files:

If you have an import or an export at the root level of a TypeScript file then it creates a local scope within that file.

Read more here

So the first file tst, and the second file tst in different scopes and can't be merged. You must remove all root import\export from file, or move it to separated file.

like image 100
Vlad Avatar answered Apr 11 '26 19:04

Vlad


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!
like image 30
mixalbl4 Avatar answered Apr 11 '26 17:04

mixalbl4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!