Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  94] [ 6]  / answers: 1 / hits: 127437  / 7 Years ago, sat, february 4, 2017, 12:00:00

This code:


import * as React from 'react';                                                                                              

const Component = () => <div/>;

function culprit<P>(node: React.ReactElement<P>) {
console.log(node);
}

culprit(<Component/>);

...produces this compilation error when compiling with TypeScript:



error TS2345: Argument of type 'Element' is not assignable to
parameter of type 'ReactElement'. Type 'null' is not assignable
to type 'ReactElement



This only happens when the strictNullChecks TypeScript compilation flag is set to true.


Yes I could disable that flag, but I want it on for added compile-time checking/safety.


If I change the last line to this:


culprit( (<Component/> as React.ReactElement<any>) );

...it works with the flag set to true.


I've recently tried migrating from "plain" JavaScript to TypeScript in a React project and this is tripping up all my tests, so adding all the TypeScript type assertions to all these occurrences in the test code will be a pain.


Is this a bug, or do I have no other choice?


More From » reactjs

 Answers
47

This is apparently an issue which was introduced in the 15.0.5 React type definitions. If you change to 15.0.4, everything should be fine.



See this issue: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/14284


[#59073] Thursday, February 2, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kelsy

Total Points: 486
Total Questions: 86
Total Answers: 76

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
;