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.

TypeScript definitions for `styled` miss `target` prop for material ui Button

See original GitHub issue

Current behavior:

When using styled (either from @emotion/styled or via @mui/material) to style <Button> from @mui/material, TypeScript complains about target prop. It does not complain about the prop on bare <Button>.

The prop works as expected though, the error is only with TypeScript.

To reproduce:

Reproduction provided by Material UI team at https://github.com/mui/material-ui/issues/31314#issuecomment-1067776719

https://codesandbox.io/s/hardcore-danilo-0e086c?file=/src/App.tsx

  1. Wrap <Button> with styled
  2. Use new component with target prop

TS reports this error:

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 ... & { ...; }'

This can be workarounded with styled(Button)(....) as unknown as typeof Button.

Expected behavior:

Prop works w/o TypeScript errors.

Environment information:

  • react version: 17.0.2
  • @emotion/react version: 11.8.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
srmaguracommented, Mar 23, 2022

Thanks @mnajdova. Seems like it is not practical for us to work around this limitation in TypeScript so I am closing the issue.

0reactions
mnajdovacommented, Mar 23, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

TS types for `styled` and `Button` are not compatible: target ...
I am trying to customize Button using styled from @mui/material . Your environment. See codesandbox.
Read more >
'styled()' function drops 'component' prop support
ButtonBase is working as expected, but suddenly there is an error from Typescript, telling that there is no 'component' prop on MyButton ....
Read more >
TypeScript - Material UI - MUI
Many components concerned with user input offer a value prop or event handlers which include the current value . In most situations that...
Read more >
How to use styled components with Material UI in a React app
It works as follow: import styled from "styled-components"; import Button from "@material-ui/core/Button"; export default styled(Button)` color ...
Read more >
react-dropzone
Simple React hook to create a HTML5-compliant drag'n'drop zone for files. Documentation and examples at https://react-dropzone.js.org.
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