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.

Can't use custom props with Styled components in Typescript

See original GitHub issue

Here’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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
VladimirHumeniukcommented, Dec 21, 2020

@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.

🙏

0reactions
Andaristcommented, Dec 21, 2020

Glad you have been able to fix this on your side 👍

Read more comments on GitHub >

github_iconTop 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 >

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