Skip to content

Incorrect work with Unions Types #1

Description

@SiarheiLazakovich

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions