2016-09-30 98 views
1

現在我真的試圖合併來自d.ts 實例命名空間:打字稿megring命名空間

當我試圖在一個文件中megre命名空間,一切都很好。

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; 

但是當我提出第一個命名空間來test.d.ts - 一切都是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'. 

我遇到了這個問題,當我添加新的我Thom to Electron,Angular2等類型。 例子:

電子/ index.d.ts

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

在我的文件test.ts

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

我得到這個錯誤:TS2339:房產 'isQuiting'在'App'類型上不存在。

我可以合併自定義名稱空間與d.ts

回答

0

我發現了兩個解決方案:

1)從* .TS所有輸入/輸出(You can read about this here

2)創建新的文件* .d.ts(例如test.extend.d.ts ), 寫所有cnanges成* .d.ts文件, 導入此文件:/// <reference path="*.d.ts" />

例子:

文件:個test.d.ts

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

文件:test.ts

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

我認爲這個問題是在根文件導入\導出:

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

所以第一個文件TST,並在不同範圍的第二個文件TST,不能合併。您必須從文件中刪除所有根導入\導出,或將其移至單獨的文件。

+0

你能告訴一個示例實現? – MixerOID

+0

例如文件** test.ts ** 'import {importClass} from「。/someFile.ts」 申報命名空間電子{ 接口應用擴展NodeJS.EventEmitter { isQuiting?:布爾; }} 出口類SomeClass的{ }' 不得包含出口\ import指令 – Vlad

+0

但我不能導入/導出!我導入電子和其他庫文件,並與他一起工作。這種變種爲「沒有文件 - 沒有問題」 – MixerOID