From 515928d0ae52aa9fb767a6a51d31e7050d07ad73 Mon Sep 17 00:00:00 2001 From: Lena Morita Date: Fri, 26 Aug 2022 22:06:25 +0900 Subject: [PATCH] Simplify --- .../src/ui/context/wordpress-component.ts | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/packages/components/src/ui/context/wordpress-component.ts b/packages/components/src/ui/context/wordpress-component.ts index 09ec4331c85e4e..5c0c60978f88b1 100644 --- a/packages/components/src/ui/context/wordpress-component.ts +++ b/packages/components/src/ui/context/wordpress-component.ts @@ -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. */ @@ -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; /** @@ -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;