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.

Using `as` prop throws error when using TypeScript

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
nonameolssoncommented, Sep 9, 2021

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

1reaction
ben-rogersoncommented, Nov 21, 2020

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

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

React component throwing TypeScript error for function ...
The first param to Component type is props type , and you have not defined it, therefore the error. Define a interface, interface...
Read more >
Typescript component does not throw error if different prop ...
I am in the process of migrating my React project to typescript. I've managed to convert a smaller component and it's working as...
Read more >
Documentation - JSX - TypeScript
Using JSX with TypeScript. ... but <bar /> will result in an error since it has not been specified on JSX. ... props;...
Read more >
typescript react component type as prop - Industry Today
The ref is set when the element is declared using the ref prop: If a different element than div is used, the Typescript...
Read more >
Typing Component Props - React TypeScript Cheatsheets
Basic Prop Types Examples​. A list of TypeScript types you will likely use in a React+TypeScript app: type AppProps = { message: string;...
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