vasturiano / force-graph

Force-directed graph rendered on HTML5 canvas
https://vasturiano.github.io/force-graph/example/directional-links-particles/
MIT License
1.54k stars 246 forks source link

Types do not correspond with API for d3Force? #243

Open magnusart opened 2 years ago

magnusart commented 2 years ago

I've seen in issues that I should be able to use the following code myGraph.d3Force('link').distance(link => /* your logic */).

However when using typescript the type signature ForceFn does not contain distance. Also the d3-force-3d project is not typed as far as I could see, so I can't use that as a base without adding my own types.

The return type is ForceFn | undefined which is an interface that looks like this:

interface ForceFn {
  (alpha: number): void;
  initialize?: (nodes: NodeObject[]) => void;
  [key: string]: any;
}

What is the recommended way do this in Typescript?

magnusart commented 2 years ago

I managed to hack around it with

  const l = instance.d3Force('link')! as ForceFn2 & { distance: (link: LinkObject) => number }
  l.distance(() => 50)

But that is very ugly. I had to redefine ForceFn as that was not exported. Any input welcome!