-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
React typings allow false for class components, but not for stateless #18912
Comments
Note: React 16 will allow arrays and strings and arrays of strings also. So at one point it would be similar to Also, if unifying with |
I mentioned it here #17021 (comment) but forgot to follow or PR. |
@vbox Using a type for this is a good idea. Please also add a test for any new elements in this return type (like |
Hey, is anyone working on this? If not I could help finish this if needed :) |
Currently, the class render(): ReactNode; Shouldn't (props: P & { children?: ReactNode }, context?: any): ReactElement<any> | null; to: (props: P & { children?: ReactNode }, context?: any): ReactNode; as well? As far as I understand both return types should be identical. Current types make it hard to refactor a class component as a function one as then suddently the return types change. |
@Kovensky did this PR AFAIR , any reasons why FC returns
|
That is because the compiler is still dumb in this respect and thinks function components have to return function Test() { return null }
function Test2() { return false }
const jsx = (
<>
<Test />
<Test2
// type error
// "JSX element type 'boolean' is not a constructor function for JSX elements."
// (looks like the error message the bug causes is itself buggy???)
/>
</>
) This is unrelated to the type of This also extends to returning (Note that The other problem I wanted to fix is that class components' To fix the root cause of that problem I need that big 600+ files PR that takes too long to build for Travis (50min and it's not even 20% done). I'm just leaning more and more towards making a hard breaking change in the React types, rewriting and throwing everything legacy away. I wonder if |
Hey, any news on this? |
This can be fixed once microsoft/TypeScript#21699 is resolved. |
This seems to be the reason for why Right now this is an error: const test: React.FunctionComponent<{}> = props => 'test'; I would like to propose that |
Almost opened another duplicate... |
What can we do to move forward with this? |
It's 2021 and this is still an open issue, is there anything we can do to help move this forward? |
This is still the latest state of this issue. We can't do anything unless TypeScript folks move forward with microsoft/TypeScript#21699 |
Duplicate of #18051 |
Can anyone explain to my why this is necessarily blocked by the mentioned Typescript issue when this is replicable without using JSX? This, for example, creates a type-error, because const ComponentWhichReturnsOne = () => 1;
React.createElement(ComponentWhichReturnsOne); |
@ryami333 By allowing it in |
Right, so it's impossible in JSX because of upstream constraints, but it's only #wontfix in vanilla because equality+solidarity? |
This was just the first argument that come to mind. We already have inconsistencies between JSX and no-JSX so it may not be as relevant. Feel free to go through the typings for |
Commenting to point out that I've been running into this with my team somewhat often. It'd be great to get the type corrected at some point, the common use case of returning return condition && <div>My markup</div> The current types force this code to be rewritten as a ternary to return |
This is still blocked by microsoft/TypeScript#21699. I recommend subscribing to microsoft/TypeScript#21699 instead of asking for updates. |
I modified @types/react/index.d.ts locally replacing
with
inside of the The following code type checks for me:
Is this a bad solution? If so, why? I don't see |
|
Now that microsoft/TypeScript#21699 is fixed, should this issue and #60242 be fixed as well? |
Still need to wait for a stable release but I prepare support for adding pre 5.1. and 5.1 support in |
@types/xxxx
package and had problems.Definitions by:
inindex.d.ts
) so they can respond.As mentioned in React documentation, a stateless component can return
false
for empty content. Currently StatelessComponent is defined as:whereas Component has
Shouldn't these types be the same (including eliminating the ReactElement vs JSX.Element difference)? And to avoid future such issues (#14064, this one) it should probably be a named type, e.g. RenderResult.
A search for
| null
also comes up withComponentSpec.render
which probably should also have the same result type.The text was updated successfully, but these errors were encountered: