mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Fix regression: only apply contextual type for fragment children, not regular JSX elements
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
This commit is contained in:
co-authored by
jakebailey
parent
483093f36f
commit
7fcfd01ec3
+12
-15
@@ -33791,25 +33791,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
(isJsxElement(parent) && parent.openingElement === openingLikeElement || isJsxFragment(parent) && parent.openingFragment === openingLikeElement) &&
|
||||
getSemanticJsxChildren(parent.children).length > 0
|
||||
) {
|
||||
// Compute contextual type for children before checking them
|
||||
// Compute contextual type for fragment children before checking them
|
||||
let childrenContextualType: Type | undefined;
|
||||
if (jsxChildrenPropertyName && jsxChildrenPropertyName !== "") {
|
||||
let contextualType: Type | undefined;
|
||||
if (isJsxOpeningElement(openingLikeElement)) {
|
||||
contextualType = getApparentTypeOfContextualType(openingLikeElement.attributes, /*contextFlags*/ undefined);
|
||||
if (isJsxOpeningFragment(openingLikeElement) && jsxChildrenPropertyName && jsxChildrenPropertyName !== "") {
|
||||
// For fragments, get the props type from the Fragment factory's signature
|
||||
const fragmentType = getJSXFragmentType(openingLikeElement);
|
||||
const signatures = getSignaturesOfType(fragmentType, SignatureKind.Call);
|
||||
if (signatures.length > 0) {
|
||||
const contextualType = getTypeOfFirstParameterOfSignature(signatures[0]);
|
||||
childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName);
|
||||
}
|
||||
else if (isJsxOpeningFragment(openingLikeElement)) {
|
||||
// For fragments, get the props type from the Fragment factory's signature
|
||||
const fragmentType = getJSXFragmentType(openingLikeElement);
|
||||
const signatures = getSignaturesOfType(fragmentType, SignatureKind.Call);
|
||||
if (signatures.length > 0) {
|
||||
contextualType = getTypeOfFirstParameterOfSignature(signatures[0]);
|
||||
}
|
||||
}
|
||||
childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName);
|
||||
}
|
||||
|
||||
// Check children with contextual type
|
||||
// Check children with contextual type (only for fragments)
|
||||
const childrenTypes: Type[] = checkJsxChildren(parent, checkMode, childrenContextualType);
|
||||
|
||||
if (!hasSpreadAnyType && jsxChildrenPropertyName && jsxChildrenPropertyName !== "") {
|
||||
@@ -33819,6 +33813,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
if (explicitlySpecifyChildrenAttribute) {
|
||||
error(attributeParent, Diagnostics._0_are_specified_twice_The_attribute_named_0_will_be_overwritten, unescapeLeadingUnderscores(jsxChildrenPropertyName));
|
||||
}
|
||||
|
||||
const contextualType = isJsxOpeningElement(openingLikeElement) ? getApparentTypeOfContextualType(openingLikeElement.attributes, /*contextFlags*/ undefined) : undefined;
|
||||
const childrenContextualType = contextualType && getTypeOfPropertyOfContextualType(contextualType, jsxChildrenPropertyName);
|
||||
// If there are children in the body of JSX element, create dummy attribute "children" with the union of children types so that it will pass the attribute checking process
|
||||
const childrenPropSymbol = createSymbol(SymbolFlags.Property, jsxChildrenPropertyName);
|
||||
childrenPropSymbol.links.type = childrenTypes.length === 1 ? childrenTypes[0] :
|
||||
|
||||
@@ -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 'React' 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 'React' to be in scope, but it could not be found.
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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 'React' 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 'React' to be in scope, but it could not be found.
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
jsxJsxsCjsTransformCustomImport.tsx(2,11): error TS2875: This JSX tag requires the module path 'preact/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
|
||||
jsxJsxsCjsTransformCustomImport.tsx(2,11): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
|
||||
|
||||
|
||||
==== jsxJsxsCjsTransformCustomImport.tsx (1 errors) ====
|
||||
==== jsxJsxsCjsTransformCustomImport.tsx (2 errors) ====
|
||||
/// <reference path="/.lib/react16.d.ts" />
|
||||
const a = <>
|
||||
~~
|
||||
!!! error TS2875: This JSX tag requires the module path 'preact/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 'React' to be in scope, but it could not be found.
|
||||
<p></p>
|
||||
text
|
||||
<div className="foo"></div>
|
||||
|
||||
+4
-1
@@ -1,11 +1,14 @@
|
||||
jsxJsxsCjsTransformCustomImport.tsx(2,11): error TS2875: This JSX tag requires the module path 'preact/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
|
||||
jsxJsxsCjsTransformCustomImport.tsx(2,11): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
|
||||
|
||||
|
||||
==== jsxJsxsCjsTransformCustomImport.tsx (1 errors) ====
|
||||
==== jsxJsxsCjsTransformCustomImport.tsx (2 errors) ====
|
||||
/// <reference path="/.lib/react16.d.ts" />
|
||||
const a = <>
|
||||
~~
|
||||
!!! error TS2875: This JSX tag requires the module path 'preact/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 'React' to be in scope, but it could not be found.
|
||||
<p></p>
|
||||
text
|
||||
<div className="foo"></div>
|
||||
|
||||
+4
-1
@@ -1,4 +1,5 @@
|
||||
preact.tsx(3,11): error TS2875: This JSX tag requires the module path 'preact/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
|
||||
preact.tsx(3,11): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
|
||||
|
||||
|
||||
==== react.tsx (0 errors) ====
|
||||
@@ -12,12 +13,14 @@ preact.tsx(3,11): error TS2875: This JSX tag requires the module path 'preact/js
|
||||
</>
|
||||
|
||||
export {};
|
||||
==== preact.tsx (1 errors) ====
|
||||
==== preact.tsx (2 errors) ====
|
||||
/// <reference path="/.lib/react16.d.ts" />
|
||||
/* @jsxImportSource preact */
|
||||
const a = <>
|
||||
~~
|
||||
!!! error TS2875: This JSX tag requires the module path 'preact/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 'React' to be in scope, but it could not be found.
|
||||
<p></p>
|
||||
text
|
||||
<div className="foo"></div>
|
||||
|
||||
+4
-1
@@ -1,4 +1,5 @@
|
||||
preact.tsx(3,11): error TS2875: This JSX tag requires the module path 'preact/jsx-dev-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
|
||||
preact.tsx(3,11): error TS2879: Using JSX fragments requires fragment factory 'React' to be in scope, but it could not be found.
|
||||
|
||||
|
||||
==== react.tsx (0 errors) ====
|
||||
@@ -12,12 +13,14 @@ preact.tsx(3,11): error TS2875: This JSX tag requires the module path 'preact/js
|
||||
</>
|
||||
|
||||
export {};
|
||||
==== preact.tsx (1 errors) ====
|
||||
==== preact.tsx (2 errors) ====
|
||||
/// <reference path="/.lib/react16.d.ts" />
|
||||
/* @jsxImportSource preact */
|
||||
const a = <>
|
||||
~~
|
||||
!!! error TS2875: This JSX tag requires the module path 'preact/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 'React' to be in scope, but it could not be found.
|
||||
<p></p>
|
||||
text
|
||||
<div className="foo"></div>
|
||||
|
||||
Reference in New Issue
Block a user