Skip to content

Commit 2eecb4d

Browse files
committed
bring back discriminant logic
1 parent 5d3fe0a commit 2eecb4d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/compiler/checker.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -29342,9 +29342,17 @@ 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;
29346+
const discriminantMatchingType = keyPropertyName ?
29347+
mapType(candidate, c => {
29348+
const discriminant = keyPropertyName && getTypeOfPropertyOfType(c, keyPropertyName);
29349+
return (discriminant && getConstituentTypeForKeyType(type as UnionType, discriminant)) ?? neverType;
29350+
}) :
29351+
neverType;
2934529352
let matchedCandidates: Type[] = [];
29346-
let narrowedType = mapType(type, t =>
29347-
mapType(
29353+
let narrowedType = mapType(
29354+
!(discriminantMatchingType.flags & TypeFlags.Never) ? discriminantMatchingType : type,
29355+
t => mapType(
2934829356
candidate,
2934929357
c => {
2935029358
const directlyRelated = checkDerived ?
@@ -29355,7 +29363,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2935529363
}
2935629364
return directlyRelated;
2935729365
},
29358-
));
29366+
),
29367+
);
2935929368
if (matchedCandidates.length !== countTypes(candidate)) {
2936029369
narrowedType = getUnionType([
2936129370
narrowedType,

0 commit comments

Comments
 (0)