|
1 |
| -export type AsProp<TElement extends React.ElementType> = { as?: TElement }; |
| 1 | +import type { Prettify } from "@zayne-labs/toolkit-type-helpers"; |
2 | 2 |
|
3 |
| -// == Return the prop object if it already contains the "as" prop, else merge it with the "as" prop |
4 |
| -type PropsWithOptionalAs<TElement extends React.ElementType, TProps> = "as" extends keyof TProps |
5 |
| - ? TProps |
6 |
| - : AsProp<TElement> & TProps; |
| 3 | +export type AsProp<TElement extends React.ElementType> = { as?: TElement }; |
7 | 4 |
|
8 |
| -// == Get all other primitive element props by Omitting the result of MergedProps from React.ComponentPropsWithRef |
9 |
| -type InferOtherProps<TElement extends React.ElementType, TProps> = Omit< |
| 5 | +// == Get the rest of the primitive props by omitting the result of TProps from the ones gotten from React.ComponentPropsWithRef |
| 6 | +type InferRestOfProps<TElement extends React.ElementType, TProps> = Omit< |
10 | 7 | React.ComponentPropsWithRef<TElement>,
|
11 |
| - keyof PropsWithOptionalAs<TElement, TProps> |
| 8 | + keyof TProps |
12 | 9 | >;
|
13 | 10 |
|
| 11 | +// prettier-ignore |
| 12 | +// == Merge the AsProp and the TProps, or omit the AsProp if the user passed their own |
| 13 | +type MergedPropsWithAs<TElement extends React.ElementType, TProps> = Omit<AsProp<TElement>, keyof TProps> & TProps; |
| 14 | + |
14 | 15 | // == Polymorphic props helper
|
15 | 16 | export type PolymorphicProps<
|
16 | 17 | TElement extends React.ElementType,
|
17 |
| - // eslint-disable-next-line ts-eslint/no-explicit-any -- Any is need so one can pass any prop without type errors |
18 |
| - TProps extends Record<keyof any, any> = AsProp<TElement>, |
19 |
| -> = InferOtherProps<TElement, TProps> & PropsWithOptionalAs<TElement, TProps>; |
| 18 | + // eslint-disable-next-line ts-eslint/no-explicit-any -- Any is needed so one can pass any prop type without type errors |
| 19 | + TProps extends Record<keyof any, any>, |
| 20 | +> = InferRestOfProps<TElement, TProps> & Prettify<MergedPropsWithAs<TElement, TProps>>; |
0 commit comments