Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics for nodes and links in typescript #359

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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