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

Wrong prop types in styled withComponent component #1991

Closed
MikelArnaiz opened this issue Sep 3, 2020 · 1 comment
Closed

Wrong prop types in styled withComponent component #1991

MikelArnaiz opened this issue Sep 3, 2020 · 1 comment
Labels

Comments

@MikelArnaiz
Copy link

MikelArnaiz commented Sep 3, 2020

Current behavior:

Appying styled to the result component of .withComponent() has the wrong prop types.

E.g in the following code, typescript complains that onSubmit might be of type (event: FormEvent<HTMLDivElement>) => void). I guess that the HTMLDivElement part is comming from the original Box component, which was a div:

const Box = styled.div`
  border: 1px solid red;
`;

const FormBox = Box.withComponent("form");

const Form = styled(FormBox)`
  background-color: "white";
`;

const onSubmit = (ev: React.FormEvent<HTMLFormElement>) => {
  ev.preventDefault();
  console.log("onSubmit");
};

export const App = () => {
  return (
    <Form onSubmit={onSubmit}>
      <input type="submit" value="Submit" />
    </Form>
  );
};

Note the type of FormBox, references to HTMLDivElement are still there.

Screenshot 2020-09-03 at 15 32 44

Full error message:

Type '(ev: React.FormEvent<HTMLFormElement>) => void' is not assignable to type '((event: FormEvent<HTMLFormElement>) => void) & ((event: FormEvent<HTMLDivElement>) => void)'.
  Type '(ev: React.FormEvent<HTMLFormElement>) => void' is not assignable to type '(event: FormEvent<HTMLDivElement>) => void'.
    Types of parameters 'ev' and 'event' are incompatible.
      Type 'FormEvent<HTMLDivElement>' is not assignable to type 'FormEvent<HTMLFormElement>'.
        Type 'HTMLDivElement' is missing the following properties from type 'HTMLFormElement': acceptCharset, action, autocomplete, elements, and 12 more.

To reproduce:

  1. Clone this repo
  2. yarn install
  3. yarn start

Expected behavior:

Component props types should be of the end html element type.

Environment information:

"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.3",
"typescript": "~3.7.2"
"@emotion/core": "^10.0.35",
"@emotion/styled": "^10.0.27",

Workaround

Until fixed a possible workarround is to cast the event as any:

    <Form onSubmit={(ev: any) => {
      onSubmit(ev)
    }}>
@Andarist
Copy link
Member

Andarist commented Sep 8, 2020

This has been fixed in the upcoming v11. It's available on npm and no major breaking changes are planned before the final release - please try it out or wait until we release it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants