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.

[styled(Link)] TypeScript error: Link does not accept `component` attribute

See original GitHub issue
  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

I should be able to specify which component my styled <Link> should use to display, e.g.:

import { Link as RouterLink } from "react-router-dom"
import { Link } from "@material-ui/core"
import { styled } from "@material-ui/styles"

const StyledLink = styled(Link)({
  color: "red",
})

export default () => 
  <StyledLink component={RouterLink} to="/password">Forgot your password?</StyledLink>

Current Behavior 😯

TypeScript error in Login.tsx(123,16):
Type '{ children: string; component: typeof Link; }' is not assignable to type 'IntrinsicAttributes & Pick<DefaultComponentProps<{ props: AnchorHTMLAttributes<HTMLAnchorElement> & Pick<TypographyProps, "style" | "title" | "color" | "display" | "hidden" | "ref" | "children" | ... 255 more ... | "variantMapping"> & { ...; }; defaultComponent: "a"; classKey: LinkClassKey; }>, "style" | ... 270 mor...'.
  Property 'component' does not exist on type 'IntrinsicAttributes & Pick<DefaultComponentProps<{ props: AnchorHTMLAttributes<HTMLAnchorElement> & Pick<TypographyProps, "style" | "title" | "color" | "display" | "hidden" | "ref" | "children" | ... 255 more ... | "variantMapping"> & { ...; }; defaultComponent: "a"; classKey: LinkClassKey; }>, "style" | ... 270 mor...'.  TS2322

Steps to Reproduce 🕹

https://codesandbox.io/s/loving-cannon-ed5e8

Context 🔦

I want to style to my link, but maybe there are other ways 😃

Your Environment 🌎

Tech Version
@material-ui/core 4.0.0-beta.2
@material-ui/styles 4.0.0-beta.2
react-router-dom @5.0.0
React 16.8.6
Browser Chrome 74 macOS
TypeScript 3.4.5

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
eps1loncommented, May 20, 2019

This is a TypeScript limitation regarding argument inference and overloaded function signatures.

const StyledLink = styled(Link)({
  color: "red",
}) as Link;

should fit too but removes all types for props from styled.

8reactions
WillSquirecommented, Dec 2, 2021

This is a TypeScript limitation regarding argument inference and overloaded function signatures.

const StyledLink = styled(Link)({
  color: "red",
}) as Link;

should fit too but removes all types for props from styled.

Shouldn’t this be as typeof Link? I.e:

const StyledLink = styled(Link)({
  color: "red",
}) as typeof Link;
Read more comments on GitHub >

github_iconTop Results From Across the Web

[styled(Link)] TypeScript error: Link does not accept ... - GitHub
This is a TypeScript limitation regarding argument inference and overloaded function signatures. const StyledLink = styled(Link)({ ...
Read more >
React styled-component with Typescript, types error
I have a isActive prop on the Link Styled component. The console complains about this because it dose not recognize isActive as a...
Read more >
TypeScript - Emotion
When using our JSX factory, TypeScript only allows the css prop on components that accept a className prop. This is because @emotion/react resolves...
Read more >
Advanced Usage - styled-components
This component provides a theme to all React components underneath itself via the context API. In the render tree all styled-components will have...
Read more >
Useful Patterns by Use Case - React TypeScript Cheatsheets
Type '"foo"' is not assignable to type '"button" | "submit" | "reset" | undefined'.(2322) // return <Button type="foo"> sldkj </Button> // no error...
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