D3.js: 'this' 隐式具有类型 'any' 因为它没有类型注释
D3.js: 'this' implicitly has type 'any' because it does not have a type annotation
我使用 3D.js 库:
d3.select(`#myGraph`)
.append('svg')
const svg = d3.select(`#myGraph svg`).append('g')
const nodeEnter = svg.selectAll("g.node")
.data(nodes)
.enter()
.append("g")
.attr("class", "node")
.call(force.drag())
svg.selectAll(".node")
.each(function (d: any) {
d3.select(this)
.attr("data-source", d.id)
})
但是我得到了错误:
'this' 隐式具有类型 'any' 因为它没有类型注释
没有@ts-ignore 我该如何修复它?
可以在函数中的this
中添加类型注解:
svg.selectAll(".node")
.each(function (this: any, d: any) {
d3.select(this)
.attr("data-source", d.id)
})
或者,如果您不想要明确的 any
,您可以将其定义为 SVGGraphicsElement
,这是一种通用的 SVG 元素类型。如果你想要更详细,它可以是更具体的类型(例如 SVGRectElement
或 SVGGElement
)以更好地描述选择。
svg.selectAll(".node")
.each(function (this: SVGGraphicsElement, d: any) {
d3.select(this)
.attr("data-source", d.id)
})
我使用 3D.js 库:
d3.select(`#myGraph`)
.append('svg')
const svg = d3.select(`#myGraph svg`).append('g')
const nodeEnter = svg.selectAll("g.node")
.data(nodes)
.enter()
.append("g")
.attr("class", "node")
.call(force.drag())
svg.selectAll(".node")
.each(function (d: any) {
d3.select(this)
.attr("data-source", d.id)
})
但是我得到了错误: 'this' 隐式具有类型 'any' 因为它没有类型注释
没有@ts-ignore 我该如何修复它?
可以在函数中的this
中添加类型注解:
svg.selectAll(".node")
.each(function (this: any, d: any) {
d3.select(this)
.attr("data-source", d.id)
})
或者,如果您不想要明确的 any
,您可以将其定义为 SVGGraphicsElement
,这是一种通用的 SVG 元素类型。如果你想要更详细,它可以是更具体的类型(例如 SVGRectElement
或 SVGGElement
)以更好地描述选择。
svg.selectAll(".node")
.each(function (this: SVGGraphicsElement, d: any) {
d3.select(this)
.attr("data-source", d.id)
})