question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Destructuring props on a styled component throws a type error on property "as"

See original GitHub issue

Current behaviour: Receive the following error when destructuring props onto an emotion/styled component. Type '{ children: (ReactNode | Element)[]; accept?: string | undefined; acceptCharset?: string | undefined; action?: string | undefined; allowFullScreen?: boolean | undefined; ... 356 more ...; key?: Key | ... 1 more ... | undefined; }' is not assignable to type '{ theme?: Theme | undefined; as?: ElementType<any> | undefined; }'. Types of property 'as' are incompatible. Type 'string | undefined' is not assignable to type 'ElementType<any> | undefined'. Type 'string' is not assignable to type 'ElementType<any> | undefined'. This occurs on any tag you destruct functional component props on. styled.<element>'s “as” property doesn’t correspond with React.HTMLProps<HTMLElement>'s “as” property (can also be HTMLButtonElement).

To reproduce: See Example Here.

  1. Create a new functional component.
  2. Within functional component, add an emotion styled component.
  3. Add some arbitrary properties (for example elementHeight, doesn’t really matter) to the React.FC<Props> Props interface.
  4. Make sure to extend the React element of whatever HTML element you wish to destruct default properties on.
  5. destruct props ({…props}) on the intended HTML element

Expected behaviour:

  • type of “as” corresponds with the React HTMLElement’s type of “as”.
  • remaining properties that aren’t unique or overriden in FC<Props> declaration are destructured appropriately.

Environment information:

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Andaristcommented, Aug 5, 2021

I recommend using React.HTMLAttributes<HTMLElement> over React.HTMLProps<HTMLElement> because this is a type of props for <nav/> element. I’m not exactly sure in what context HTMLProps should be used.

2reactions
robinmetralcommented, Sep 24, 2021

It also seems that HTMLProps<HTMLInputElement> includes extra props (in contrast to HTMLAttributes<HTMLInputElement>), like the disabled and required props.

So switching to HTMLProps worked for me in some components, but not all—I can also Omit<HTMLProps<HTMLInputElement>, "as"> but it’s not super nice.

Did you end up solving this differently?


Edit:

Hm so I found this and it looks like the way to go would be InputHTMLAttributes<HTMLInputElement>. Works well on my end and I could clean up some old Omit<> 🎉

The only thing I’m not sure of is why some elements are missing their *HTMLAttributes types, like ul. I assume that it’s because they don’t have any other props beyond the HTMLAttribute type (all other element-specific generics extend it), so for these I’d just use it directly: HTMLAttributes<HTMLUListElement>.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript and destructured variables not recognising props
In the above statement you try to destructure the data key. But data is just of object type. Hence you get the error....
Read more >
Destructuring props in styled-components - Dom Habersack
In this example, we have to write props. three times. This becomes more tedious as we pass more properties and change more styles....
Read more >
Typescript fails to infer props when destructuring props with ...
Typescript isn't complaining when a React Functional Component (React.FC<Props>) with wrong props is send as a prop? Why is typescript triggering an error...
Read more >
React Props Cheatsheet: 10 Patterns You Should Know
If prop values are not passed a certain component, an error will not be thrown. Instead, within the component that prop will have...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
You can end a destructuring pattern with a rest property ...rest . This pattern will store all remaining properties of the object or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found