Skip to content
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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15208,7 +15208,7 @@ namespace ts {
if (!deferredJsxStatelessElementType) {
const jsxElementType = getJsxGlobalElementType();
if (jsxElementType) {
deferredJsxStatelessElementType = getUnionType([jsxElementType, nullType]);
deferredJsxStatelessElementType = getUnionType([createArrayType(jsxElementType), jsxElementType, nullType]);
}
}
return deferredJsxStatelessElementType;
Expand Down
24 changes: 24 additions & 0 deletions tests/baselines/reference/tsxSfcReturnArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [file.tsx]
import React = require('react');

const Foo = (props: any) => [<span />, <span />];

function Greet(x: { name?: string }) {
return [<span />, <span />];
}

const foo = <Foo />;
const G = <Greet />;


//// [file.jsx]
define(["require", "exports", "react"], function (require, exports, React) {
"use strict";
exports.__esModule = true;
var Foo = function (props) { return [<span />, <span />]; };
function Greet(x) {
return [<span />, <span />];
}
var foo = <Foo />;
var G = <Greet />;
});
28 changes: 28 additions & 0 deletions tests/baselines/reference/tsxSfcReturnArray.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/conformance/jsx/file.tsx ===
import React = require('react');
>React : Symbol(React, Decl(file.tsx, 0, 0))

const Foo = (props: any) => [<span />, <span />];
>Foo : Symbol(Foo, Decl(file.tsx, 2, 5))
>props : Symbol(props, Decl(file.tsx, 2, 13))
>span : Symbol(JSX.IntrinsicElements.span, Decl(react.d.ts, 2461, 51))
>span : Symbol(JSX.IntrinsicElements.span, Decl(react.d.ts, 2461, 51))

function Greet(x: { name?: string }) {
>Greet : Symbol(Greet, Decl(file.tsx, 2, 49))
>x : Symbol(x, Decl(file.tsx, 4, 15))
>name : Symbol(name, Decl(file.tsx, 4, 19))

return [<span />, <span />];
>span : Symbol(JSX.IntrinsicElements.span, Decl(react.d.ts, 2461, 51))
>span : Symbol(JSX.IntrinsicElements.span, Decl(react.d.ts, 2461, 51))
}

const foo = <Foo />;
>foo : Symbol(foo, Decl(file.tsx, 8, 5))
>Foo : Symbol(Foo, Decl(file.tsx, 2, 5))

const G = <Greet />;
>G : Symbol(G, Decl(file.tsx, 9, 5))
>Greet : Symbol(Greet, Decl(file.tsx, 2, 49))

37 changes: 37 additions & 0 deletions tests/baselines/reference/tsxSfcReturnArray.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/conformance/jsx/file.tsx ===
import React = require('react');
>React : typeof React

const Foo = (props: any) => [<span />, <span />];
>Foo : (props: any) => JSX.Element[]
>(props: any) => [<span />, <span />] : (props: any) => JSX.Element[]
>props : any
>[<span />, <span />] : JSX.Element[]
><span /> : JSX.Element
>span : any
><span /> : JSX.Element
>span : any

function Greet(x: { name?: string }) {
>Greet : (x: { name?: string; }) => JSX.Element[]
>x : { name?: string; }
>name : string

return [<span />, <span />];
>[<span />, <span />] : JSX.Element[]
><span /> : JSX.Element
>span : any
><span /> : JSX.Element
>span : any
}

const foo = <Foo />;
>foo : JSX.Element
><Foo /> : JSX.Element
>Foo : (props: any) => JSX.Element[]

const G = <Greet />;
>G : JSX.Element
><Greet /> : JSX.Element
>Greet : (x: { name?: string; }) => JSX.Element[]

17 changes: 17 additions & 0 deletions tests/cases/conformance/jsx/tsxSfcReturnArray.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @filename: file.tsx
// @jsx: preserve
// @module: amd
// @noLib: true
// @skipLibCheck: true
// @libFiles: react.d.ts,lib.d.ts

import React = require('react');

const Foo = (props: any) => [<span />, <span />];

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? :)

Copy link
Contributor Author

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...?


function Greet(x: { name?: string }) {
return [<span />, <span />];
}

const foo = <Foo />;
const G = <Greet />;