@@ -284,6 +284,8 @@ namespace ts {
284284 let deferredGlobalAsyncIterableIteratorType: GenericType;
285285 let deferredGlobalTemplateStringsArrayType: ObjectType;
286286 let deferredJsxElementClassType: Type;
287+ let deferredJsxElementType: Type;
288+ let deferredJsxStatelessElementType: Type;
287289
288290 let deferredNodes: Node[];
289291 let deferredUnusedIdentifierNodes: Node[];
@@ -404,7 +406,6 @@ namespace ts {
404406 });
405407 const typeofType = createTypeofType();
406408
407- let jsxElementType: Type;
408409 let _jsxNamespace: string;
409410 let _jsxFactoryEntity: EntityName;
410411
@@ -12462,12 +12463,12 @@ namespace ts {
1246212463 type.flags & TypeFlags.UnionOrIntersection && !forEach((<UnionOrIntersectionType>type).types, t => !isValidSpreadType(t)));
1246312464 }
1246412465
12465- function checkJsxSelfClosingElement(node: JsxSelfClosingElement) {
12466+ function checkJsxSelfClosingElement(node: JsxSelfClosingElement): Type {
1246612467 checkJsxOpeningLikeElement(node);
12467- return jsxElementType || anyType;
12468+ return getJsxGlobalElementType() || anyType;
1246812469 }
1246912470
12470- function checkJsxElement(node: JsxElement) {
12471+ function checkJsxElement(node: JsxElement): Type {
1247112472 // Check attributes
1247212473 checkJsxOpeningLikeElement(node.openingElement);
1247312474
@@ -12494,7 +12495,7 @@ namespace ts {
1249412495 }
1249512496 }
1249612497
12497- return jsxElementType || anyType;
12498+ return getJsxGlobalElementType() || anyType;
1249812499 }
1249912500
1250012501 /**
@@ -12735,13 +12736,14 @@ namespace ts {
1273512736 function defaultTryGetJsxStatelessFunctionAttributesType(openingLikeElement: JsxOpeningLikeElement, elementType: Type, elemInstanceType: Type, elementClassType?: Type): Type {
1273612737 Debug.assert(!(elementType.flags & TypeFlags.Union));
1273712738 if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
12738- if (jsxElementType) {
12739+ const jsxStatelessElementType = getJsxGlobalStatelessElementType();
12740+ if (jsxStatelessElementType) {
1273912741 // We don't call getResolvedSignature here because we have already resolve the type of JSX Element.
1274012742 const callSignature = getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, /*candidatesOutArray*/ undefined);
1274112743 if (callSignature !== unknownSignature) {
1274212744 const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
1274312745 let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
12744- if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType )) {
12746+ if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType )) {
1274512747 // Intersect in JSX.IntrinsicAttributes if it exists
1274612748 const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes);
1274712749 if (intrinsicAttributes !== unknownType) {
@@ -12769,7 +12771,8 @@ namespace ts {
1276912771 Debug.assert(!(elementType.flags & TypeFlags.Union));
1277012772 if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
1277112773 // Is this is a stateless function component? See if its single signature's return type is assignable to the JSX Element Type
12772- if (jsxElementType) {
12774+ const jsxStatelessElementType = getJsxGlobalStatelessElementType();
12775+ if (jsxStatelessElementType) {
1277312776 // We don't call getResolvedSignature because here we have already resolve the type of JSX Element.
1277412777 const candidatesOutArray: Signature[] = [];
1277512778 getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, candidatesOutArray);
@@ -12778,7 +12781,7 @@ namespace ts {
1277812781 for (const candidate of candidatesOutArray) {
1277912782 const callReturnType = getReturnTypeOfSignature(candidate);
1278012783 const paramType = callReturnType && (candidate.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(candidate.parameters[0]));
12781- if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType )) {
12784+ if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType )) {
1278212785 let shouldBeCandidate = true;
1278312786 for (const attribute of openingLikeElement.attributes.properties) {
1278412787 if (isJsxAttribute(attribute) &&
@@ -13022,6 +13025,23 @@ namespace ts {
1302213025 return deferredJsxElementClassType;
1302313026 }
1302413027
13028+ function getJsxGlobalElementType(): Type {
13029+ if (!deferredJsxElementType) {
13030+ deferredJsxElementType = getExportedTypeFromNamespace(JsxNames.JSX, JsxNames.Element);
13031+ }
13032+ return deferredJsxElementType;
13033+ }
13034+
13035+ function getJsxGlobalStatelessElementType(): Type {
13036+ if (!deferredJsxStatelessElementType) {
13037+ const jsxElementType = getJsxGlobalElementType();
13038+ if (jsxElementType) {
13039+ deferredJsxStatelessElementType = getUnionType([jsxElementType, nullType]);
13040+ }
13041+ }
13042+ return deferredJsxStatelessElementType;
13043+ }
13044+
1302513045 /**
1302613046 * Returns all the properties of the Jsx.IntrinsicElements interface
1302713047 */
@@ -13036,7 +13056,7 @@ namespace ts {
1303613056 error(errorNode, Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided);
1303713057 }
1303813058
13039- if (jsxElementType === undefined) {
13059+ if (getJsxGlobalElementType() === undefined) {
1304013060 if (noImplicitAny) {
1304113061 error(errorNode, Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
1304213062 }
@@ -22080,7 +22100,6 @@ namespace ts {
2208022100 globalNumberType = getGlobalType("Number", /*arity*/ 0, /*reportErrors*/ true);
2208122101 globalBooleanType = getGlobalType("Boolean", /*arity*/ 0, /*reportErrors*/ true);
2208222102 globalRegExpType = getGlobalType("RegExp", /*arity*/ 0, /*reportErrors*/ true);
22083- jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element);
2208422103 anyArrayType = createArrayType(anyType);
2208522104 autoArrayType = createArrayType(autoType);
2208622105
0 commit comments