Skip to content
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

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

Closed
alisabzevari opened this issue Oct 31, 2016 · 8 comments
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created

Comments

@alisabzevari
Copy link
Contributor

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'.
@alitaheri
Copy link

This is annoying. I worked abound it by asserting that null is not null: 😱

const Foo = ({ prop1 }: FooProps) => {
  if (prop1) {
    return null!; // <= never is assignable to anything, so the return type will be Element
  }
  return (
    <div>Hello!</div>
  );
}

@alisabzevari
Copy link
Contributor Author

@alitaheri Good idea. I just decided to remove strictNullChecks. But I will go with your workaround to fix the problem.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Oct 31, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.1 milestone Oct 31, 2016
@RyanCavanaugh
Copy link
Member

@yuit we should allow null as a return type for JSX functions

@RyanCavanaugh
Copy link
Member

@yuit as well as null constructor functions

@mhegazy mhegazy modified the milestones: TypeScript 2.1.2, TypeScript 2.1, Future Nov 7, 2016
@tamlyn
Copy link

tamlyn commented Dec 16, 2016

Can someone explain what is happening in @alitaheri's workaround? I've never seen the null! syntax before and I can't find any documentation on it.

@alitaheri
Copy link

@tamlyn This is where you can find information on it.

@yuit
Copy link
Contributor

yuit commented Feb 16, 2017

This is the same as #11566

@yuit yuit closed this as completed Feb 16, 2017
@yuit yuit added the Duplicate An existing issue was already created label Feb 16, 2017
@tkrotoff
Copy link

This is not fixed yet.
React definition from DefinitelyTyped still needs to be updated since the original PR was reverted due to regressions with TypeScript < 2.3

@mhegazy mhegazy removed this from the Future milestone Apr 26, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

7 participants