打字稿:使用模块扩充功能在内部对象上添加属性

时间:2020-05-10 17:28:48

标签: typescript module-augmentation

考虑一个外部(npm)模块extmod,在其声明文件中公开以下接口:

interface Options {
  somevar?: string;
  suboptions?: {
    somesubvar?: string;
  };
}

如何通过模块扩展在somesubvar2内添加属性suboptions

我在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; }'.

1 个答案:

答案 0 :(得分:-1)

如何在模块的子选项内添加属性somesubvar2 增强?

在您的示例中,您将类型suboptions<SNIP>更改为{ somesubvar2: string; }

您可以使用其他属性来修补<SNIP>类型。