Skip to content

Conversation

@fxhm
Copy link

@fxhm fxhm commented Aug 14, 2024

This adds a generic version of ForceGraph and ForceGraphInstance:
type TypedForceGraphInstance<N extends NodeObject, L extends TypedLinkObject<N>>
and
function TypedForceGraph<N extends NodeObject, L extends TypedLinkObject<N>>(): TypedForceGraphInstance<N, L>

The types ForceGraph and ForceGraphInstance are kept for backwards compatibility.

Reason

One might want to access custom properties of your own node and link objects in the graphs accessor functions.
Typescript won't let you do that without errors/warnings if there the type is fixed to NodeObject / LinkObject.

Example usage

import {TypedLinkObject, NodeObject, TypedForceGraph} from 'force-graph'

class NamedNode implements NodeObject {
    firstName: string,
    lastName: string,
}

const graph = TypedForceGraph<NamedNode, TypedLinkObject<NamedNode>>()

graph.nodeLabel((node: NamedNode): string => {
    return node.firstName + ' ' + node.lastName
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant