Skip to content

Commit 1e96c57

Browse files
committed
Favor asserted type in type predicate narrowing
1 parent 4026c6f commit 1e96c57

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/compiler/checker.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -25428,8 +25428,12 @@ namespace ts {
2542825428
const discriminant = keyPropertyName && getTypeOfPropertyOfType(c, keyPropertyName);
2542925429
const matching = discriminant && getConstituentTypeForKeyType(type as UnionType, discriminant);
2543025430
// For each constituent t in the current type, if t and and c are directly related, pick the most
25431-
// specific of the two.
25432-
const directlyRelated = mapType(matching || type, t => isRelated(t, c) ? t : isRelated(c, t) ? c : neverType);
25431+
// specific of the two. When t and c are related in both directions, we prefer c for type predicates
25432+
// because that is the asserted type, but t for `instanceof` because generics aren't reflected in
25433+
// prototype object types.
25434+
const directlyRelated = mapType(matching || type, checkDerived ?
25435+
t => isTypeDerivedFrom(t, c) ? t : isTypeDerivedFrom(c, t) ? c : neverType :
25436+
t => isTypeSubtypeOf(c, t) ? c : isTypeSubtypeOf(t, c) ? t : neverType);
2543325437
// If no constituents are directly related, create intersections for any generic constituents that
2543425438
// are related by constraint.
2543525439
return directlyRelated.flags & TypeFlags.Never ?

0 commit comments

Comments
 (0)