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 #23

Open
SiarheiLazakovich opened this issue Jan 5, 2021 · 2 comments
Open

Incorrect work with Unions Types #23

SiarheiLazakovich opened this issue Jan 5, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@SiarheiLazakovich
Copy link

Description

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

Reproduction

import React from 'react';
import { Box } from 'react-polymorphic-box';

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" />
      <Box as={Test} value="b" extra="extra" />
    </>
  );
}

Expected behavior

Polymorphic box has correct props types.

Actual behavior

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

@SiarheiLazakovich SiarheiLazakovich added the bug Something isn't working label Jan 5, 2021
@kripod
Copy link
Owner

kripod commented Jan 5, 2021

Thank you for your report!

Please try specifying the union as follows and get back to me whether it works or not:

interface ITestA {
  value: 'a';
  extra?: never; // Inspired by https://github.com/vercel/next.js/blob/51f2a530d623f4b50e71ce849e4276d2fea28211/packages/next/client/image.tsx#L63-L74
}

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

type Test = ITestA | ITestB;

@SiarheiLazakovich
Copy link
Author

SiarheiLazakovich commented Jan 6, 2021

Thank you for your fast response)

this workaround is not perfect:

import React from 'react';
import { Box } from 'react-polymorphic-box';

interface ITestA {
  value: 'a';
  extra?: string;
}

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

type Test = ITestA | ITestB;

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

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

I expect that "Box" has the same error.

error:

Type '{ value: "b"; }' is not assignable to type '(IntrinsicAttributes & ITestA & { children?: ReactNode; }) | (IntrinsicAttributes & ITestB & { children?: ReactNode; })'. Property 'extra' is missing in type '{ value: "b"; }' but required in type 'ITestB'.ts(2322)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants