Xen*_*yal 6 javascript d3.js typescript
我当前正在使用从此包加载的类型定义。我的代码尝试使用D3.js 生成的元素设置d
svg 属性。path
arc
我的代码如下所示:
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
Run Code Online (Sandbox Code Playgroud)
但是,我收到此编译错误:
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.
Run Code Online (Sandbox Code Playgroud)
看看这里的例子,似乎我可以在声明<any>
上声明一个类型path
。同样,我也尝试导入Arc
using import { Arc } from "d3-shape";
of 的类型定义,其中包含实际的类型定义并像这样分配它:
.attr("d", <Arc>path)
Run Code Online (Sandbox Code Playgroud)
其中还会收到编译警告。知道我可能缺少什么类型,具体在哪里?
这段代码正在处理此处所示的示例。
虽然这个问题已经问了三年了,但是为了帮助后来的人,我还是想在这里分享一下解决方案。
为了解决这个问题,我们需要修改 的泛型参数d3.arc
。假设我们的数据类型是SomeDataInterface
,泛型参数应该这样写:
d3.arc<d3.PieArcDatum<SomeDataInterface>>()
Run Code Online (Sandbox Code Playgroud)
关键是使用d3.PieArcDatum<>
来包装我们的数据类型。
归档时间: |
|
查看次数: |
1042 次 |
最近记录: |