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

Event handlers do not infer props correctly #2

Closed
nasheomirro opened this issue Jan 7, 2023 · 2 comments
Closed

Event handlers do not infer props correctly #2

nasheomirro opened this issue Jan 7, 2023 · 2 comments

Comments

@nasheomirro
Copy link
Owner

nasheomirro commented Jan 7, 2023

Components have the correct types but are inferring incorrectly:

const Button = polyRef<"button">(
  ({ as: As = "button", ...props }) => {
    return <As {...props} />;
  }
);

// `event` is of type `any` when it should be of type `React.MouseEvent<HTMLButtonElement, MouseEvent>`
<Button onClick={event => {}} />

Note that onClick still has the correct type, it's just that typescript isn't inferring our function. What's weirder is that if you provide an as prop, or explicitly provide a value for the component type, then it will infer correctly.

// both `event`s have the correct type
<Button<"button"> onClick={event => {}} />;
<Button as="button" onClick={event => {}} />;

Helpful links:
kripod/react-polymorphic-types#5
microsoft/TypeScript#44596

@nasheomirro nasheomirro changed the title Event handlers on polyRef Components do not infer props correctly Event handlers do not infer props correctly Jan 8, 2023
@nasheomirro
Copy link
Owner Author

nasheomirro commented Jan 8, 2023

Looks like this issue is related to how typescript does inference, see accepted answer. I am not sure if there is a solution at the moment.

@nasheomirro
Copy link
Owner Author

After some time the trick with commit 03bf3db is now un-needed, I don't know what changed but it has to be some update to typescript or maybe ts-server (idk).

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