-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
Using as
prop throws error when using TypeScript
#180
Comments
There seems to be an issue with this typing twin provides: export type TwComponent<K extends keyof JSX.IntrinsicElements> = (
props: JSX.IntrinsicElements[K]
) => JSX.Element But you can add your own workaround for now: // twin.d.ts
// The 'as' prop for styled components
declare global {
namespace JSX {
interface IntrinsicAttributes<T> extends DOMAttributes<T> {
as?: string | Element
}
}
} If anyone wants to help with the updated types please chime in. |
I've updated twin's typescript docs to include the workaround. Feel free to reopen if you want to adjust the core typings. |
@ben-rogerson Somewhat related, and I can open a separate issue if needed. Adding the types in the docs throws this type error for me. I have
tsconfig.json
|
The same happens for me! :( |
Update! I just got it to work (I think). // twin.d.ts
// The 'as' prop for styled components
declare global {
namespace JSX {
interface IntrinsicAttributes<T> extends DOMAttributes<T> {
as?: string | React.ElementType // CHANGE TO THIS
}
}
} |
Add missing type for prop `as`. This removes the need for the workaround proposed here: - #180 Also, it works better because the `as` prop is only available for styled components.
Thanks to @klzns the 'as' prop is now part of the twin types so the custom global styles mentioned above won't be needed anymore 🎉 |
The problem
Using the
as
prop onTwComponent
andStyledComponent
raises an error when using TypeScript. Not sure if the problem is directly related totwin.macro
:Although using the prop works as expected in production if you use
// @ts-expect-error
to sidestep the issue.The text was updated successfully, but these errors were encountered: