TypeScript error when component renders a styled component
See original GitHub issueCurrent 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.0emotion
version: 10.0.27
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:14 (5 by maintainers)
Top 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 >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
@criles25 React types exposes
ComponentProps
type. It will extract the types of a component, so no need to manually add HTMLProps. I would try:The solution is to use the
React.HTMLProps
.