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

Add type and props to ReactElement to permit restricting child type #619

Open
steinybot opened this issue Jan 18, 2023 · 0 comments
Open

Comments

@steinybot
Copy link
Contributor

There are some long standing issues in Typescript and its types for JSX which make it impossible to restrict the type of children that a component will accept (microsoft/TypeScript#13618 which is not fixed).

The Typescript type for ReactElement does have enough information to do this:

    interface ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> {
        type: T;
        props: P;
        key: Key | null;
    }

It is the JSX that has the issue as everything ends up as ReactElement<any, any>.

AFAIK Flow already supports this: https://flow.org/en/docs/react/children/#toc-only-allowing-a-specific-element-type-as-children

Any chance of getting support for specifying P and T?

There are a few other changes that would be needed to take full advantage of this such as ensuring that WithAttrs and KeyAddingStage captures these types. FunctionalComponentCore would just work since it has Props and Result. Component et al. would need a Result type.

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