️ feat: add polymorphic prop type for heading component

This commit is contained in:
Wahyu Kurniawan 2024-02-28 08:31:07 +07:00
parent 66277d6c3b
commit 1fbaf6fdf0
No known key found for this signature in database
GPG Key ID: 040A1549143A8E33

View File

@ -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<T, U> = Pick<T, Exclude<keyof T, keyof U>>;
export type PolymorphicProps<Element extends ElementType, Props> = Props &
Omit<ComponentPropsWithoutRef<Element>, '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 = <T, P = object>(
render: (props: P, ref: React.Ref<T>) => React.ReactNode,
) => (props: P & React.RefAttributes<T>) => JSX.Element;
export const fixedForwardRef = forwardRef as FixedForwardRef;