Skip to content

JSX Children Tuple with single element #30918

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

Closed
plusgut opened this issue Apr 13, 2019 · 4 comments
Closed

JSX Children Tuple with single element #30918

plusgut opened this issue Apr 13, 2019 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@plusgut
Copy link

plusgut commented Apr 13, 2019

Search Terms:
When A single element is given as a Child, it is not handled as an Array.

Code

const Component = (props: { children: [ JSX.Element ] }) => props.children;

const test = <Component><element /></Component>; // Should not throw an error

Expected behavior:
There should not be a completely different behaviour from TypeScript when a single Child is present in a Tuple.

Actual behavior:
The following error is thrown: This JSX tag's 'children' prop expects type '[Element]' which requires multiple children, but only a single child was provided

I am aware that React behaves that way, but the JSX-Language states that everything between JSXOpeningElement and JSXSelfClosingElement are JSXChildren. https://github.com/facebook/jsx

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Apr 15, 2019
@RyanCavanaugh
Copy link
Member

{ children: [ JSX.Element ] } is basically an impossible contract to satisfy.

I don't understand what you expect to happen here. Your function says that this code is legal:

const Component = (props: { children: [ JSX.Element ] }) => props.children.slice(0);

const test = <Component><element /></Component>; // Should not throw an error

But that code is going to throw an error at runtime. Why should something that manifestly defies the specified types pass the type checking?

@weswigham
Copy link
Member

weswigham commented Apr 15, 2019

FYI: Impossible in react (for now - children is apparently supposed to be opaque and subject to change), but fully possible (probable, even) in other JSX frameworks. This is a place where we have some unfortunate react-unique logic baked in, since, AFAIK, at the time we didn't even have tuple types but still wanted to allow validation against singular children?

@plusgut
Copy link
Author

plusgut commented Apr 16, 2019

@RyanCavanaugh
It would throw a runtime error in react, but not necessarily in other frameworks.
And especially when we look at the jsx specifications itself, React is doing it wrong.

@weswigham Exactly! Don't you agree that there should be the option, to opt-out of these react-specific things?

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants