问题描述
我有一个javascript库,该库在此处具有类型定义文件: https://github.com/tyranid-org/tyranid/blob/master/tyranid.d.ts ,该文件通过我的package.json
文件中的typings
属性公开.
I have a javascript library which has a type definition file here: https://github.com/tyranid-org/tyranid/blob/master/tyranid.d.ts which is exposed via the typings
property in my package.json
file.
定义文件的基本版本:
export default Tyr;
declare namespace Tyr {
interface Document {
$save(): Promise<Document>;
}
}
我想在一个完全独立的打字稿库中扩展Document
接口: https://github .com/CrossLead/tyranid-gracl 通过将$newMethod()
方法添加到文档界面来导入tyranid
及其类型.能做到吗?
I would like to extend the Document
interface in a completely separate typescript library: https://github.com/CrossLead/tyranid-gracl which imports tyranid
along with its typings, by adding a $newMethod()
method to the document interface. Can this be done?
我在第二个仓库中的一个单独的声明文件中尝试了以下操作,但是它不起作用:
I've tried the following in a separate declaration file in the second repo, but it doesn't work:
import Tyr from 'tyranid';
declare namespace Tyr {
interface Document {
$newMethod(): number;
}
}
如有必要,我可以修改两个声明-很有可能我没有正确编写tyranid的原始类型定义文件.谢谢!
I can modify both declarations if necessary -- its very possible I am not writing the original type definition file for tyranid correctly. Thanks!
推荐答案
以下内容最终可以正常工作...
The following ended up working...
在node_modules/tyranid/tyranid.d.ts
中:
export = Tyr // must do "import * as Tyr from 'tyranid'" instead of default
declare namespace Tyr {
export interface Document {
// original definition...
}
}
在mylib/tyranid-extensions.d.ts
...
declare module "tyranid" {
interface Document {
addedMethod(): boolean;
}
}
感谢@ pelle-jacobs的帮助!
Thanks @pelle-jacobs for the help!
这篇关于合并模块声明文件中命名空间的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!