@@ -278,6 +278,8 @@ namespace ts {
278
278
let deferredGlobalAsyncIterableIteratorType: GenericType;
279
279
let deferredGlobalTemplateStringsArrayType: ObjectType;
280
280
let deferredJsxElementClassType: Type;
281
+ let deferredJsxElementType: Type;
282
+ let deferredJsxStatelessElementType: Type;
281
283
282
284
let deferredNodes: Node[];
283
285
let deferredUnusedIdentifierNodes: Node[];
@@ -398,7 +400,6 @@ namespace ts {
398
400
});
399
401
const typeofType = createTypeofType();
400
402
401
- let jsxElementType: Type;
402
403
let _jsxNamespace: string;
403
404
let _jsxFactoryEntity: EntityName;
404
405
@@ -12306,12 +12307,12 @@ namespace ts {
12306
12307
type.flags & TypeFlags.UnionOrIntersection && !forEach((<UnionOrIntersectionType>type).types, t => !isValidSpreadType(t)));
12307
12308
}
12308
12309
12309
- function checkJsxSelfClosingElement(node: JsxSelfClosingElement) {
12310
+ function checkJsxSelfClosingElement(node: JsxSelfClosingElement): Type {
12310
12311
checkJsxOpeningLikeElement(node);
12311
- return jsxElementType || anyType;
12312
+ return getJsxGlobalElementType() || anyType;
12312
12313
}
12313
12314
12314
- function checkJsxElement(node: JsxElement) {
12315
+ function checkJsxElement(node: JsxElement): Type {
12315
12316
// Check attributes
12316
12317
checkJsxOpeningLikeElement(node.openingElement);
12317
12318
@@ -12338,7 +12339,7 @@ namespace ts {
12338
12339
}
12339
12340
}
12340
12341
12341
- return jsxElementType || anyType;
12342
+ return getJsxGlobalElementType() || anyType;
12342
12343
}
12343
12344
12344
12345
/**
@@ -12579,13 +12580,14 @@ namespace ts {
12579
12580
function defaultTryGetJsxStatelessFunctionAttributesType(openingLikeElement: JsxOpeningLikeElement, elementType: Type, elemInstanceType: Type, elementClassType?: Type): Type {
12580
12581
Debug.assert(!(elementType.flags & TypeFlags.Union));
12581
12582
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
12582
- if (jsxElementType) {
12583
+ const jsxStatelessElementType = getJsxGlobalStatelessElementType();
12584
+ if (jsxStatelessElementType) {
12583
12585
// We don't call getResolvedSignature here because we have already resolve the type of JSX Element.
12584
12586
const callSignature = getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, /*candidatesOutArray*/ undefined);
12585
12587
if (callSignature !== unknownSignature) {
12586
12588
const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
12587
12589
let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
12588
- if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType )) {
12590
+ if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType )) {
12589
12591
// Intersect in JSX.IntrinsicAttributes if it exists
12590
12592
const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes);
12591
12593
if (intrinsicAttributes !== unknownType) {
@@ -12613,7 +12615,8 @@ namespace ts {
12613
12615
Debug.assert(!(elementType.flags & TypeFlags.Union));
12614
12616
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
12615
12617
// Is this is a stateless function component? See if its single signature's return type is assignable to the JSX Element Type
12616
- if (jsxElementType) {
12618
+ const jsxStatelessElementType = getJsxGlobalStatelessElementType();
12619
+ if (jsxStatelessElementType) {
12617
12620
// We don't call getResolvedSignature because here we have already resolve the type of JSX Element.
12618
12621
const candidatesOutArray: Signature[] = [];
12619
12622
getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, candidatesOutArray);
@@ -12622,7 +12625,7 @@ namespace ts {
12622
12625
for (const candidate of candidatesOutArray) {
12623
12626
const callReturnType = getReturnTypeOfSignature(candidate);
12624
12627
const paramType = callReturnType && (candidate.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(candidate.parameters[0]));
12625
- if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType )) {
12628
+ if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType )) {
12626
12629
let shouldBeCandidate = true;
12627
12630
for (const attribute of openingLikeElement.attributes.properties) {
12628
12631
if (isJsxAttribute(attribute) &&
@@ -12871,6 +12874,23 @@ namespace ts {
12871
12874
return deferredJsxElementClassType;
12872
12875
}
12873
12876
12877
+ function getJsxGlobalElementType(): Type {
12878
+ if (!deferredJsxElementType) {
12879
+ deferredJsxElementType = getExportedTypeFromNamespace(JsxNames.JSX, JsxNames.Element);
12880
+ }
12881
+ return deferredJsxElementType;
12882
+ }
12883
+
12884
+ function getJsxGlobalStatelessElementType(): Type {
12885
+ if (!deferredJsxStatelessElementType) {
12886
+ const jsxElementType = getJsxGlobalElementType();
12887
+ if (jsxElementType){
12888
+ deferredJsxStatelessElementType = getUnionType([jsxElementType, nullType]);
12889
+ }
12890
+ }
12891
+ return deferredJsxStatelessElementType;
12892
+ }
12893
+
12874
12894
/**
12875
12895
* Returns all the properties of the Jsx.IntrinsicElements interface
12876
12896
*/
@@ -12885,7 +12905,7 @@ namespace ts {
12885
12905
error(errorNode, Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided);
12886
12906
}
12887
12907
12888
- if (jsxElementType === undefined) {
12908
+ if (getJsxGlobalElementType() === undefined) {
12889
12909
if (compilerOptions.noImplicitAny) {
12890
12910
error(errorNode, Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
12891
12911
}
@@ -21917,7 +21937,6 @@ namespace ts {
21917
21937
globalNumberType = getGlobalType("Number", /*arity*/ 0, /*reportErrors*/ true);
21918
21938
globalBooleanType = getGlobalType("Boolean", /*arity*/ 0, /*reportErrors*/ true);
21919
21939
globalRegExpType = getGlobalType("RegExp", /*arity*/ 0, /*reportErrors*/ true);
21920
- jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element);
21921
21940
anyArrayType = createArrayType(anyType);
21922
21941
autoArrayType = createArrayType(autoType);
21923
21942
0 commit comments