Skip to content

Adding a jsx comment causes TypeScript to identify children type as any[], even though generated javascript doesn't change #41389

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
lxe opened this issue Nov 3, 2020 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@lxe
Copy link

lxe commented Nov 3, 2020

TypeScript Version: 3.3.3 - 4.0.5

Search Terms:

Typescript jsx comment any[] children

Code

Playground

import * as React from 'react';

declare var SomeComponent: React.FC<{
    children?: React.ReactElement
}>;

function Good () {
    // Yields React.createElement(SomeComponent, null, React.createElement("div", null)));
    // This satisfies TypeScript
    return <SomeComponent>
        <div />
    </SomeComponent>
}

function Bad () {
    // Still yields React.createElement(SomeComponent, null, React.createElement("div", null)));
    // This does NOT satisfy TypeScript type checking:
    // Type '{ children: any[]; }' is not assignable to type '{ children?: ReactElement<...'.
    return <SomeComponent>
        {/* Comment */}
        <div />
    </SomeComponent>
}

Expected behavior:

The case without a JSX comment and the case with a comment both yield the same JavaScript (e.g. the comment does not count as an extra child). TypeScript should understand that the type of <div /> matches the expected signature React.ReactElement

Actual behavior:

However, when the JSX comment is present, even though the resulting JS is the same, TypeScript now assumes the type to be any[] which no longer satisfies React.ReactElement.

Playground Link: Link

Related Issues:

@MartinJohns
Copy link
Contributor

Very likely a duplicate of #41125.

@lxe
Copy link
Author

lxe commented Nov 4, 2020

Thanks @MartinJohns that's gotta be it! No issues in nightly! Playground.

Does this mean this will be mainlined in 4.1 or do we have to wait until February 2021 for this to be available in 4.2?

Feel free to tag and close.

@RyanCavanaugh
Copy link
Member

Things merged prior to last week will be in 4.1. Thanks!

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 12, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants