Skip to content

Commit 2097c67

Browse files
committed
Bring the discriminant logic back in a different form
1 parent 40ebaee commit 2097c67

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/compiler/checker.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29342,11 +29342,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2934229342
// We first attempt to filter the current type, narrowing constituents as appropriate and removing
2934329343
// constituents that are unrelated to the candidate.
2934429344
const isRelated = checkDerived ? isTypeDerivedFrom : isTypeSubtypeOf;
29345+
const keyPropertyName = type.flags & TypeFlags.Union ? getKeyPropertyName(type as UnionType) : undefined;
2934529346
let matchedCandidates: Type[] = [];
2934629347
let narrowedType = mapType(type, t =>
2934729348
mapType(
2934829349
candidate,
2934929350
c => {
29351+
if (keyPropertyName) {
29352+
// If a discriminant property is available, use only matching constituents to reduce the type.
29353+
const discriminant = keyPropertyName && getTypeOfPropertyOfType(c, keyPropertyName);
29354+
if (!discriminant || getConstituentTypeForKeyType(type as UnionType, discriminant) !== t) {
29355+
return neverType;
29356+
}
29357+
}
2935029358
// For each constituent t in the current type, if t and and c are directly related, pick the most
2935129359
// specific of the two. When t and c are related in both directions, we prefer c for type predicates
2935229360
// because that is the asserted type, but t for `instanceof` because generics aren't reflected in

0 commit comments

Comments
 (0)