@@ -16174,7 +16174,7 @@ namespace ts {
16174
16174
return result;
16175
16175
}
16176
16176
16177
- function isMatchingReferenceDiscriminant (expr: Expression, computedType: Type) {
16177
+ function isMatchingInFlowDiscriminant (expr: Expression, computedType: Type) {
16178
16178
if (!isAccessExpression(expr)) {
16179
16179
return false;
16180
16180
}
@@ -16184,10 +16184,21 @@ namespace ts {
16184
16184
}
16185
16185
let propType;
16186
16186
return isMatchingReference(reference, expr.expression) &&
16187
- (isDiscriminantProperty(computedType, name) ||
16188
- ((computedType.flags & TypeFlags.Union) === 0) &&
16189
- (propType = getTypeOfPropertyOfType(computedType, name))
16190
- && isUnitType(propType));
16187
+ ((computedType.flags & TypeFlags.Union) ?
16188
+ isDiscriminantProperty(computedType, name) :
16189
+ (propType = getTypeOfPropertyOfType(computedType, name)) &&
16190
+ isUnitType(propType));
16191
+ }
16192
+
16193
+ function isMatchingReferenceDiscriminant(expr: Expression, computedType: Type) {
16194
+ if (!(computedType.flags & TypeFlags.Union) || !isAccessExpression(expr)) {
16195
+ return false;
16196
+ }
16197
+ const name = getAccessedPropertyName(expr);
16198
+ if (name === undefined) {
16199
+ return false;
16200
+ }
16201
+ return isMatchingReference(reference, expr.expression) && isDiscriminantProperty(computedType, name);
16191
16202
}
16192
16203
16193
16204
function narrowTypeByDiscriminant(type: Type, access: AccessExpression, narrowType: (t: Type) => Type): Type {
@@ -16255,10 +16266,10 @@ namespace ts {
16255
16266
if (isMatchingReference(reference, right)) {
16256
16267
return narrowTypeByEquality(type, operator, left, assumeTrue);
16257
16268
}
16258
- if (isMatchingReferenceDiscriminant(left, type)) {
16269
+ if (isMatchingReferenceDiscriminant(left, declaredType) || isMatchingInFlowDiscriminant(left, type)) {
16259
16270
return narrowTypeByDiscriminant(type, <AccessExpression>left, t => narrowTypeByEquality(t, operator, right, assumeTrue));
16260
16271
}
16261
- if (isMatchingReferenceDiscriminant(right , type)) {
16272
+ if (isMatchingReferenceDiscriminant(left, declaredType) || isMatchingInFlowDiscriminant(left , type)) {
16262
16273
return narrowTypeByDiscriminant(type, <AccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
16263
16274
}
16264
16275
if (containsMatchingReferenceDiscriminant(reference, left) || containsMatchingReferenceDiscriminant(reference, right)) {
0 commit comments