@@ -284,6 +284,8 @@ namespace ts {
284
284
let deferredGlobalAsyncIterableIteratorType: GenericType;
285
285
let deferredGlobalTemplateStringsArrayType: ObjectType;
286
286
let deferredJsxElementClassType: Type;
287
+ let deferredJsxElementType: Type;
288
+ let deferredJsxStatelessElementType: Type;
287
289
288
290
let deferredNodes: Node[];
289
291
let deferredUnusedIdentifierNodes: Node[];
@@ -404,7 +406,6 @@ namespace ts {
404
406
});
405
407
const typeofType = createTypeofType();
406
408
407
- let jsxElementType: Type;
408
409
let _jsxNamespace: string;
409
410
let _jsxFactoryEntity: EntityName;
410
411
@@ -12462,12 +12463,12 @@ namespace ts {
12462
12463
type.flags & TypeFlags.UnionOrIntersection && !forEach((<UnionOrIntersectionType>type).types, t => !isValidSpreadType(t)));
12463
12464
}
12464
12465
12465
- function checkJsxSelfClosingElement(node: JsxSelfClosingElement) {
12466
+ function checkJsxSelfClosingElement(node: JsxSelfClosingElement): Type {
12466
12467
checkJsxOpeningLikeElement(node);
12467
- return jsxElementType || anyType;
12468
+ return getJsxGlobalElementType() || anyType;
12468
12469
}
12469
12470
12470
- function checkJsxElement(node: JsxElement) {
12471
+ function checkJsxElement(node: JsxElement): Type {
12471
12472
// Check attributes
12472
12473
checkJsxOpeningLikeElement(node.openingElement);
12473
12474
@@ -12494,7 +12495,7 @@ namespace ts {
12494
12495
}
12495
12496
}
12496
12497
12497
- return jsxElementType || anyType;
12498
+ return getJsxGlobalElementType() || anyType;
12498
12499
}
12499
12500
12500
12501
/**
@@ -12735,13 +12736,14 @@ namespace ts {
12735
12736
function defaultTryGetJsxStatelessFunctionAttributesType(openingLikeElement: JsxOpeningLikeElement, elementType: Type, elemInstanceType: Type, elementClassType?: Type): Type {
12736
12737
Debug.assert(!(elementType.flags & TypeFlags.Union));
12737
12738
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
12738
- if (jsxElementType) {
12739
+ const jsxStatelessElementType = getJsxGlobalStatelessElementType();
12740
+ if (jsxStatelessElementType) {
12739
12741
// We don't call getResolvedSignature here because we have already resolve the type of JSX Element.
12740
12742
const callSignature = getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, /*candidatesOutArray*/ undefined);
12741
12743
if (callSignature !== unknownSignature) {
12742
12744
const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
12743
12745
let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
12744
- if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType )) {
12746
+ if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType )) {
12745
12747
// Intersect in JSX.IntrinsicAttributes if it exists
12746
12748
const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes);
12747
12749
if (intrinsicAttributes !== unknownType) {
@@ -12769,7 +12771,8 @@ namespace ts {
12769
12771
Debug.assert(!(elementType.flags & TypeFlags.Union));
12770
12772
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
12771
12773
// 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) {
12773
12776
// We don't call getResolvedSignature because here we have already resolve the type of JSX Element.
12774
12777
const candidatesOutArray: Signature[] = [];
12775
12778
getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, candidatesOutArray);
@@ -12778,7 +12781,7 @@ namespace ts {
12778
12781
for (const candidate of candidatesOutArray) {
12779
12782
const callReturnType = getReturnTypeOfSignature(candidate);
12780
12783
const paramType = callReturnType && (candidate.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(candidate.parameters[0]));
12781
- if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType )) {
12784
+ if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType )) {
12782
12785
let shouldBeCandidate = true;
12783
12786
for (const attribute of openingLikeElement.attributes.properties) {
12784
12787
if (isJsxAttribute(attribute) &&
@@ -13022,6 +13025,23 @@ namespace ts {
13022
13025
return deferredJsxElementClassType;
13023
13026
}
13024
13027
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
+
13025
13045
/**
13026
13046
* Returns all the properties of the Jsx.IntrinsicElements interface
13027
13047
*/
@@ -13036,7 +13056,7 @@ namespace ts {
13036
13056
error(errorNode, Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided);
13037
13057
}
13038
13058
13039
- if (jsxElementType === undefined) {
13059
+ if (getJsxGlobalElementType() === undefined) {
13040
13060
if (noImplicitAny) {
13041
13061
error(errorNode, Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
13042
13062
}
@@ -22080,7 +22100,6 @@ namespace ts {
22080
22100
globalNumberType = getGlobalType("Number", /*arity*/ 0, /*reportErrors*/ true);
22081
22101
globalBooleanType = getGlobalType("Boolean", /*arity*/ 0, /*reportErrors*/ true);
22082
22102
globalRegExpType = getGlobalType("RegExp", /*arity*/ 0, /*reportErrors*/ true);
22083
- jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element);
22084
22103
anyArrayType = createArrayType(anyType);
22085
22104
autoArrayType = createArrayType(autoType);
22086
22105
0 commit comments