简体   繁体   English

如何扩展和接口已经在打字中设置?

[英]How to extend and interface already set in typings?

In D3 it's common to add attributes to already existing D3 variables.在 D3 中,向已经存在的 D3 变量添加属性是很常见的。

An example (ES5):一个例子(ES5):

force = d3.layout.force()
  .nodes([{ links: 0, color: '#2BC9E9' }])

The error:错误:

Argument of type '{ links: number; color: string; }[]' is not assignable to
parameter of type 'Node[]'.

If we could create a new interface we would simple do:如果我们可以创建一个新界面,我们会简单地做:

interface Node_ extends Node {
    links: number;
    color: string;
}

How do we extend the interface Node already defined in D3's typings to allow for the new attributes ?我们如何扩展已在 D3 类型中定义的接口Node以允许新属性?

Typescript allows split interface declaration into multiple files. Typescript 允许将接口声明拆分为多个文件。 So you can just add bellow definition anywhere in your code所以你可以在代码的任何地方添加波纹管定义

declare namespace d3 {
    namespace layout {
        namespace force {
            interface Node {
                links?: number;
                color?: string;
            }
        }
    }
}

This way new interface definition (Node_) is not needed这种方式不需要新的接口定义(Node_)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM