-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
allow returning an array of jsx elements #20239
allow returning an array of jsx elements #20239
Conversation
So if I see correctly I should now create the following files:
Are the |
You should write only the PS: If you haven't already, you can read on the needed commands in the Contributing How-To. |
Thank you @gcnew. I also linked my changes (also applied to the |
|
||
import React = require('react'); | ||
|
||
const Foo = (props: any) => [<span />, <span />]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would warn at runtime due to missing key
on the elements, but does it matter? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, this would warn at runtime. I guess it is fine for the test here as it only checks compiling...?
Hi @Kovensky, thank you for the review. Anything I can do to get this merged? :) Thanks! |
Can you clarify a bit on why it's not sufficient to update the react.d.ts file? |
I think it is a similar problem to this one: #14152 |
@RyanCavanaugh and @weswigham what is the final call on this change? |
IMO, we could do more here since, for example, all elements returned in the array must have the TL:DR: This looks good (pending update with master and ideally a test with generic components), but I'm not happy about it, because it has the possibility of quietly reducing safety for people on older react versions (and strongly couples us to react 16+ in general). We should fix that. |
Also note, you can render strings directly now, too: render() {
return 'Look ma, no spans!';
} This should be able to support that. (Also numbers, booleans, and portals) |
I agree, I'd prefer to not include so much React-specific and React-version-specific information directly in the compiler as well. It'd be great if this PR could just have been a PR to DefinitelyTyped instead. For now I'll try to
I'd keep all other points open for future PRs. If someone helps me finding the correct strategy I could try to also remove the React-specific parts in the compiler and put them to a different place...? |
Maybe TypeScript can support a new sort of primitive/plugin which enables extracting the React-JSX-logic into a standalone package. E.g. the current TypeScript Language Service Plugins don't allow adding new custom syntax to TypeScript, but maybe we can have "TypeScript Language Syntax Plugins" or "TypeScript Language Macros" or whatever in the future where this would be possible...? |
6f5f3c5
to
5ae54a5
Compare
Hmmmm... So we talked about this a bit today in person, and we'd like to take this opportunity to remove this coupling with react, so nobody needs to go in and update us when react changes (and to avoid breaking older react versions). The correct type to check here should be the return type of each of the signatures if the first argument type of the factory function, for all factory function signatures whose first argument type has call or construct signatures. |
*the return type for the call signatures, the return type of the render method of the return type of construct signatures. |
Could you point me to the places which would be needed to change? And maybe some pseudo code for an overview which needs to be done? That would be really nice as this was my first PR to TypeScript ever and I'm not very familiar with the code base. Then I'd try a new PR. So what we'll do is basically is extracting the return types so they can be configured externally, while the while JSX parsing logic stays in TypeScript, right? |
I have recently run into this issue when I tried returning a string from SFC (stateless functional component). There is a PR pending in DefinitelyTyped repo, but it's failing there and apparently, it's nothing we can do about it. I can just add my two cents that extracting React specific logic out of the Typescript seems reasonable way, but I have no clue how that could be approached. Typescript does not have any sort of plugins or extensions, or does it? |
@donaldpipowitch sorry this has taken a bit for me to writeup, but it does make the change a fair bit more involved, compared to what's here 🙁 First, inside Ideally we'd then do a similar thing for There's a long trail of ways to improve (and associated issues), so I think I'll refrain from suggesting anything more here. 🐱 |
Really thank you for this summary. I'll see when I can find time to fiddle around with this. Should this PR be closed for now or merged as it is? Just so everybody knows where we currently are. |
@donaldpipowitch I've opened an issue to track the problem - you can close this PR. If you aren't able to get to this in the next week or so, we'll probably take it on internally, since we do really want to see this fixed by 2.8, now that we know about the problem. |
@weswigham Ah, I see. I don't think I can fix this within a week. I have a couple of private events and less spare time currently. Feel free to start with it now. |
Hi everyone! First time contributor here 👋 At least in terms of the compiler.
It is currently not possible to return an array of JSX Elements inside of a stateless React component. The change can't be applied to the typings directly, because the constructor is affected. See here for an open issue DefinitelyTyped/DefinitelyTyped#20356 and this comment DefinitelyTyped/DefinitelyTyped#19363 (comment).
I basically want to be able to change
(props: P & { children?: ReactNode }, context?: any): ReactElement<any> | null;
of the stateless component to(props: P & { children?: ReactNode }, context?: any): ReactElement<any>[] | ReactElement<any>| null;
.I looked for older similar issues and found this one: #14152. (Surprise. This is actually a fix for an older issue I reported, because I couldn't return
null
. 😄 #11566) (Note that it's hard to initially find the correct code via GitHubs search, because thechecker.ts
is too big. It looks like GitHub doesn't allow searching in big files. I usually start by finding and reading the code on GitHub, before I clone the repo to get some initial impression if it is fixable for me.)So here I am. This is an ongoing PR. If you have any feedback, I'd be happy. Is this even the right place to make the change?
I'll start looking into tests as the next step.