mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Simplify the checker, no recursive checking for signature is needed anymore.
This commit is contained in:
+10
-34
@@ -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((<UnionOrIntersectionType> 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 <MyClass>, 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));
|
||||
|
||||
Reference in New Issue
Block a user