Skip to content

Commit

Permalink
Update create-polymorphic-component.ts
Browse files Browse the repository at this point in the history
Prevents TypeScript from showing the error: "Expression produces a union type that is too complex to represent.", when props are extended from interfaces such as StackProps and provided as explicit type to createPolymorphicComponent.
  • Loading branch information
DonnyVerduijn authored Oct 11, 2024
1 parent e0721af commit 97e4f68
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ export type PolymorphicRef<C> = C extends React.ElementType
? React.ComponentPropsWithRef<C>['ref']
: never;

export type PolymorphicComponentProps<C, Props = {}> = C extends React.ElementType
? InheritedProps<C, Props & ComponentProp<C>> & {
export type PolymorphicComponentProps<C, Props = {}> = C extends React.ElementType
? InheritedProps<C, Props & ComponentProp<C>> & {
ref?: PolymorphicRef<C>;
renderRoot?: (props: any) => any;
}
: Props & { component: React.ElementType; renderRoot?: (props: Record<string, any>) => any };
: Props extends Record<string, unknown>
? Props & FallbackProps
: FallbackProps;

type FallbackProps = {
component: React.ElementType;
renderRoot?: (props: Record<string, any>) => any;
};

export function createPolymorphicComponent<
ComponentDefaultType,
Expand Down

0 comments on commit 97e4f68

Please sign in to comment.