Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect work with Unions Types #1

Open
SiarheiLazakovich opened this issue Jan 5, 2021 · 1 comment
Open

Incorrect work with Unions Types #1

SiarheiLazakovich opened this issue Jan 5, 2021 · 1 comment

Comments

@SiarheiLazakovich
Copy link

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
Copy link
Owner

kripod commented Jan 5, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants