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

Check Typescript guide for React Flow 12 #407

Open
moklick opened this issue Jun 16, 2024 · 1 comment
Open

Check Typescript guide for React Flow 12 #407

moklick opened this issue Jun 16, 2024 · 1 comment
Assignees
Labels
react-docs Issues belonging to the reactflow website

Comments

@moklick
Copy link
Member

moklick commented Jun 16, 2024

There are a lot of changes regarding types for nodes and edges in React Flow 12. We have a new guide that explains everything but we should double check if it's correct and if it all works. It would be nice if we could add a section about how to type useStore correctly on user side.

@moklick moklick added the react-docs Issues belonging to the reactflow website label Jun 16, 2024
@buchananwill
Copy link
Contributor

buchananwill commented Jul 24, 2024

I've just done a migration from v11 to v12, and I had some head-scratching with the Types for NodeProps.

v11

Before the update, I might have a node component defined like this:


export function BaseEditableNode<T extends HasNumberId>({
  data,
  isConnectable,
  xPos,
  yPos,
  type,
  children,
  className,
  style
}: NodeProps<T> & Pick<GenericDivProps, 'children' | 'className' | 'style'>) {
  

Which gave me correct typing of the data prop when I then wrapped this component in a more specific use case. However, this did not translate at all to v12.

v12

The path of least refactoring ended up being this:


export function BaseEditableNode<
  NodeData extends NodeDataType,
  NodeType extends string,
  BaseNodeType extends NodeBase<NodeData, NodeType>
>({
  data,
  isConnectable,
  positionAbsoluteX,
  positionAbsoluteY,
  type,
  children,
  className,
  style
}: NodeProps<BaseNodeType> &
  Pick<GenericDivProps, 'children' | 'className' | 'style'>) {

Where I had to cut&paste the source code for NodeBase from the xyflow github, and add it to my own codebase. Fortunately, Typescript falls back to duck typing, and this is a working solution that compiler/linter are happy with. However, I would naturally prefer not to have to expose aspects of a libraries internals that aren't intentionally part of its API.

  1. Is there a simpler solution I could have applied?
  2. If not, could the NodeBase be added to the exposed API?
  3. Or if not, could the Type definitions be revised to better infer generic data types?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react-docs Issues belonging to the reactflow website
Projects
None yet
Development

No branches or pull requests

3 participants