Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Styling components with required props triggering an TypeScript error #472

Open
theBork opened this issue Jul 17, 2022 · 4 comments
Open
Labels
bug Something isn't working

Comments

@theBork
Copy link

theBork commented Jul 17, 2022

When I try to wrap custom React components with goober's "styled" method, TypeScript shows error im cases when component have required props.

import React from 'react'
import { styled } from 'goober'

interface IProps {
  myProp: string
}

const Component: React.FC<IProps> = (props) => {
  return <div>{props.myProp}</div>
}

const StyledComponent = styled(Component)``
/* Argument of type 'FC<IProps>' is not assignable to parameter of type 'Element | ElementClass'.
Type 'FunctionComponent<IProps>' is missing the following properties from type 'ElementClass': render, context, setState, forceUpdate, and 3 more. */

When I change myProp to optional, there is no errors.

TypeScript: 4.7.4
Goober: 2.1.10

Sandbox with problem's example: https://codesandbox.io/s/goober-typescript-styled-custom-component-y25bq0?file=/src/Component.tsx

@cristianbote
Copy link
Owner

Thanks @theBork for opening this issue.

Might be due to this line https://github.com/cristianbote/goober/blob/master/goober.d.ts#L32 wondering how can I add the FC interface in there 🤔. Is there a JSX.FC maybe?

@theBork
Copy link
Author

theBork commented Jul 18, 2022

@cristianbote yes, I think that JSX.Element, that used in this line of code describes type of Element like that:

<Component myProp={myProp} />

or

React.createElement(Component, { myProp })

When we provide our component as argument to styled method, they are not Element yet, so I think changing to JSX.FC will help. I can make PR next days, if you want :)

@cristianbote
Copy link
Owner

That would be super helpful @theBork! Much appreciated.

@cristianbote cristianbote added the bug Something isn't working label Jul 18, 2022
@86Magical
Copy link

86Magical commented Jul 20, 2022

One way i was able to solve this was adding types to the styled(tag)

i already created a pull request for it

Also i was able to get it done with

React.ElementType as well but i couldn't add that to the repo as Goober is used/can be used in a none react app
8b7e289

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants