Skip to content

Commit 2456744

Browse files
committed
Add early bail out for call expressions that are never type predicates
1 parent 0e879c0 commit 2456744

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/compiler/checker.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8981,6 +8981,28 @@ namespace ts {
89818981
return isLengthPushOrUnshift || isElementAssignment;
89828982
}
89838983

8984+
function maybeTypePredicateCall(node: CallExpression) {
8985+
const links = getNodeLinks(node);
8986+
if (links.maybeTypePredicate === undefined) {
8987+
links.maybeTypePredicate = getMaybeTypePredicate(node);
8988+
}
8989+
return links.maybeTypePredicate;
8990+
}
8991+
8992+
function getMaybeTypePredicate(node: CallExpression) {
8993+
if (node.expression.kind !== SyntaxKind.SuperKeyword) {
8994+
const funcType = checkNonNullExpression(node.expression);
8995+
if (funcType !== silentNeverType) {
8996+
const apparentType = getApparentType(funcType);
8997+
if (apparentType !== unknownType) {
8998+
const callSignatures = getSignaturesOfType(apparentType, SignatureKind.Call);
8999+
return !!forEach(callSignatures, sig => sig.typePredicate);
9000+
}
9001+
}
9002+
}
9003+
return false;
9004+
}
9005+
89849006
function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean, flowContainer: Node) {
89859007
let key: string;
89869008
if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) {
@@ -9495,7 +9517,7 @@ namespace ts {
94959517
}
94969518

94979519
function narrowTypeByTypePredicate(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type {
9498-
if (!hasMatchingArgument(callExpression, reference)) {
9520+
if (!hasMatchingArgument(callExpression, reference) || !maybeTypePredicateCall(callExpression)) {
94999521
return type;
95009522
}
95019523
const signature = getResolvedSignature(callExpression);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,7 @@ namespace ts {
26682668
resolvedSignature?: Signature; // Cached signature of signature node or call expression
26692669
resolvedSymbol?: Symbol; // Cached name resolution result
26702670
resolvedIndexInfo?: IndexInfo; // Cached indexing info resolution result
2671+
maybeTypePredicate?: boolean; // Cached check whether call expression might reference a type predicate
26712672
enumMemberValue?: number; // Constant value of enum member
26722673
isVisible?: boolean; // Is this node visible
26732674
hasReportedStatementInAmbientContext?: boolean; // Cache boolean if we report statements in ambient context

0 commit comments

Comments
 (0)