I am looking for a callback function that can be utilized with both rect
and circle
elements.
Here is an example:
function commonTasks(selection:d3.Selection<PLACEHOLDER_TYPE, MyDataType, SVGGElement, unknown>) {
selection
.classed('my-class1', true)
.classed('my-class2', true)
}
This function should be callable on both rect
and circle
elements like this:
nodes.append('rect').call(commonTasks)
nodes.append('circle').call(commonTasks)
The documentation for d3.Selection states that
The first generic "GElement" refers to the type of the selected element(s).
However, the challenge I'm facing is determining what to set as that type (referred to as PLACEHOLDER_TYPE
above) in order to encompass both SVGRectElement
and SVGCircleElement
.
If I set it to just SVGRectElement
, I encounter the error
Type 'SVGCircleElement' is not assignable to type 'SVGRectElement'
and vice versa.
Setting it to SVGRectElement|SVGCircleElement
results in the same error.
Even setting it to their common parent, SVGGraphicsElement
, leads to the same issue.
I have a suspicion that I should be passing a generic parameter somehow, but my knowledge of TypeScript is insufficient to resolve this.