TS types for `styled` and `Button` are not compatible: target attribute is not recognized
See original GitHub issueDuplicates
- I have searched the existing issues
Latest version
- I have tested the latest version
Current behavior 😯
If I use styled
override with Button
component, TS suddenly complains about target
prop, though it works fine with bare Button
from material.
const MyButton = styled(Button)({
color: 'red'
});
export default function App() {
return (
<MyButton
variant="outlined"
href="https://google.com"
target="_blank"
>
Google
</MyButton>
);
}
The error is:
Type '{ children: string; variant: "outlined"; href: string; target: string; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | "primary" | ... 5 more ... | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & ... 4 more ... & { ...; }'.
Property 'target' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | "primary" | ... 5 more ... | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & ... 4 more ... & { ...; }'
However, the rendered component works as expected. Currently I work around this problem with styled(Button)(....) as unknown as typeof Button
.
Expected behavior 🤔
I expected styled
-wrapper support all props the wrapped component supports, in this case — target
.
Steps to reproduce 🕹
https://codesandbox.io/s/romantic-allen-3qyg7b
Open App.tsx
in code sandbox and see the error reported on target
prop on MyButton
. The identical usage of plain Button
gives no errors.
Context 🔦
I am trying to customize Button
using styled
from @mui/material
.
Your environment 🌎
See codesandbox
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Styled components + typescript: "as" is not assignable to type ...
in completion to @Aron's answer , this code working for me ( styled-component + styled-system ) { as?: ComponentType<any> | keyof JSX.
Read more >API Reference - styled-components
Returns a function that accepts a tagged template literal and turns it into a StyledComponent. // import styled from 'styled-components' const Button =...
Read more >The Label element - HTML: HyperText Markup Language | MDN
The label text is not only visually associated with its corresponding text input; it is programmatically associated with it too.
Read more >Binding syntax - Angular
Notice that the binding is to the disabled property of the button's DOM element, not the attribute. Data binding works with properties of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Makes sense, now that I think about it, we had similar issues before, for example https://github.com/mui/material-ui/issues/21727#issuecomment-869228390
I don’t think we can do much better, so I would propose casting the styled component:
See https://codesandbox.io/s/reverent-jennings-ldwick?file=/src/App.tsx. We should add this on our
TypeScript
guide to avoid duplicated issue in the future. Thanks everyone for the help.The docs was updated in https://github.com/mui/material-ui/pull/32168, I am closing this.