Skip to content

JSX does not accept functional components that can return null types #14570

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
apexskier opened this issue Mar 10, 2017 · 2 comments
Closed

JSX does not accept functional components that can return null types #14570

apexskier opened this issue Mar 10, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@apexskier
Copy link

TypeScript Version: 2.2.1

Code

import * as React from "react";

interface TestStatelessProps {
    output: boolean;
}

function TestStateless(props: TestStatelessProps): JSX.Element | null {
    if (props.output) {
        return <div>Hello World</div>;
    }
    return null;
}

const test = (
    <div>
        1: {null}
        2: <TestStateless output />
        3: <TestStateless output={false} />
    </div>
);
console.log(test);

Expected behavior:
Compiles. test has type JSX.Element. If test were rendered it would show

<div>1:2:<div>Hello World</div>3:</div>

Null wouldn't render anything, the first TestStateless would render it's content, the second wouldn't render anything.

Actual behavior:
If I try to use <TestStateless />, I get the following errors.

[ts] JSX element type 'Element | null' is not a constructor function for JSX elements.
  Type 'null' is not assignable to type 'ElementClass'.
[ts] JSX element class does not support attributes because it does not have a 'props' property

The {null} part of the JSX works correctly.
The same issue happens if the functional component returns undefined

@monolithed
Copy link

monolithed commented Mar 11, 2017

The same problem:

let X: React.StatelessComponent<{}> = () => null;

@RyanCavanaugh
Copy link
Member

Duplicate #11566

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 13, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

3 participants