首页 > 解决方案 > Typescript 类型转换 D3.js DefaultArcObject

问题描述

我目前正在使用从此加载的类型定义。我的代码正在尝试使用D3.js 生成的元素设置dsvg 属性。patharc

我的代码如下所示:

const pie = d3.pie<SomeDataInterface>()
        .value((d) => d.dataVal);

const arc = g.selectAll(".arc")
        .data(pie(mockData));

const path = d3.arc();

arc.append("path")
        .attr("d", path); // >> Where compilation fails

但是,我收到此编译错误:

TypeScript error: Argument of type 'Arc<any, DefaultArcObject>' is not assignable to parameter of type 'ValueFn<SVGPathElement, PieArcDatum<ID3Data>, string | number | boolean | null>'.
  Types of parameters 'd' and 'datum' are incompatible.

这里的例子,似乎我可以在声明中声明一个<any>类型path。同样,我也尝试导入包含实际类型定义的Arcusing的类型定义并像这样分配它:import { Arc } from "d3-shape";

.attr("d", <Arc>path)

其中还收到编译警告。知道我可能缺少什么类型转换,具体在哪里?


更新

这段代码正在处理此处显示的示例。

标签: javascripttypescriptd3.js

解决方案


推荐阅读