kripod / react-polymorphic-types

Zero-runtime polymorphic component definitions for React
MIT License
195 stars 8 forks source link

Incorrect work with Unions Types #1

Open SiarheiLazakovich opened 3 years ago

SiarheiLazakovich commented 3 years ago

Description

Polymorphic component has partialized props if "as" component has unioned props.

Reproduction

import * as React from 'react';
import type {
  PolymorphicForwardRefExoticComponent,
  PolymorphicPropsWithoutRef,
  PolymorphicPropsWithRef,
} from 'react-polymorphic-types';

export const HeadingDefaultElement = 'h2';

export type HeadingOwnProps = {
  color?: string;
};

export type HeadingProps<
  T extends React.ElementType = typeof HeadingDefaultElement
> = PolymorphicPropsWithRef<HeadingOwnProps, T>;

export const Heading: PolymorphicForwardRefExoticComponent<
  HeadingOwnProps,
  typeof HeadingDefaultElement
> = React.forwardRef(function Heading<
  T extends React.ElementType = typeof HeadingDefaultElement
>(
  {
    as,
    color,
    style,
    ...restProps
  }: PolymorphicPropsWithoutRef<HeadingOwnProps, T>,
  ref: React.ForwardedRef<React.ElementRef<T>>
) {
  const Element: React.ElementType = as || HeadingDefaultElement;
  return <Element ref={ref} style={{ color, ...style }} {...restProps} />;
});

interface ITestA {
  value: 'a';
}

interface ITestB {
  value: 'b';
  extra: string;
}

type Test = ITestA | ITestB;

const Test: React.FC<Test> = (props) => {
  return <></>;
};

function App() {
  return (
    <>
      <Test value="b" extra="extra" />
      <Heading as={Test} value="b" extra="extra" />
    </>
  );
}

Expected behavior

Polymorphic component has correct props types.

Actual behavior

Type '{ as: FC<Test>; value: "b"; extra: string; }' is not assignable to type 'IntrinsicAttributes & Pick<PropsWithChildren<Test>, "children" | "value"> & HeadingOwnProps & { ...; }'. Property 'extra' does not exist on type 'IntrinsicAttributes & Pick<PropsWithChildren<Test>, "children" | "value"> & HeadingOwnProps & { ...; }'.ts(2322)

kripod commented 3 years ago

Thank you for your report!

I’ve just answered a similar issue of yours here. Please continue discussing over there, as my proposed solution may resolve the problem.