Skip to content

Remove @emotion/styled dependency #1183

@MMT-LD

Description

@MMT-LD

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 ;-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    affects-docsChanges affect documentation, but not only documentationenhancementNew feature or requesttypes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions