【问题标题】:d3 and Typescript. selection.merge types problemd3 和打字稿。 selection.merge 类型问题
【发布时间】:2020-08-27 09:21:49
【问题描述】:

我正在尝试使用 typescript 实现 d3 输入、更新、删除模式

来源: https://codesandbox.io/s/brave-banach-g1h51?file=/src/index.ts

import { select } from "d3-selection";

const chart = select("#chart");

const values = [1, 2, 3, 4, 5];

const node = chart.selectAll("g.node").data(values);
const nodeEnter = node
  .enter()
  .append("g")
  .attr("class", "node");

// Type Error here
const nodeUpdate = nodeEnter.merge(node);

node.exit().remove();

但我在这个问题上遇到了错误:

Argument of type 
'Selection<BaseType, number, BaseType, unknown>' is not assignable to parameter of type 
'Selection<SVGGElement, number, BaseType, unknown>'.
Types of property 'select' are incompatible.

我该如何解决?

【问题讨论】:

    标签: typescript d3.js typescript-typings


    【解决方案1】:

    我找到了解决办法。

    变化

    const node = chart.selectAll("g.node").data(values);
    

    const node = chart.selectAll<SVGGElement, number[]>("g.node").data(values);
    

    正在解决类型不匹配问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2018-10-11
      • 2020-07-07
      • 1970-01-01
      • 2021-02-07
      • 2020-01-17
      • 2021-03-04
      相关资源
      最近更新 更多