Skip to content

Instantly share code, notes, and snippets.

@LeandrodeLimaC
Created March 21, 2024 16:43
Show Gist options
  • Save LeandrodeLimaC/6a3025adc168ee282fa5520452a2cade to your computer and use it in GitHub Desktop.
Save LeandrodeLimaC/6a3025adc168ee282fa5520452a2cade to your computer and use it in GitHub Desktop.
/*
TODO: Simplify this type by creating a PolymorphicComponentWithoutRef
first and then a PolymorphicComponentWithRef using React.ElementRef<'div'>
*/
/**
* Props for a polymorphic React component that includes a ref.
*
* @template E - The expected React element type.
* @template P - Additional props specific to the component.
*/
export type PolymorphicComponentPropsWithRef<
E extends React.ElementType,
P = {},
> = DistributiveOmit<
React.ComponentPropsWithRef<React.ElementType extends E ? 'div' : E>,
'as' & keyof P
> &
P &
AsProp<E>
/**
* Props for a polymorphic React component without a ref.
*
* @template E - The expected React element type.
* @template P - Additional props specific to the component.
*/
export type PolymorphicComponentPropsWithoutRef<
E extends React.ElementType,
P = {},
> = DistributiveOmit<PolymorphicComponentPropsWithRef<E, P>, 'ref'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment