Skip to content

TypeScript doesn't correctly infer union types as React/JSX properties #20244

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
junseld opened this issue Nov 23, 2017 · 1 comment
Closed
Labels
Duplicate An existing issue was already created

Comments

@junseld
Copy link

junseld commented Nov 23, 2017

Hey,

typescript version: 2.5.3

My problem is really close to this issue's, although the main problem of it was fixed:
#10171
so I take this issue's example:

type TextProps = { editable: false }
               | { editable: true, onEdit: (newText: string) => void }

class TextComponent extends React.Component<TextProps, {}> {
    render() {
        return <span>Some Text..</span>;
    }
}

I can't use the props in JSX, while it works with directly using React.createElement:

const textProps: TextProps = {
    editable: true as true,  // Whoops, why not literal type?
    onEdit: () => {}
} as any;

<TextComponent {...textProps} /> // fails
React.createElement(Button, textProps) // works

// or
function id(t: TextProps): TextProps { return t; }
const textProps2: TextProps = id(textProps);

<TextComponent {...textProps2} />, // fails
React.createElement(Button, textProps2) // works

Any clue why or how to fix/workaround that?

Thanks

@mhegazy
Copy link
Contributor

mhegazy commented Nov 27, 2017

Duplicate of #18670

@mhegazy mhegazy marked this as a duplicate of #18670 Nov 27, 2017
@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 27, 2017
@junseld junseld closed this as completed Nov 28, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants