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.

TS types for `styled` and `Button` are not compatible: target attribute is not recognized

See original GitHub issue

Duplicates

  • 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:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
mnajdovacommented, Mar 23, 2022

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:

const MyButton = styled(Button)({
  color: "red"
}) as typeof Button;

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.

2reactions
mnajdovacommented, Jun 1, 2022

The docs was updated in https://github.com/mui/material-ui/pull/32168, I am closing this.

Read more comments on GitHub >

github_iconTop 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 >
Docs • Svelte
Complete documentation for Svelte.
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 >

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