相关文章推荐

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 data.label is not assignable to parameter of type Node< { label: string} > in typescript react #2110 data.label is not assignable to parameter of type Node< { label: string} > in typescript react #2110 Aldo111 opened this issue Apr 30, 2022 · 4 comments

Describe the Bug

TS2345: Argument of type '({ id: string; type: string; data: { label: string; }; position: { x: number; y: number; }; } | { id: string; data: { label: Element; }; position: { x: number; y: number; }; type?: undefined; })[]' is not assignable to parameter of type 'Node<{ label: string; }>[]'.
  Type '{ id: string; type: string; data: { label: string; }; position: { x: number; y: number; }; } | { id: string; data: { label: Element; }; position: { x: number; y: number; }; type?: undefined; }' is not assignable to type 'Node<{ label: string; }>'.
    Type '{ id: string; data: { label: JSX.Element; }; position: { x: number; y: number; }; type?: undefined; }' is not assignable to type 'Node<{ label: string; }>'.
      The types of 'data.label' are incompatible between these types.
        Type 'Element' is not assignable to type 'string'.

Following the initialNodes example from the Core Concepts page in a react/ts project causes an error in type definitions (perhaps with the latest version of react?):

id: "2", // you can also pass a React component as a label data: { label: <div>Default Node</div> }, position: { x: 100, y: 125 },

This node is the problematic one, which causes an error when used with
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

  • Use the initialNodes example from the Core Concepts page within a typescript/.tsx project file.
  • Expected behavior

    There shouldn't be any error in using this type but there is.

    Screenshots or Videos

    No response

    Platform

  • OS: Windows
  • Browser: Chrome
  • IDE: VS Code
  • Additional context

    No response

    Hey @SteveAtKlover

    all the examples in this repo are written in TS.
    For nodes you need to import the Node type from the react-flow-renderer package and use it like this:

    import { Node } from 'react-flow-renderer';
    const initialNodes: Node[] = {
      id: 'a',
      position: { x: 0, y:0 },
      data: { label: 'a node' }