diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 789f2bd8104..1ca2aff9961 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8981,6 +8981,28 @@ namespace ts { return isLengthPushOrUnshift || isElementAssignment; } + function maybeTypePredicateCall(node: CallExpression) { + const links = getNodeLinks(node); + if (links.maybeTypePredicate === undefined) { + links.maybeTypePredicate = getMaybeTypePredicate(node); + } + return links.maybeTypePredicate; + } + + function getMaybeTypePredicate(node: CallExpression) { + if (node.expression.kind !== SyntaxKind.SuperKeyword) { + const funcType = checkNonNullExpression(node.expression); + if (funcType !== silentNeverType) { + const apparentType = getApparentType(funcType); + if (apparentType !== unknownType) { + const callSignatures = getSignaturesOfType(apparentType, SignatureKind.Call); + return !!forEach(callSignatures, sig => sig.typePredicate); + } + } + } + return false; + } + function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean, flowContainer: Node) { let key: string; if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) { @@ -9495,7 +9517,7 @@ namespace ts { } function narrowTypeByTypePredicate(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type { - if (!hasMatchingArgument(callExpression, reference)) { + if (!hasMatchingArgument(callExpression, reference) || !maybeTypePredicateCall(callExpression)) { return type; } const signature = getResolvedSignature(callExpression); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index c814a2b1d46..79000ba8f05 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2668,6 +2668,7 @@ namespace ts { resolvedSignature?: Signature; // Cached signature of signature node or call expression resolvedSymbol?: Symbol; // Cached name resolution result resolvedIndexInfo?: IndexInfo; // Cached indexing info resolution result + maybeTypePredicate?: boolean; // Cached check whether call expression might reference a type predicate enumMemberValue?: number; // Constant value of enum member isVisible?: boolean; // Is this node visible hasReportedStatementInAmbientContext?: boolean; // Cache boolean if we report statements in ambient context