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

Using as prop throws error when using TypeScript #180

Closed
arafatamim opened this issue Oct 31, 2020 · 6 comments
Closed

Using as prop throws error when using TypeScript #180

arafatamim opened this issue Oct 31, 2020 · 6 comments

Comments

@arafatamim
Copy link

The problem

Using the as prop on TwComponent and StyledComponent raises an error when using TypeScript. Not sure if the problem is directly related to twin.macro:

Property 'as' does not exist on type 'IntrinsicAttributes & ClassAttributes<HTMLAnchorElement> & AnchorHTMLAttributes<HTMLAnchorElement>'

Although using the prop works as expected in production if you use // @ts-expect-error to sidestep the issue.

@ben-rogerson
Copy link
Owner

ben-rogerson commented Nov 1, 2020

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.

@ben-rogerson
Copy link
Owner

I've updated twin's typescript docs to include the workaround.

Feel free to reopen if you want to adjust the core typings.

@garand
Copy link

garand commented Feb 22, 2021

@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 @types/react installed.

node_modules/@emotion/react/types/jsx-namespace.d.ts(17,36): error TS2314: Generic type 'IntrinsicAttributes<T>' requires 1 type argument(s).
node_modules/@types/react/index.d.ts(2990,19): error TS2428: All declarations of 'IntrinsicAttributes' must have identical type parameters.
node_modules/twin.macro/types/index.d.ts(51,15): error TS2428: All declarations of 'IntrinsicAttributes' must have identical type parameters.
src/types/twin.d.ts(18,15): error TS2428: All declarations of 'IntrinsicAttributes' must have identical type parameters.
src/types/twin.d.ts(18,46): error TS2304: Cannot find name 'DOMAttributes'.

tsconfig.json

{
  "compilerOptions": {
    "strict": true,
    "emitDeclarationOnly": true,
    "declaration": true,
    "declarationDir": "dist",
    "moduleResolution": "node",
    "jsx": "react-jsx"
  },
  "include": ["src"]
}

@nonameolsson
Copy link

The same happens for me! :(

@nonameolsson
Copy link

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

ben-rogerson pushed a commit that referenced this issue Jan 19, 2024
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.
@ben-rogerson
Copy link
Owner

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 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants