首页 > 解决方案 > d3 和打字稿。selection.merge 类型问题

问题描述

我正在尝试使用打字稿实现 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.

我该如何解决?

标签: typescriptd3.jstypescript-typings

解决方案


我找到了解决方案。

长银

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

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

正在解决类型不匹配。


推荐阅读