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

Consufing error when forgot to define JSX.ElementChildrenAttribute type #60572

Open
uhyo opened this issue Nov 23, 2024 · 0 comments
Open

Consufing error when forgot to define JSX.ElementChildrenAttribute type #60572

uhyo opened this issue Nov 23, 2024 · 0 comments

Comments

@uhyo
Copy link
Contributor

uhyo commented Nov 23, 2024

πŸ”Ž Search Terms

TS2745 This JSX tag's children expects type which requires multiple children but only a single child was provided
ElementChildrenAttribute

πŸ•— Version & Regression Information

  • This changed between versions v4.9.5 and 5.0.4

⏯ Playground Link

https://www.typescriptlang.org/play/?jsx=4#code/CYUwxgNghgTiAEBzCB7ARlC8DeAoe8AdlALYgDOADlGAgFIDKAGjvgfAJaEAuIMAZjQQBJHjC7kOYAKIQQZHuVbt2ACwCMALhwBfNgT3743AJ6UEs+SB7wAvPHLdxhRAG5cRgPSfOPPoNp4SwVuAGFVDghgOEIAQW4nDjQAV15ldm92MAiomO1sHXcVTL0DXEMwFEJHeAAVDm45O3gACkoYFEpyfPhsyOjrbUdnRHgdAEo7AD54AB4NKex2zvIAOj7c6x1ZzwX3XErq7ngQORDm2frGkCmACVPUABp4AHcUGCiAQh2ruSnXIA

πŸ’» Code

declare global {
  namespace JSX {
    interface IntrinsicElements {
      h1: {}
    }

    type Element = string;

    // interface ElementChildrenAttribute {
    //   children: {};
    // }
  }
}

const Title = (props: { children: string }) => <h1>{props.children}</h1>;

const element = <Title>Hello, world!</Title>;

πŸ™ Actual behavior

You get the following error on <Title>:

This JSX tag's 'children' prop expects type 'string' which requires multiple children, but only a single child was provided.

The error message is incorrect in that TS believes string requires multiple children, which actually does not.

πŸ™‚ Expected behavior

No error; string already accepts single children and actually a single children is provided to Title.

Additional information about the issue

The confusing error disappears when you define ElementChildrenAttribute.

I'm not sure whether function components are supposed to typecheck without an explicit definition of ElementChildrenAttribute, or whether we must define it. However, I believe that ElementChildrenAttribute plays no role under "jsx": "react-jsx". Probably ElementChildrenAttribute should not affect type checking under this mode.

I'm reporting this because at least the current error message doesn't look right.


Note that in 4.9.5 the error message is different:

Property 'children' is missing in type '{}' but required in type '{ children: string; }'

which kind of makes more sence.

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

No branches or pull requests

1 participant