diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index dcea5667eb9..12f68f7b41d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9499,52 +9499,28 @@ namespace ts { return links.resolvedSymbol; } - /** - * Given a type, resolve its constructor signatures. - * If no construct signatures, it should have call signature, - * otherwise it's not a valid React component. - */ - function getConstructOrCallSignature(type: Type): Signature[] { - // If the type is a union type, recursively resolve the signatures. - if (type.flags & TypeFlags.Union) { - const signatures: Signature[] = []; - forEach(( type).types, (childType) => { - const childSignatures = getConstructOrCallSignature(childType); - forEach(childSignatures, (signature) => { - signatures.push(signature); - }); - }); - return signatures; - } - else { - const constructSignatures = getSignaturesOfType(type, SignatureKind.Construct); - if (constructSignatures.length === 0) { - // No construct signatures, try call signatures - return getSignaturesOfType(type, SignatureKind.Call); - } - else { - return constructSignatures; - } - } - } - /** * Given a JSX element that is a class element, finds the Element Instance Type. If the * element is not a class element, or the class element type cannot be determined, returns 'undefined'. * For example, in the element , the element instance type is `MyClass` (not `typeof MyClass`). */ function getJsxElementInstanceType(node: JsxOpeningLikeElement, valueType: Type) { + Debug.assert(!(valueType.flags & TypeFlags.Union)); if (isTypeAny(valueType)) { // Short-circuit if the class tag is using an element type 'any' return anyType; } - // Resolve the signatures - const signatures = getConstructOrCallSignature(valueType); + // Resolve the signatures, preferring constructor + let signatures = getSignaturesOfType(valueType, SignatureKind.Construct); if (signatures.length === 0) { - // We found no signatures at all, which is an error - error(node.tagName, Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, getTextOfNode(node.tagName)); - return unknownType; + // No construct signatures, try call signatures + signatures = getSignaturesOfType(valueType, SignatureKind.Call); + if (signatures.length === 0) { + // We found no signatures at all, which is an error + error(node.tagName, Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, getTextOfNode(node.tagName)); + return unknownType; + } } return getUnionType(signatures.map(getReturnTypeOfSignature));