tabler / tabler-icons

A set of over 5500 free MIT-licensed high-quality SVG icons for you to use in your web projects.
https://tabler.io/icons
MIT License
18.03k stars 898 forks source link

`IconProps` type is incompatible with icons components #1077

Closed rtivital closed 5 months ago

rtivital commented 6 months ago

The following code will have a TypeScript error:

import { IconCreditCard, IconProps } from "@tabler/icons-react";

// Error about incompatible `ref` prop
function TestIcon(props: IconProps) {
  return <IconCreditCard {...props} />;
}

Sandbox with a reproduction – https://codesandbox.io/p/sandbox/mantine-react-template-forked-jhy3jl?file=%2Fsrc%2FApp.tsx%3A3%2C1-7%2C2

The error is gone once ref prop is removed:

// ok
function TestIcon(props: Omit<IconProps, 'ref'>) {
  return <IconCreditCard {...props} />;
}

To fix types mismatch change IconProps type from

interface IconProps extends Partial<Omit<React.SVGProps<SVGSVGElement>, 'stroke'>> {
    size?: string | number;
    stroke?: string | number;
}

to

interface IconProps extends Partial<Omit<React.ComponentPropsWithoutRef<'svg'>, 'stroke'>> {
  size?: string | number;
  stroke?: string | number;
}
diego-menezes commented 5 months ago

i cant't use newer version becasue this bug...

diego-menezes commented 5 months ago

@rtivital you know a workarround for this?

diego-menezes commented 5 months ago

the solution for me

in 2.x i use

export interface Acao {
...
    icone: (props: TablerIconsProps) => ReactElement;
...
}

now in 3.2.0

export interface Acao {

    icone: React.ExoticComponent<React.RefAttributes<Icon>>;

}

and where i used Icon in props needs no changes.

export const menu: Acao[] = [
  {
...
    icone: IconDashboard,
...
  },
....