Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Aug 26, 2022
1 parent 73a5c38 commit 515928d
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions packages/components/src/ui/context/wordpress-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ export type WordPressComponentProps<
/** The HTML element to inherit props from. */
T extends React.ElementType,
/** Supports polymorphism through the `as` prop. */
IsPolymorphic extends boolean = true,
/** Supports ref forwarding. */
ForwardsRef extends boolean = true
IsPolymorphic extends boolean = true
> = P &
// The `children` prop is being explicitly omitted since it is otherwise implicitly added
// by `ComponentPropsWithRef`. The context is that components should require the `children`
// prop explicitely when needed (see https://github.com/WordPress/gutenberg/pull/31817).
Omit<
ForwardsRef extends true
? React.ComponentPropsWithRef< T >
: React.ComponentPropsWithoutRef< T >,
'as' | keyof P | 'children'
> &
Omit< React.ComponentPropsWithoutRef< T >, 'as' | keyof P | 'children' > &
( IsPolymorphic extends true
? {
/** The HTML element or React component to render the component as. */
Expand All @@ -33,15 +26,14 @@ export type WordPressComponentProps<
export type WordPressComponent<
T extends React.ElementType,
O,
IsPolymorphic extends boolean = true,
ForwardsRef extends boolean = true
IsPolymorphic extends boolean
> = {
< TT extends React.ElementType >(
props: WordPressComponentProps< O, TT, IsPolymorphic, ForwardsRef > &
props: WordPressComponentProps< O, TT, IsPolymorphic > &
( IsPolymorphic extends true ? { as: TT } : {} )
): JSX.Element | null;
(
props: WordPressComponentProps< O, T, IsPolymorphic, ForwardsRef >
props: WordPressComponentProps< O, T, IsPolymorphic >
): JSX.Element | null;
displayName?: string;
/**
Expand All @@ -55,7 +47,13 @@ export type WordPressComponent<
selector: `.${ string }`;
};

export type WordPressComponentFromProps< Props > =
Props extends WordPressComponentProps< infer P, infer T, infer I, infer F >
? WordPressComponent< T, P, I, F >
: never;
export type WordPressComponentFromProps<
Props,
ForwardsRef extends boolean = true
> = Props extends WordPressComponentProps< infer P, infer T, infer I >
? WordPressComponent<
T,
P & ( ForwardsRef extends true ? React.RefAttributes< any > : {} ),
I
>
: never;

0 comments on commit 515928d

Please sign in to comment.