-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Fix JSX fragment children type checking in react-jsx mode #62743
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
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-jsx-fragment-typing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+413
−11
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
607b949
Initial plan
Copilot 483093f
Add test case and implement fix for JSX Fragment typing with react-js…
Copilot 7fcfd01
Fix regression: only apply contextual type for fragment children, not…
Copilot 983250f
Fix error message to show 'Fragment' instead of 'React' for react-jsx…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| //// [tests/cases/compiler/jsxFragmentChildrenCheck.tsx] //// | ||
|
|
||
| //// [index.d.ts] | ||
| export const jsx: any; | ||
| export const jsxs: any; | ||
|
|
||
| type JsxElement = | ||
| | JsxElementArray | ||
| | undefined | ||
| | string | ||
| | ((arg: { foo: "bar" }) => void); | ||
| interface JsxElementArray extends Array<JsxElement> {} | ||
|
|
||
| interface FragmentProps { | ||
| children?: JsxElement; | ||
| } | ||
|
|
||
| export const Fragment: (props: FragmentProps) => any; | ||
|
|
||
| declare global { | ||
| namespace JSX { | ||
| interface IntrinsicElements { | ||
| div: any; | ||
| span: any; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //// [index.tsx] | ||
| import { Fragment } from "@test/jsx-runtime"; | ||
|
|
||
| // This should pass - using explicit Fragment | ||
| <Fragment> | ||
| {"ok"} | ||
| {({ foo }) => "also ok"} | ||
| </Fragment>; | ||
|
|
||
| // This should also pass - using <> syntax should be equivalent | ||
| <> | ||
| {"ok"} | ||
| {({ foo }) => "should also be ok"} | ||
| </>; | ||
|
|
||
|
|
||
| //// [index.js] | ||
| import { jsxs as _jsxs, Fragment as _Fragment } from "@test/jsx-runtime"; | ||
| import { Fragment } from "@test/jsx-runtime"; | ||
| // This should pass - using explicit Fragment | ||
| _jsxs(Fragment, { children: ["ok", function (_a) { | ||
| var foo = _a.foo; | ||
| return "also ok"; | ||
| }] }); | ||
| // This should also pass - using <> syntax should be equivalent | ||
| _jsxs(_Fragment, { children: ["ok", function (_a) { | ||
| var foo = _a.foo; | ||
| return "should also be ok"; | ||
| }] }); |
80 changes: 80 additions & 0 deletions
80
tests/baselines/reference/jsxFragmentChildrenCheck.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //// [tests/cases/compiler/jsxFragmentChildrenCheck.tsx] //// | ||
|
|
||
| === node_modules/@test/jsx-runtime/index.d.ts === | ||
| export const jsx: any; | ||
| >jsx : Symbol(jsx, Decl(index.d.ts, 0, 12)) | ||
|
|
||
| export const jsxs: any; | ||
| >jsxs : Symbol(jsxs, Decl(index.d.ts, 1, 12)) | ||
|
|
||
| type JsxElement = | ||
| >JsxElement : Symbol(JsxElement, Decl(index.d.ts, 1, 23)) | ||
|
|
||
| | JsxElementArray | ||
| >JsxElementArray : Symbol(JsxElementArray, Decl(index.d.ts, 7, 36)) | ||
|
|
||
| | undefined | ||
| | string | ||
| | ((arg: { foo: "bar" }) => void); | ||
| >arg : Symbol(arg, Decl(index.d.ts, 7, 6)) | ||
| >foo : Symbol(foo, Decl(index.d.ts, 7, 12)) | ||
|
|
||
| interface JsxElementArray extends Array<JsxElement> {} | ||
| >JsxElementArray : Symbol(JsxElementArray, Decl(index.d.ts, 7, 36)) | ||
| >Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) | ||
| >JsxElement : Symbol(JsxElement, Decl(index.d.ts, 1, 23)) | ||
|
|
||
| interface FragmentProps { | ||
| >FragmentProps : Symbol(FragmentProps, Decl(index.d.ts, 8, 54)) | ||
|
|
||
| children?: JsxElement; | ||
| >children : Symbol(FragmentProps.children, Decl(index.d.ts, 10, 25)) | ||
| >JsxElement : Symbol(JsxElement, Decl(index.d.ts, 1, 23)) | ||
| } | ||
|
|
||
| export const Fragment: (props: FragmentProps) => any; | ||
| >Fragment : Symbol(Fragment, Decl(index.d.ts, 14, 12)) | ||
| >props : Symbol(props, Decl(index.d.ts, 14, 24)) | ||
| >FragmentProps : Symbol(FragmentProps, Decl(index.d.ts, 8, 54)) | ||
|
|
||
| declare global { | ||
| >global : Symbol(global, Decl(index.d.ts, 14, 53)) | ||
|
|
||
| namespace JSX { | ||
| >JSX : Symbol(JSX, Decl(index.d.ts, 16, 16)) | ||
|
|
||
| interface IntrinsicElements { | ||
| >IntrinsicElements : Symbol(IntrinsicElements, Decl(index.d.ts, 17, 17)) | ||
|
|
||
| div: any; | ||
| >div : Symbol(IntrinsicElements.div, Decl(index.d.ts, 18, 33)) | ||
|
|
||
| span: any; | ||
| >span : Symbol(IntrinsicElements.span, Decl(index.d.ts, 19, 15)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| === index.tsx === | ||
| import { Fragment } from "@test/jsx-runtime"; | ||
| >Fragment : Symbol(Fragment, Decl(index.tsx, 0, 8)) | ||
|
|
||
| // This should pass - using explicit Fragment | ||
| <Fragment> | ||
| >Fragment : Symbol(Fragment, Decl(index.tsx, 0, 8)) | ||
|
|
||
| {"ok"} | ||
| {({ foo }) => "also ok"} | ||
| >foo : Symbol(foo, Decl(index.tsx, 5, 5)) | ||
|
|
||
| </Fragment>; | ||
| >Fragment : Symbol(Fragment, Decl(index.tsx, 0, 8)) | ||
|
|
||
| // This should also pass - using <> syntax should be equivalent | ||
| <> | ||
| {"ok"} | ||
| {({ foo }) => "should also be ok"} | ||
| >foo : Symbol(foo, Decl(index.tsx, 11, 5)) | ||
|
|
||
| </>; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| //// [tests/cases/compiler/jsxFragmentChildrenCheck.tsx] //// | ||
|
|
||
| === node_modules/@test/jsx-runtime/index.d.ts === | ||
| export const jsx: any; | ||
| >jsx : any | ||
|
|
||
| export const jsxs: any; | ||
| >jsxs : any | ||
|
|
||
| type JsxElement = | ||
| >JsxElement : JsxElement | ||
| > : ^^^^^^^^^^ | ||
|
|
||
| | JsxElementArray | ||
| | undefined | ||
| | string | ||
| | ((arg: { foo: "bar" }) => void); | ||
| >arg : { foo: "bar"; } | ||
| > : ^^^^^^^ ^^^ | ||
| >foo : "bar" | ||
| > : ^^^^^ | ||
|
|
||
| interface JsxElementArray extends Array<JsxElement> {} | ||
|
|
||
| interface FragmentProps { | ||
| children?: JsxElement; | ||
| >children : JsxElement | ||
| > : ^^^^^^^^^^ | ||
| } | ||
|
|
||
| export const Fragment: (props: FragmentProps) => any; | ||
| >Fragment : (props: FragmentProps) => any | ||
| > : ^ ^^ ^^^^^ | ||
| >props : FragmentProps | ||
| > : ^^^^^^^^^^^^^ | ||
|
|
||
| declare global { | ||
| >global : any | ||
| > : ^^^ | ||
|
|
||
| namespace JSX { | ||
| interface IntrinsicElements { | ||
| div: any; | ||
| >div : any | ||
|
|
||
| span: any; | ||
| >span : any | ||
| } | ||
| } | ||
| } | ||
|
|
||
| === index.tsx === | ||
| import { Fragment } from "@test/jsx-runtime"; | ||
| >Fragment : (props: import("node_modules/@test/jsx-runtime/index").FragmentProps) => any | ||
| > : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| // This should pass - using explicit Fragment | ||
| <Fragment> | ||
| ><Fragment> {"ok"} {({ foo }) => "also ok"}</Fragment> : error | ||
| >Fragment : (props: import("node_modules/@test/jsx-runtime/index").FragmentProps) => any | ||
| > : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| {"ok"} | ||
| >"ok" : "ok" | ||
| > : ^^^^ | ||
|
|
||
| {({ foo }) => "also ok"} | ||
| >({ foo }) => "also ok" : ({ foo }: { foo: "bar"; }) => string | ||
| > : ^ ^^^^^^^^^ ^^^^^^^^^^^^^^ | ||
| >foo : "bar" | ||
| > : ^^^^^ | ||
| >"also ok" : "also ok" | ||
| > : ^^^^^^^^^ | ||
|
|
||
| </Fragment>; | ||
| >Fragment : (props: import("node_modules/@test/jsx-runtime/index").FragmentProps) => any | ||
| > : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| // This should also pass - using <> syntax should be equivalent | ||
| <> | ||
| ><> {"ok"} {({ foo }) => "should also be ok"}</> : any | ||
|
|
||
| {"ok"} | ||
| >"ok" : "ok" | ||
| > : ^^^^ | ||
|
|
||
| {({ foo }) => "should also be ok"} | ||
| >({ foo }) => "should also be ok" : ({ foo }: { foo: "bar"; }) => string | ||
| > : ^ ^^^^^^^^^ ^^^^^^^^^^^^^^ | ||
| >foo : "bar" | ||
| > : ^^^^^ | ||
| >"should also be ok" : "should also be ok" | ||
| > : ^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| </>; | ||
|
|
5 changes: 4 additions & 1 deletion
5
tests/baselines/reference/jsxFragmentFactoryReference(jsx=react-jsx).errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,15 @@ | ||
| jsxFragmentFactoryReference.tsx(3,9): error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. | ||
| jsxFragmentFactoryReference.tsx(3,9): error TS2879: Using JSX fragments requires fragment factory 'Fragment' to be in scope, but it could not be found. | ||
|
|
||
|
|
||
| ==== jsxFragmentFactoryReference.tsx (1 errors) ==== | ||
| ==== jsxFragmentFactoryReference.tsx (2 errors) ==== | ||
| export class LoggedOut { | ||
| content = () => ( | ||
| <></> | ||
| ~~ | ||
| !!! error TS2875: This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. | ||
| ~~ | ||
| !!! error TS2879: Using JSX fragments requires fragment factory 'Fragment' to be in scope, but it could not be found. | ||
| ) | ||
| } | ||
|
|
5 changes: 4 additions & 1 deletion
5
tests/baselines/reference/jsxFragmentFactoryReference(jsx=react-jsxdev).errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,15 @@ | ||
| jsxFragmentFactoryReference.tsx(3,9): error TS2875: This JSX tag requires the module path 'react/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. | ||
| jsxFragmentFactoryReference.tsx(3,9): error TS2879: Using JSX fragments requires fragment factory 'Fragment' to be in scope, but it could not be found. | ||
|
|
||
|
|
||
| ==== jsxFragmentFactoryReference.tsx (1 errors) ==== | ||
| ==== jsxFragmentFactoryReference.tsx (2 errors) ==== | ||
| export class LoggedOut { | ||
| content = () => ( | ||
| <></> | ||
| ~~ | ||
| !!! error TS2875: This JSX tag requires the module path 'react/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed. | ||
| ~~ | ||
| !!! error TS2879: Using JSX fragments requires fragment factory 'Fragment' to be in scope, but it could not be found. | ||
| ) | ||
| } | ||
|
|
5 changes: 4 additions & 1 deletion
5
tests/baselines/reference/jsxJsxsCjsTransformCustomImport(jsx=react-jsx).errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
tests/baselines/reference/jsxJsxsCjsTransformCustomImport(jsx=react-jsxdev).errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.