From 1fbaf6fdf042aa699d93b46d395de11bab5a96a9 Mon Sep 17 00:00:00 2001 From: Wahyu Kurniawan Date: Wed, 28 Feb 2024 08:31:07 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20feat:=20add=20polymorphic?= =?UTF-8?q?=20prop=20type=20for=20heading=20component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/frontend/src/types/common.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/frontend/src/types/common.ts b/packages/frontend/src/types/common.ts index fc796289..dd0fc09f 100644 --- a/packages/frontend/src/types/common.ts +++ b/packages/frontend/src/types/common.ts @@ -1,3 +1,9 @@ +import { + type ElementType, + type ComponentPropsWithoutRef, + forwardRef, +} from 'react'; + /** * Construct a type by excluding common keys from one type to another. * @template T - The type from which to omit properties. @@ -7,3 +13,15 @@ * @returns A new type that includes all properties from T except those that are common with U. */ export type OmitCommon = Pick>; + +export type PolymorphicProps = Props & + Omit, 'as'> & { + as?: Element; + }; + +// taken from : https://github.com/total-typescript/react-typescript-tutorial/blob/main/src/08-advanced-patterns/72-as-prop-with-forward-ref.solution.tsx +type FixedForwardRef = ( + render: (props: P, ref: React.Ref) => React.ReactNode, +) => (props: P & React.RefAttributes) => JSX.Element; + +export const fixedForwardRef = forwardRef as FixedForwardRef;