【发布时间】:2020-08-26 04:31:39
【问题描述】:
考虑一个外部 (npm) 模块 extmod 在其声明文件中公开以下接口:
interface Options {
somevar?: string;
suboptions?: {
somesubvar?: string;
};
}
如何通过模块扩充在suboptions 中添加属性somesubvar2?
我在 extmod.d.ts 文件中尝试了以下内容:
declare module 'extmod' {
interface Options {
suboptions?: {
somesubvar2?: string;
};
}
}
但它会引发以下错误:
error TS2687: All declarations of 'suboptions' must have identical modifiers.
error TS2717: Subsequent property declarations must have the same type. Property 'suboptions' must be of type '<SNIP>', but here has type '{ somesubvar2: string; }'.
【问题讨论】:
-
您可能想看看indexable types 或简单地使用多个接口。我认为你不能“嵌套”它们。
-
你不能像这样合并字段...见Module declaration with same name for property and function;这能回答你的问题吗?
-
也许我错了,have a look
-
您是否使用带有声明文件的第三方库?如果是这样,您可能需要在本地复制它,随意修改它,然后使用修改后的版本。至于“所述接口用于库本身定义的多个函数的参数中”......但
somesubvar2是suboptions的必需属性不会有问题?
标签: typescript module-augmentation