mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 15:45:27 -05:00
Merge pull request #26679 from Microsoft/improveGetTypeOfExpression
Improve control flow analysis of type assertions
This commit is contained in:
@@ -21699,15 +21699,19 @@ namespace ts {
|
||||
* to cache the result.
|
||||
*/
|
||||
function getTypeOfExpression(node: Expression, cache?: boolean) {
|
||||
const expr = skipParentheses(node);
|
||||
// Optimize for the common case of a call to a function with a single non-generic call
|
||||
// signature where we can just fetch the return type without checking the arguments.
|
||||
if (node.kind === SyntaxKind.CallExpression && (<CallExpression>node).expression.kind !== SyntaxKind.SuperKeyword && !isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true) && !isSymbolOrSymbolForCall(node)) {
|
||||
const funcType = checkNonNullExpression((<CallExpression>node).expression);
|
||||
if (expr.kind === SyntaxKind.CallExpression && (<CallExpression>expr).expression.kind !== SyntaxKind.SuperKeyword && !isRequireCall(expr, /*checkArgumentIsStringLiteralLike*/ true) && !isSymbolOrSymbolForCall(expr)) {
|
||||
const funcType = checkNonNullExpression((<CallExpression>expr).expression);
|
||||
const signature = getSingleCallSignature(funcType);
|
||||
if (signature && !signature.typeParameters) {
|
||||
return getReturnTypeOfSignature(signature);
|
||||
}
|
||||
}
|
||||
else if (expr.kind === SyntaxKind.TypeAssertionExpression || expr.kind === SyntaxKind.AsExpression) {
|
||||
return getTypeFromTypeNode((<TypeAssertion>expr).type);
|
||||
}
|
||||
// Otherwise simply call checkExpression. Ideally, the entire family of checkXXX functions
|
||||
// should have a parameter that indicates whether full error checking is required such that
|
||||
// we can perform the optimizations locally.
|
||||
|
||||
Reference in New Issue
Block a user