Skip to content

JSX spread doesn't work with union props #18670

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

Closed
pelotom opened this issue Sep 21, 2017 · 1 comment · Fixed by #19047
Closed

JSX spread doesn't work with union props #18670

pelotom opened this issue Sep 21, 2017 · 1 comment · Fixed by #19047
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@pelotom
Copy link

pelotom commented Sep 21, 2017

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

It's often useful to define a React component's properties as a union type, e.g.

type InfoProps =
  | { status: 'hidden' }
  | { status: 'visible'; content: string }

const Info = (props: InfoProps) =>
  props.status === 'hidden'
    ? <noscript />
    : <div>{props.content}</div>

When constructing elements from this component, it works fine in concrete cases:

<Info status="hidden" />
<Info status="visible" content="hello world" />

however if you try to use "spread" syntax, it's a type error:

declare const infoProps: InfoProps

<Info {...infoProps} />
/* .  ^^^^^^^^^^^^^^
Type '{ status: "hidden" | "visible"; }' is not assignable to type '(IntrinsicAttributes & { status: "hidden"; }) | (IntrinsicAttributes & { status: "visible"; conte...'.
  Type '{ status: "hidden" | "visible"; }' is not assignable to type 'IntrinsicAttributes & { status: "visible"; content: string; }'.
    Type '{ status: "hidden" | "visible"; }' is not assignable to type '{ status: "visible"; content: string; }'.
      Types of property 'status' are incompatible.
        Type '"hidden" | "visible"' is not assignable to type '"visible"'.
          Type '"hidden"' is not assignable to type '"visible"'.
*/

As I understand it, this should be no different semantically from

React.createElement(Info, infoProps)

which the compiler approves of.

@mhegazy mhegazy added the Bug A bug in TypeScript label Sep 21, 2017
@OliverJAsh
Copy link
Contributor

Just hit on exactly the same problem, reproducible in 2.4.1 and 2.5.2. Another example:

import React, { SFC } from 'react';

{
    type CommonProps = { common: string };
    type Props = CommonProps &
        ({ type: 'foo' } | { type: 'bar'; message: string });

    const Button: SFC<Props> = (props) => null;

    const OtherButton1: SFC<Props> = (props) => React.createElement(Button, props);

    // Type '"foo"' is not assignable to type '"bar"'.
    const OtherButton2: SFC<Props> = (props) => <Button {...props} />;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
4 participants