@@ -15831,6 +15831,7 @@ namespace ts {
1583115831 function getFlowTypeOfReference(reference: Node, declaredType: Type, initialType = declaredType, flowContainer?: Node, couldBeUninitialized?: boolean) {
1583215832 let key: string | undefined;
1583315833 let flowDepth = 0;
15834+ let firstDiscriminable: UnionType | undefined;
1583415835 if (flowAnalysisDisabled) {
1583515836 return errorType;
1583615837 }
@@ -16174,20 +16175,14 @@ namespace ts {
1617416175 return result;
1617516176 }
1617616177
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 ;
1618016181 }
16181- const name = getAccessedPropertyName(expr);
16182- if (name === undefined) {
16183- return false;
16182+ if (declaredType.flags & TypeFlags.Union) {
16183+ return firstDiscriminable = <UnionType>declaredType;
1618416184 }
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;
1619116186 }
1619216187
1619316188 function isMatchingReferenceDiscriminant(expr: Expression, computedType: Type) {
@@ -16266,10 +16261,10 @@ namespace ts {
1626616261 if (isMatchingReference(reference, right)) {
1626716262 return narrowTypeByEquality(type, operator, left, assumeTrue);
1626816263 }
16269- if (isMatchingReferenceDiscriminant(left, declaredType) || isMatchingInFlowDiscriminant(left, type)) {
16264+ if (isMatchingReferenceDiscriminant(left, getTypeToDiscriminate( type) )) {
1627016265 return narrowTypeByDiscriminant(type, <AccessExpression>left, t => narrowTypeByEquality(t, operator, right, assumeTrue));
1627116266 }
16272- if (isMatchingReferenceDiscriminant(right, declaredType) || isMatchingInFlowDiscriminant(right, type)) {
16267+ if (isMatchingReferenceDiscriminant(right, getTypeToDiscriminate( type) )) {
1627316268 return narrowTypeByDiscriminant(type, <AccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
1627416269 }
1627516270 if (containsMatchingReferenceDiscriminant(reference, left) || containsMatchingReferenceDiscriminant(reference, right)) {
0 commit comments