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 error when component renders a styled component

See original GitHub issue

Current behavior: I’m using styled to create a button. In my implementation, my button is a function component that returns a styled component.

TypeScript throws an error when I use it.

To reproduce: My button implementation

// Button.tsx

type Size = "small" | "medium" | "large";

type ButtonProps = {
  size?: Size;
  kind?:
    | "primary"
    | "text-primary"
    | "cta"
    | "gray"
    | "danger"
    | "danger-neutral"
    | "warning";
  link?: boolean;
};

const ButtonStyled = styled.button`
/* styles */
`
const Button = (props: ButtonProps) => <ButtonStyled {...props} />

export default Button;
import Button from 'Button.tsx';

// tsx does not complain
<Button size='small'></Button>
// tsx does not complain
<Button>foo</Button>
// tsx complains
<Button size='small'>foo</Button>
Type '{ children: string; size: "small"; }' is not assignable to type 'IntrinsicAttributes & Pick<Pick<ButtonProps, "size" | "link" | "kind"> & Pick<InferProps<{ size: Requireable<string>; kind: Requireable<string>; link: Requireable<...>; }>, never> & Pick<...>, never> & Partial<...> & Partial<...>'.
  Property 'children' does not exist on type 'IntrinsicAttributes & Pick<Pick<ButtonProps, "size" | "link" | "kind"> & Pick<InferProps<{ size: Requireable<string>; kind: Requireable<string>; link: Requireable<...>; }>, never> & Pick<...>, never> & Partial<...> & Partial<...>'
  • react version: 16.12.0
  • emotion version: 10.0.27

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
jonavilacommented, Feb 24, 2020

@criles25 React types exposes ComponentProps type. It will extract the types of a component, so no need to manually add HTMLProps. I would try:

const Button: React.FC<ButtonProps & React.ComponentProps<'button'>> = (props) => <ButtonStyled {...props} />
4reactions
criles25commented, Feb 19, 2020

The solution is to use the React.HTMLProps.

const Button: React.FC<ButtonProps & React.HTMLProps<HTMLButtonElement>> = (props) => <ButtonStyled {...props} />
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prop TypeScript Error With React Component, Using Styled ...
When I hover over {({ disabled } , TypeScript is giving me a Property 'disabled' does not exist on type 'Pick<DetailedHTMLProps< ...
Read more >
styled-components typescript error with Material-UI ...
This was working fine a few months ago, but I just started a new project and am having the same issue. Must be...
Read more >
API Reference - styled-components
To prevent TypeScript errors on the css prop on arbitrary elements, install @types/styled-components and add the following import once in your project:.
Read more >
styled-components - npm
Start using styled-components in your project by running `npm i ... Create a <Title> react component that renders an <h1> which is ...
Read more >
Using Styled Components - Expo Documentation
Styled Components is a CSS-in-JS solution that enables you to create React components with a given style very easily. Using styled-components with Expo, ......
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