I have a collection of widgets created using d3 (version 5) and now I need to transition them to version 7. After consulting the migration guide at , I discovered the updated syntax for events:
// previously
element.on('click', (d, i, e) => ...
// now
element.on('click', (event, d) => ...
Upon switching to the new syntax, the program started running smoothly again. Great news!
The current challenge is that I am utilizing TypeScript in this project! Despite having installed the most recent @types/...
"dependencies": {
"d3": "^7.1.1",
...
"devDependencies": {
"@types/d3": "^7.1.0",
...
...I continue to encounter difficulties with type definitions and Intellisense particularly concerning events.
// 1. previous (Functioned correctly)
element.on('click', (d: MyData, i: number, e: whatever[]) => ...
// 2. adding types == Error reappears! ❌
element.on('click', (event: PointerEvent, d: MyData) => ...
// 3. thus, my temporary solution:
element.on('click', (event: any , d: any) => ...
Interestingly, even though I have @types/d3 v7.0.1 installed, it seems like the Typings system still refers to the old v5 format. Somehow Intellisense continues to think the first parameter is data and the second is a numeric index:
https://i.sstatic.net/XelTD.png
https://i.sstatic.net/pYw7b.png
...In reality, what I receive is { event: PointerEvent, d: MyData }, as displayed in the image below.
https://i.sstatic.net/8u7HF.png
Therefore, it appears there may be an issue with D3's type declaration since Intellisense believes the Event is still in the v5 format while that is not the case in practice.
Does anyone have a solution to rectify this typing problem? It can be quite frustrating to resort to assigning "any" within my events.