现在我正在尝试从d.ts合并名称空间 例如:
当我在一个文件中尝试使用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'.
当我向Electron,Angular2等类型添加新方法时,我遇到了这个问题。 例如:
在 electron / index.d.ts
中declare namespace Electron {
interface App extends NodeJS.EventEmitter {
...
}
}
在我的文件 test.ts
中declare namespace Electron {
interface App extends NodeJS.EventEmitter {
isQuiting?: boolean;
}
}
我收到了这个错误:TS2339:Property&#39; isQuiting&#39;类型&#39; App&#39;。
上不存在我可以将自定义命名空间与 d.ts ?
合并答案 0 :(得分:0)
我认为问题是文件中的root import \ export:
如果您在TypeScript的根级别进行导入或导出 然后它在该文件中创建一个本地范围。
所以第一个文件 tst ,第二个文件 tst 在不同的范围内,无法合并。您必须从文件中删除所有root import \ export,或将其移动到分隔文件。
答案 1 :(得分: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!