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.

Remove @emotion/styled dependency

See original GitHub issue

Hey,

If i use Box and then i apply ref forwarding on a new component called button. It would seem that in typescript when using forward ref it seems that box is set to HTMLDivElement and if i supply HTMLButtonElement we get error that it needs to be a div element, i assume this is to do with the ‘as’ prop?

Just wandered what i’m doing wrong - i even tried the typings for ForwardRef and it still did not work. TS version 4.0.2

for example:

import * as React from 'react';
import { Box, BoxProps } from 'my-lib';

export interface IButtonProps extends BoxProps {
  label: string;
  variant?: 'primary' | 'secondary' | 'tertiary';
  children: React.ReactNode;
}

const Button = React.forwardRef<
  HTMLButtonElement,
  IButtonProps,
>(({ label = 'change me', variant = 'primary', ...rest }, ref) => {
  return (
    <Box
      as="button"
      ref={ref}
      variant={variant}
      aria-label={label}
      {...rest}
    />
  );
});

export { Button };
Type '((instance: HTMLButtonElement | null) => void) | MutableRefObject<HTMLButtonElement | null> | null' is not assignable to type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.
  Type '(instance: HTMLButtonElement | null) => void' is not assignable to type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.
    Type '(instance: HTMLButtonElement | null) => void' is not assignable to type '(instance: HTMLDivElement | null) => void'.
      Types of parameters 'instance' and 'instance' are incompatible.
        Type 'HTMLDivElement | null' is not assignable to type 'HTMLButtonElement | null'.
          Type 'HTMLDivElement' is missing the following properties from type 'HTMLButtonElement': disabled, form, formAction, formEnctype, and 13 more.ts(2322)
index.d.ts(143, 9): The expected type comes from property 'ref' which is declared here on type 'IntrinsicAttributes & ClassAttributes<HTMLDivElement> & HTMLAttributes<HTMLDivElement> & Pick<...> 

for now my fix is lame but its all i have got 😉 ref={ref as any}

looking on emotion comments and it looks like the as prop has been thought about by different source - maybe a good reference?

https://github.com/kripod/react-polymorphic-box/blob/main/src/Box.tsx

Thanks in advance 😉

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
hasparuscommented, Sep 13, 2020

could support the ‘As’ prop like Chakra, looks like they made significant progress over the last month, then that would be an amazing feature

I’ve spent a bunch of time fiddling with it, and I’m not convinced it’s an amazing feature. TypeScript compilation times suffer drastically in large codebases using as prop (I even managed to get an OOM 😄). It’s a really cool gun to shoot oneself in the foot. I did it, and I’d strongly advise to use sx prop with jsx pragma in TypeScript.

Pretty soon, with React Automatic Runtime, you won’t even need /** @jsx jsx */ comment.

Additional context:

https://blog.andrewbran.ch/polymorphic-react-components/

1reaction
lachlanjccommented, Dec 29, 2021

Papertrail: we deprecated .withComponent some time ago & will be fully removing it soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to remove Emotion - Next Right Now
Remove the following libraries: @emotion/core : Necessary to use emotion, with built-in css notation support. @emotion/styled : Necessary to used the styled ......
Read more >
@emotion/styled - npm
styled API for emotion. Latest version: 11.10.5, last published: 2 months ago. Start using @emotion/styled in your project by running `npm i ...
Read more >
@emotion/styled | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Install - Emotion
styled is a way to create React components that have styles attached to them. # assuming you already have @emotion/react installed yarn add...
Read more >
Module not found: Can't resolve '@emotion/react'
npm install @emotion/react npm install @emotion/styled ... try to delete the .next dir after installing the dependencies.
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