-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fixed JSX attributes discriminating based on optional children #53980
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
Merged
weswigham
merged 2 commits into
microsoft:main
from
Andarist:fix/jsx-children-as-optional-discriminant
Apr 26, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
114 changes: 114 additions & 0 deletions
114
tests/baselines/reference/contextuallyTypedJsxChildren.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,114 @@ | ||
=== tests/cases/compiler/contextuallyTypedJsxChildren.tsx === | ||
/// <reference path="react16.d.ts" /> | ||
|
||
import React from 'react'; | ||
>React : Symbol(React, Decl(contextuallyTypedJsxChildren.tsx, 2, 6)) | ||
|
||
// repro from https://github.com/microsoft/TypeScript/issues/53941 | ||
declare namespace DropdownMenu { | ||
>DropdownMenu : Symbol(DropdownMenu, Decl(contextuallyTypedJsxChildren.tsx, 2, 26), Decl(contextuallyTypedJsxChildren.tsx, 23, 13)) | ||
|
||
interface BaseProps { | ||
>BaseProps : Symbol(BaseProps, Decl(contextuallyTypedJsxChildren.tsx, 5, 32)) | ||
|
||
icon: string; | ||
>icon : Symbol(BaseProps.icon, Decl(contextuallyTypedJsxChildren.tsx, 6, 23)) | ||
|
||
label: string; | ||
>label : Symbol(BaseProps.label, Decl(contextuallyTypedJsxChildren.tsx, 7, 17)) | ||
} | ||
interface PropsWithChildren extends BaseProps { | ||
>PropsWithChildren : Symbol(PropsWithChildren, Decl(contextuallyTypedJsxChildren.tsx, 9, 3)) | ||
>BaseProps : Symbol(BaseProps, Decl(contextuallyTypedJsxChildren.tsx, 5, 32)) | ||
|
||
children(props: { onClose: () => void }): JSX.Element; | ||
>children : Symbol(PropsWithChildren.children, Decl(contextuallyTypedJsxChildren.tsx, 10, 49)) | ||
>props : Symbol(props, Decl(contextuallyTypedJsxChildren.tsx, 11, 13)) | ||
>onClose : Symbol(onClose, Decl(contextuallyTypedJsxChildren.tsx, 11, 21)) | ||
>JSX : Symbol(JSX, Decl(react16.d.ts, 2493, 12)) | ||
>Element : Symbol(JSX.Element, Decl(react16.d.ts, 2494, 23)) | ||
|
||
controls?: never | undefined; | ||
>controls : Symbol(PropsWithChildren.controls, Decl(contextuallyTypedJsxChildren.tsx, 11, 58)) | ||
} | ||
interface PropsWithControls extends BaseProps { | ||
>PropsWithControls : Symbol(PropsWithControls, Decl(contextuallyTypedJsxChildren.tsx, 13, 3)) | ||
>BaseProps : Symbol(BaseProps, Decl(contextuallyTypedJsxChildren.tsx, 5, 32)) | ||
|
||
controls: Control[]; | ||
>controls : Symbol(PropsWithControls.controls, Decl(contextuallyTypedJsxChildren.tsx, 14, 49)) | ||
>Control : Symbol(Control, Decl(contextuallyTypedJsxChildren.tsx, 17, 3)) | ||
|
||
children?: never | undefined; | ||
>children : Symbol(PropsWithControls.children, Decl(contextuallyTypedJsxChildren.tsx, 15, 24)) | ||
} | ||
interface Control { | ||
>Control : Symbol(Control, Decl(contextuallyTypedJsxChildren.tsx, 17, 3)) | ||
|
||
title: string; | ||
>title : Symbol(Control.title, Decl(contextuallyTypedJsxChildren.tsx, 18, 21)) | ||
} | ||
type Props = PropsWithChildren | PropsWithControls; | ||
>Props : Symbol(Props, Decl(contextuallyTypedJsxChildren.tsx, 20, 3)) | ||
>PropsWithChildren : Symbol(PropsWithChildren, Decl(contextuallyTypedJsxChildren.tsx, 9, 3)) | ||
>PropsWithControls : Symbol(PropsWithControls, Decl(contextuallyTypedJsxChildren.tsx, 13, 3)) | ||
} | ||
declare const DropdownMenu: React.ComponentType<DropdownMenu.Props>; | ||
>DropdownMenu : Symbol(DropdownMenu, Decl(contextuallyTypedJsxChildren.tsx, 2, 26), Decl(contextuallyTypedJsxChildren.tsx, 23, 13)) | ||
>React : Symbol(React, Decl(contextuallyTypedJsxChildren.tsx, 2, 6)) | ||
>ComponentType : Symbol(React.ComponentType, Decl(react16.d.ts, 117, 60)) | ||
>DropdownMenu : Symbol(DropdownMenu, Decl(contextuallyTypedJsxChildren.tsx, 2, 26), Decl(contextuallyTypedJsxChildren.tsx, 23, 13)) | ||
>Props : Symbol(DropdownMenu.Props, Decl(contextuallyTypedJsxChildren.tsx, 20, 3)) | ||
|
||
<DropdownMenu icon="move" label="Select a direction"> | ||
>DropdownMenu : Symbol(DropdownMenu, Decl(contextuallyTypedJsxChildren.tsx, 2, 26), Decl(contextuallyTypedJsxChildren.tsx, 23, 13)) | ||
>icon : Symbol(icon, Decl(contextuallyTypedJsxChildren.tsx, 25, 13)) | ||
>label : Symbol(label, Decl(contextuallyTypedJsxChildren.tsx, 25, 25)) | ||
|
||
{({ onClose }) => ( | ||
>onClose : Symbol(onClose, Decl(contextuallyTypedJsxChildren.tsx, 26, 5)) | ||
|
||
<div> | ||
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2546, 114)) | ||
|
||
<button onClick={onClose}>Click me</button> | ||
>button : Symbol(JSX.IntrinsicElements.button, Decl(react16.d.ts, 2532, 96)) | ||
>onClick : Symbol(onClick, Decl(contextuallyTypedJsxChildren.tsx, 28, 13)) | ||
>onClose : Symbol(onClose, Decl(contextuallyTypedJsxChildren.tsx, 26, 5)) | ||
>button : Symbol(JSX.IntrinsicElements.button, Decl(react16.d.ts, 2532, 96)) | ||
|
||
</div> | ||
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2546, 114)) | ||
|
||
)} | ||
</DropdownMenu>; | ||
>DropdownMenu : Symbol(DropdownMenu, Decl(contextuallyTypedJsxChildren.tsx, 2, 26), Decl(contextuallyTypedJsxChildren.tsx, 23, 13)) | ||
|
||
<DropdownMenu | ||
>DropdownMenu : Symbol(DropdownMenu, Decl(contextuallyTypedJsxChildren.tsx, 2, 26), Decl(contextuallyTypedJsxChildren.tsx, 23, 13)) | ||
|
||
icon="move" | ||
>icon : Symbol(icon, Decl(contextuallyTypedJsxChildren.tsx, 33, 13)) | ||
|
||
label="Select a direction" | ||
>label : Symbol(label, Decl(contextuallyTypedJsxChildren.tsx, 34, 13)) | ||
|
||
children={({ onClose }) => ( | ||
>children : Symbol(children, Decl(contextuallyTypedJsxChildren.tsx, 35, 28)) | ||
>onClose : Symbol(onClose, Decl(contextuallyTypedJsxChildren.tsx, 36, 14)) | ||
|
||
<div> | ||
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2546, 114)) | ||
|
||
<button onClick={onClose}>Click me</button> | ||
>button : Symbol(JSX.IntrinsicElements.button, Decl(react16.d.ts, 2532, 96)) | ||
>onClick : Symbol(onClick, Decl(contextuallyTypedJsxChildren.tsx, 38, 13)) | ||
>onClose : Symbol(onClose, Decl(contextuallyTypedJsxChildren.tsx, 36, 14)) | ||
>button : Symbol(JSX.IntrinsicElements.button, Decl(react16.d.ts, 2532, 96)) | ||
|
||
</div> | ||
>div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2546, 114)) | ||
|
||
)} | ||
/>; | ||
|
106 changes: 106 additions & 0 deletions
106
tests/baselines/reference/contextuallyTypedJsxChildren.types
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,106 @@ | ||
=== tests/cases/compiler/contextuallyTypedJsxChildren.tsx === | ||
/// <reference path="react16.d.ts" /> | ||
|
||
import React from 'react'; | ||
>React : typeof React | ||
|
||
// repro from https://github.com/microsoft/TypeScript/issues/53941 | ||
declare namespace DropdownMenu { | ||
interface BaseProps { | ||
icon: string; | ||
>icon : string | ||
|
||
label: string; | ||
>label : string | ||
} | ||
interface PropsWithChildren extends BaseProps { | ||
children(props: { onClose: () => void }): JSX.Element; | ||
>children : (props: { onClose: () => void;}) => JSX.Element | ||
>props : { onClose: () => void; } | ||
>onClose : () => void | ||
>JSX : any | ||
|
||
controls?: never | undefined; | ||
>controls : undefined | ||
} | ||
interface PropsWithControls extends BaseProps { | ||
controls: Control[]; | ||
>controls : Control[] | ||
|
||
children?: never | undefined; | ||
>children : undefined | ||
} | ||
interface Control { | ||
title: string; | ||
>title : string | ||
} | ||
type Props = PropsWithChildren | PropsWithControls; | ||
>Props : PropsWithChildren | PropsWithControls | ||
} | ||
declare const DropdownMenu: React.ComponentType<DropdownMenu.Props>; | ||
>DropdownMenu : React.ComponentType<DropdownMenu.Props> | ||
>React : any | ||
>DropdownMenu : any | ||
|
||
<DropdownMenu icon="move" label="Select a direction"> | ||
><DropdownMenu icon="move" label="Select a direction"> {({ onClose }) => ( <div> <button onClick={onClose}>Click me</button> </div> )}</DropdownMenu> : JSX.Element | ||
>DropdownMenu : React.ComponentType<DropdownMenu.Props> | ||
>icon : string | ||
>label : string | ||
|
||
{({ onClose }) => ( | ||
>({ onClose }) => ( <div> <button onClick={onClose}>Click me</button> </div> ) : ({ onClose }: { onClose: () => void; }) => JSX.Element | ||
>onClose : () => void | ||
>( <div> <button onClick={onClose}>Click me</button> </div> ) : JSX.Element | ||
|
||
<div> | ||
><div> <button onClick={onClose}>Click me</button> </div> : JSX.Element | ||
>div : any | ||
|
||
<button onClick={onClose}>Click me</button> | ||
><button onClick={onClose}>Click me</button> : JSX.Element | ||
>button : any | ||
>onClick : () => void | ||
>onClose : () => void | ||
>button : any | ||
|
||
</div> | ||
>div : any | ||
|
||
)} | ||
</DropdownMenu>; | ||
>DropdownMenu : React.ComponentType<DropdownMenu.Props> | ||
|
||
<DropdownMenu | ||
><DropdownMenu icon="move" label="Select a direction" children={({ onClose }) => ( <div> <button onClick={onClose}>Click me</button> </div> )}/> : JSX.Element | ||
>DropdownMenu : React.ComponentType<DropdownMenu.Props> | ||
|
||
icon="move" | ||
>icon : string | ||
|
||
label="Select a direction" | ||
>label : string | ||
|
||
children={({ onClose }) => ( | ||
>children : ({ onClose }: { onClose: () => void; }) => JSX.Element | ||
>({ onClose }) => ( <div> <button onClick={onClose}>Click me</button> </div> ) : ({ onClose }: { onClose: () => void; }) => JSX.Element | ||
>onClose : () => void | ||
>( <div> <button onClick={onClose}>Click me</button> </div> ) : JSX.Element | ||
|
||
<div> | ||
><div> <button onClick={onClose}>Click me</button> </div> : JSX.Element | ||
>div : any | ||
|
||
<button onClick={onClose}>Click me</button> | ||
><button onClick={onClose}>Click me</button> : JSX.Element | ||
>button : any | ||
>onClick : () => void | ||
>onClose : () => void | ||
>button : any | ||
|
||
</div> | ||
>div : any | ||
|
||
)} | ||
/>; | ||
|
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,47 @@ | ||
// @strict: true | ||
// @jsx: react | ||
// @esModuleInterop: true | ||
// @noEmit: true | ||
|
||
/// <reference path="/.lib/react16.d.ts" /> | ||
|
||
import React from 'react'; | ||
|
||
// repro from https://github.com/microsoft/TypeScript/issues/53941 | ||
declare namespace DropdownMenu { | ||
interface BaseProps { | ||
icon: string; | ||
label: string; | ||
} | ||
interface PropsWithChildren extends BaseProps { | ||
children(props: { onClose: () => void }): JSX.Element; | ||
controls?: never | undefined; | ||
} | ||
interface PropsWithControls extends BaseProps { | ||
controls: Control[]; | ||
children?: never | undefined; | ||
} | ||
interface Control { | ||
title: string; | ||
} | ||
type Props = PropsWithChildren | PropsWithControls; | ||
} | ||
declare const DropdownMenu: React.ComponentType<DropdownMenu.Props>; | ||
|
||
<DropdownMenu icon="move" label="Select a direction"> | ||
{({ onClose }) => ( | ||
<div> | ||
<button onClick={onClose}>Click me</button> | ||
</div> | ||
)} | ||
</DropdownMenu>; | ||
|
||
<DropdownMenu | ||
icon="move" | ||
label="Select a direction" | ||
children={({ onClose }) => ( | ||
<div> | ||
<button onClick={onClose}>Click me</button> | ||
</div> | ||
)} | ||
/>; |
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.
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.
I came to the conclusion that this "branch" doesn't need any immediate adjustments because JSX expressions can't be easily used as value-based discriminants.
JsxText
is always typed asstringType
(no string literal types there)In theory, it's actually possible to have things like:
So maybe this should be improved further, for this very specific use case - just to be consistent with:
I think though that this can be done in a separate PR (if requested). It would be somewhat weird though that those 2 wouldn't behave the same, so perhaps further adjustments (beyond this function here) should be introduced in such a PR:
Note that this doesn't work in 5.0 anyway so likely I didn't regress this in #53502: TS playground 5.0