Can't use custom props with Styled components in Typescript
See original GitHub issueHere’s my very simple component:
import React, { ReactChild, ElementType } from 'react'
import styled from '@emotion/styled'
type WrapperPropsType = {
size?: SizeType
}
type ButtonPropsType = {
as?: ElementType<any>,
size?: SizeType,
children?: ReactChild
}
const Wrapper = styled.button<WrapperPropsType>((props: WrapperPropsType) => ({
fontSize: FontSize[props.size],
}))
const Button = ({ as, size, children }: ButtonPropsType) => {
return (
<Wrapper as={as} size={size}>
{ children }
</Wrapper>
)
}
My .tsconfig
:
{
"compilerOptions": {
"types": ["node", "@emotion/react"],
"declaration": true,
"declarationDir": "build",
"module": "esnext",
"target": "es5",
"sourceMap": true,
"jsx": "preserve",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": ["src/**/*"],
"exclude": [
"node_modules",
"build",
"src/**/*.test.tsx"
]
}
But on compile I’m getting next error:
Error: src/components/Button/Button.tsx(18,30): semantic error TS2339: Property ‘size’ does not exist on type ‘{ theme?: Theme; as?: ElementType<any>; } & Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, “form” | … 265 more … | “onTransitionEndCapture”> & { …; }’.
What am I doing wrong? Thanks for any help
Packages versions:
@emotion/react
version: 11.1.2
@emotion/styled
version: 11.0.0
typescript
version: 4.1.3
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
styled components 'as' prop and custom prop in typescript
Show activity on this post. I'm getting an overload typescript warning when using styled components as prop with a custom prop. import React,...
Read more >How do I use Typescript custom props in styled-components ...
How to use custom props and theme with material-ui styled components API (Typescript)? ... How to use TypeScript with withRouter, connect, React.Component and ......
Read more >Using styled-components in TypeScript: A tutorial with examples
Here, you can learn how to build and style a TypeScript app using styled-components, with an e-commerce page as an example.
Read more >Advanced Usage - styled-components
To illustrate this, let's create our Button component, but this time we'll pass some variables down as a theme. // Define our button,...
Read more >Tooling - styled-components
babelrc file matters. If you're using the env property in your babel configuration, then putting this plugin into the plugins array won't suffice....
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
@Andarist repository: https://github.com/VladimirHumeniuk/samurai-ui , to get an error you have to run build script:
npm i && npm run build
Would be amazing if you can take a look because I have checked all similar issues on SO and here, I have checked the documentation a million times and have already dropped my hands, have no idea what I’m doing wrong.
🙏
Glad you have been able to fix this on your side 👍