@@ -7343,9 +7343,9 @@ namespace ts {
7343
7343
// Remove those constituent types of declaredType to which no constituent type of assignedType is assignable.
7344
7344
// For example, when a variable of type number | string | boolean is assigned a value of type number | boolean,
7345
7345
// we remove type string.
7346
- function getAssignmentReducedType(declaredType: Type , assignedType: Type) {
7346
+ function getAssignmentReducedType(declaredType: UnionType , assignedType: Type) {
7347
7347
if (declaredType !== assignedType && declaredType.flags & TypeFlags.Union) {
7348
- const reducedTypes = filter((<UnionType> declaredType) .types, t => typeMaybeAssignableTo(assignedType, t));
7348
+ const reducedTypes = filter(declaredType.types, t => typeMaybeAssignableTo(assignedType, t));
7349
7349
if (reducedTypes.length) {
7350
7350
return reducedTypes.length === 1 ? reducedTypes[0] : getUnionType(reducedTypes);
7351
7351
}
@@ -7573,16 +7573,22 @@ namespace ts {
7573
7573
7574
7574
function getTypeAtFlowAssignment(flow: FlowAssignment) {
7575
7575
const node = flow.node;
7576
+ // Assignments only narrow the computed type if the declared type is a union type. Thus, we
7577
+ // only need to evaluate the assigned type if the declared type is a union type.
7576
7578
if ((node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement) &&
7577
7579
reference.kind === SyntaxKind.Identifier &&
7578
7580
getResolvedSymbol(<Identifier>reference) === getSymbolOfNode(node)) {
7579
- return getAssignmentReducedType(declaredType, getInitialType(<VariableDeclaration | BindingElement>node));
7581
+ return declaredType.flags & TypeFlags.Union ?
7582
+ getAssignmentReducedType(<UnionType>declaredType, getInitialType(<VariableDeclaration | BindingElement>node)) :
7583
+ declaredType;
7580
7584
}
7581
7585
// If the node is not a variable declaration or binding element, it is an identifier
7582
7586
// or a dotted name that is the target of an assignment. If we have a match, reduce
7583
7587
// the declared type by the assigned type.
7584
7588
if (isMatchingReference(reference, node)) {
7585
- return getAssignmentReducedType(declaredType, getAssignedType(<Expression>node));
7589
+ return declaredType.flags & TypeFlags.Union ?
7590
+ getAssignmentReducedType(<UnionType>declaredType, getAssignedType(<Expression>node)) :
7591
+ declaredType;
7586
7592
}
7587
7593
// We didn't have a direct match. However, if the reference is a dotted name, this
7588
7594
// may be an assignment to a left hand part of the reference. For example, for a
0 commit comments