typescript - 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.
我该如何解决?
解决方案
我找到了解决方案。
长银
const node = chart.selectAll("g.node").data(values);
至
const node = chart.selectAll<SVGGElement, number[]>("g.node").data(values);
正在解决类型不匹配。
推荐阅读
- graphql - 减少 graphql 模式中的嵌套深度
- powershell - 变量未在 PowerShell 脚本中传递
- php - 如何在 PHP 中为重定向的索引页面提供参数
- r - 使用 R 形成组的最有效方式
- c++ - 使用 C 接口将 MinGW 库链接到 MSVC 应用程序
- android - 查找哪些地方正在使用未在 strings.xml 中的字符串
- php - Woocommerce 订阅:获取有效订阅中的所有产品并更新运输元数据
- javascript - 如何在初始化我的 Nuxt 应用程序之前获取和存储窗口宽度?
- lisp - 从中心点创建一个具有现有高度和宽度大小的矩形
- google-chrome - Chrome - 如何打开特定 URL 并自动打开 devtools