typescript - 如何使用打字稿在 d3 svg 上正确键入缩放?
问题描述
我有这个代码:
if (svg) {
// zoom
// svg type = d3.Selection<SVGSVGElement, unknown, null, undefined>
const zoomBehavior = d3
.zoom()
.scaleExtent([0.5, 5])
.translateExtent([
[0, 0],
[containerWidth, containerHeight],
])
.on("zoom", (event) => {
const zoomState = event.transform;
console.log("zoomState", zoomState);
});
svg.call(zoomBehavior);
}
我收到此错误类型:
Argument of type 'ZoomBehavior<Element, unknown>' is not assignable to parameter of type '(selection: Selection<SVGSVGElement, unknown, null, undefined>, ...args: any[]) => void'.
我不想使用 ts-ignore
解决方案
您可以在函数的泛型中定义缩放行为的类型,以匹配调用它的 SVG 选择的类型。
由于您的 SVG 选择键入为d3.Selection<SVGSVGElement, unknown, null, undefined>
,因此:
const zoomBehavior = d3
.zoom<SVGSVGElement, unknown>()
...
推荐阅读
- java - 如何在 Mockito 中的间谍对象条件下调用真实方法?
- git - git worktree 和重置保管箱共享文件夹
- python - 如何创建委托类而不是子类?
- css - 颤振等效关闭CSS背景过滤器:模糊(20px)饱和度(180%)
- opencv - 制作raspicam时,找不到OpenCV(本地安装)[RaspberryPi]
- fuchsia - Fuchsia OS fx 设置在构建期间失败
- php - PHP中的Json对象排序
- php - PHP 未收到改造后的数据
- python - 将两列合并为上下填充的单列
- azure - 我们是否需要在 azure 云中安装 iis 服务器才能在 azure 中托管 Web 应用程序