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

fix: do not use JSX.ElementChildrenAttribute under jsx: react-jsx or jsx: react-jsxdev #60880

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

uhyo
Copy link
Contributor

@uhyo uhyo commented Dec 30, 2024

Happy Holidays! Fixes #60572

🖼️ Background

Under jsx: react-jsx or jsx: react-jsxdev, the name of the prop for JSX children is always children. The jsx transformer properly implements this:

return result && factory.createPropertyAssignment("children", result);

However, the type checker didin't take this into consideration. Instead, it used JSX.ElementChildrenAttribute to determine the name of prop. If type definition lacks this interface, undesirable compiler errors are emitted (like the one mentioned in the linked issue).

It is of note that, I believe, no practical type definition for JSX libraries ever provided such definition that would lead to errors like those.

That said, I think the current behavior is still a bit harmful to those who want to learn how to write type definitions for JSX libraries, so proposing a fix.

🦾 Solution

This PR fixes the getJsxElementChildrenPropertyName function so that children is hard-coded under these jsx modes.

🌵 Current Behavior

code:

//@filename: /jsx.d.ts
declare namespace JSX {
  interface IntrinsicElements {
    h1: { children: string }
  }

  type Element = string;
}

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

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

Errors:

    const Title = (props: { children: string }) => <h1>{props.children}</h1>;
                                                    ~~
!!! error TS2741: Property 'children' is missing in type '{}' but required in type '{ children: string; }'.
!!! related TS2728 /jsx.d.ts:3:11: 'children' is declared here.

    const element = <Title>Hello, world!</Title>;
                     ~~~~~
!!! error TS2741: Property 'children' is missing in type '{}' but required in type '{ children: string; }'.
!!! related TS2728 /test.tsx:1:25: 'children' is declared here.

✨ New Behavior

(No error)

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Dec 30, 2024
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be good to add a test case that defines ElementChildrenAttribute as something different than children to verify that it's ignored in favor of "hard-coded" children

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! Added a test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Confusing error when forgot to define JSX.ElementChildrenAttribute type
3 participants