tailwindlabs / heroicons

A set of free MIT-licensed high-quality SVG icons for UI development.
https://heroicons.com
MIT License
21.07k stars 1.27k forks source link

Supertype for icons? #542

Closed RobRoseKnows closed 2 years ago

RobRoseKnows commented 2 years ago

As of #64, there's now Typescript types for the icons. I was hoping though that there could be some sort of "supertype" for all the icons, so that I could define a React component that would require an icon instead of just a generic ReactNode. My usecase is I'm trying to make a generic version of this component from Tailwind UI, and it would be nice if I could make it so a generic icon could be provided.

timroes commented 2 years ago

Since all icons are just taking svgs props into them (since they are just that), you can define a type for any generic icons, like:

type Icon = React.ComponentType<React.ComponentProps<'svg'>>;
RobRoseKnows commented 2 years ago

Good point, I ended up figuring that out and forgot to close earlier. Thanks!

jaidetree commented 1 year ago

It seems in a recent update that definition no longer applies. Anyone know what it should be now?

michaelphines commented 1 year ago

This works for me:

export type HeroIcon = React.ComponentType<
  React.PropsWithoutRef<React.ComponentProps<"svg">> & {
    title?: string | undefined;
    titleId?: string | undefined;
  }
>;

I think you can also just do something like get the typeof an example icon:

import { PencilIcon } from "@heroicons/react/24/outline";
export type HeroIcon = typeof PencilIcon;

Both have upsides and downsides.