[英]How to extend and interface already set in typings?
在 D3 中,向已经存在的 D3 变量添加属性是很常见的。
一个例子(ES5):
force = d3.layout.force()
.nodes([{ links: 0, color: '#2BC9E9' }])
错误:
Argument of type '{ links: number; color: string; }[]' is not assignable to
parameter of type 'Node[]'.
如果我们可以创建一个新界面,我们会简单地做:
interface Node_ extends Node {
links: number;
color: string;
}
我们如何扩展已在 D3 类型中定义的接口Node
以允许新属性?
Typescript 允许将接口声明拆分为多个文件。 所以你可以在代码的任何地方添加波纹管定义
declare namespace d3 {
namespace layout {
namespace force {
interface Node {
links?: number;
color?: string;
}
}
}
}
这种方式不需要新的接口定义(Node_)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.