@@ -15831,6 +15831,7 @@ namespace ts {
15831
15831
function getFlowTypeOfReference(reference: Node, declaredType: Type, initialType = declaredType, flowContainer?: Node, couldBeUninitialized?: boolean) {
15832
15832
let key: string | undefined;
15833
15833
let flowDepth = 0;
15834
+ let firstDiscriminable: UnionType | undefined;
15834
15835
if (flowAnalysisDisabled) {
15835
15836
return errorType;
15836
15837
}
@@ -16174,20 +16175,14 @@ namespace ts {
16174
16175
return result;
16175
16176
}
16176
16177
16177
- function isMatchingInFlowDiscriminant(expr: Expression, computedType: Type) {
16178
- if (!isAccessExpression(expr) ) {
16179
- return false ;
16178
+ function getTypeToDiscriminate( computedType: Type): Type {
16179
+ if (firstDiscriminable ) {
16180
+ return firstDiscriminable ;
16180
16181
}
16181
- const name = getAccessedPropertyName(expr);
16182
- if (name === undefined) {
16183
- return false;
16182
+ if (declaredType.flags & TypeFlags.Union) {
16183
+ return firstDiscriminable = <UnionType>declaredType;
16184
16184
}
16185
- let propType;
16186
- return isMatchingReference(reference, expr.expression) &&
16187
- ((computedType.flags & TypeFlags.Union) ?
16188
- isDiscriminantProperty(computedType, name) :
16189
- (propType = getTypeOfPropertyOfType(computedType, name)) &&
16190
- isUnitType(propType));
16185
+ return (computedType.flags & TypeFlags.Union) ? (firstDiscriminable = <UnionType>computedType) : declaredType;
16191
16186
}
16192
16187
16193
16188
function isMatchingReferenceDiscriminant(expr: Expression, computedType: Type) {
@@ -16266,10 +16261,10 @@ namespace ts {
16266
16261
if (isMatchingReference(reference, right)) {
16267
16262
return narrowTypeByEquality(type, operator, left, assumeTrue);
16268
16263
}
16269
- if (isMatchingReferenceDiscriminant(left, declaredType) || isMatchingInFlowDiscriminant(left, type)) {
16264
+ if (isMatchingReferenceDiscriminant(left, getTypeToDiscriminate( type) )) {
16270
16265
return narrowTypeByDiscriminant(type, <AccessExpression>left, t => narrowTypeByEquality(t, operator, right, assumeTrue));
16271
16266
}
16272
- if (isMatchingReferenceDiscriminant(right, declaredType) || isMatchingInFlowDiscriminant(right, type)) {
16267
+ if (isMatchingReferenceDiscriminant(right, getTypeToDiscriminate( type) )) {
16273
16268
return narrowTypeByDiscriminant(type, <AccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
16274
16269
}
16275
16270
if (containsMatchingReferenceDiscriminant(reference, left) || containsMatchingReferenceDiscriminant(reference, right)) {
0 commit comments