Skip to content

StatelessFunctionalComponenet that probably returns null cannot be instanciated in JSX #11955

@alisabzevari

Description

@alisabzevari

TypeScript Version: 2.1.0-dev.20161019

Code

import * as React from 'react';
import { render } from 'react-dom';

interface FooProps extends React.Props<null> {
  prop1?: boolean;
}

const Foo = ({ prop1 }: FooProps) => {
  if (prop1) {
    return null;
  }
  return (
    <div>Hello!</div>
  );
}

render(<Foo />, null);

Expected behavior:

This is a valid JSX code. It is true that the return value of the SFC can be null but there is no way to check for its value in the code!

Actual behavior:

Compiler complains with these errors:

error TS2605: JSX element type 'Element | null' is not a constructor function for JSX elements.
  Type 'null' is not assignable to type 'ElementClass'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions