Skip to content

Conversation

@jstcki
Copy link
Contributor

@jstcki jstcki commented Apr 26, 2018

Fixes #18051

The tests in tsx.tsx fail for some reason I can't explain:

JSX element type 'ReactNode' is not a constructor function for JSX elements.
  Type 'undefined' is not assignable to type 'ElementClass'.

ReactNode is definitely the correct return type for SFCs though (see linked Flow type definition). I've confirmed this in a real application. Also, the return type of Component.render() already is ReactNode – SFCs aren't different in this regard.

@jstcki jstcki requested a review from johnnyreilly as a code owner April 26, 2018 21:27
@jstcki jstcki changed the title Change React StatelessComponent return type to ReactNode [react] Change React StatelessComponent return type to ReactNode Apr 26, 2018
@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). The Travis CI build failed labels Apr 26, 2018
@typescript-bot
Copy link
Contributor

typescript-bot commented Apr 26, 2018

@herrstucki Thank you for submitting this PR!

🔔 @johnnyreilly @bbenezech @pzavolinsky @digiguru @ericanderson @morcerf @tkrotoff @DovydasNavickas @onigoetz @theruther4d @guilhermehubner @JoshuaKGoldberg @jrakotoharisoa - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@typescript-bot
Copy link
Contributor

typescript-bot commented Apr 26, 2018

@herrstucki The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

@ferdaber
Copy link
Contributor

ferdaber commented May 2, 2018

The tests are failing because of how Typescript resolves JSX element constructors. When you changed SFC to return ReactNode, it made Typescript actually fail to resolve the type SFC to an SFC-type value-based component. To make it work you would need to change JSX.Element to be of type ReactNode instead of an interface extending ReactElement<any>

I'm not sure why it worked in the first place based on my understanding of the resolution rules for JSX constructors, because the current return type of SFC is ReactElement<any> | null and it is not assignable to ReactElement<any> but somehow it works.

Maybe one of the TS devs can help out or someone who knows more about the JSX rules in TS 😄

@typescript-bot
Copy link
Contributor

@herrstucki I haven't seen anything from you in a while and this PR currently has problems that prevent it from being merged. The PR will be closed tomorrow if there aren't new commits to fix the issues.

@jstcki
Copy link
Contributor Author

jstcki commented May 3, 2018

@ferdaber Thanks for the hint. When JSX.Element is React.ReactNode it works fine (see b36083e)! Although I'm not 100% sure if this is semantically correct 🤔

@typescript-bot
Copy link
Contributor

typescript-bot commented May 3, 2018

@herrstucki The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

@jstcki
Copy link
Contributor Author

jstcki commented May 3, 2018

Well, at least the react typings lint fine now but a lot of other packages seem to be broken by this 😥

@ferdaber
Copy link
Contributor

ferdaber commented May 3, 2018

Yes, the change to JSX.Element to ReactNode is a breaking change because before it used to provide access to key, props and type properties of the element itself for JSX expressions. The problem here is that JSX types are a black box, until React resolves what the element actually renders, it's just a plain object, so even though you (the implementer) knows that a certain component returns an array or null or whatever, what actually is being returned is a plain object until React resolves it.

I also can't think of a way this would work with the current JSX constructor rules that Typescript imposes... hmmmmmm 🤔

@ferdaber
Copy link
Contributor

ferdaber commented May 3, 2018

Ah, I didn't realize there was already an issue opened in the Typescript repo for this, see microsoft/TypeScript#21699

I don't think that until that is resolved you'd be able to change the return type to ReactNode safely, sorry.

@jstcki
Copy link
Contributor Author

jstcki commented May 3, 2018

Alright, I’ll close this then. Thanks for your feedback @ferdaber!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Popular package This PR affects a popular package (as counted by NPM download counts).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants