diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4e8cb7cc340..30edecf3b40 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6275,37 +6275,19 @@ namespace ts { // Get the narrowed type of a given symbol at a given location function getNarrowedTypeOfSymbol(symbol: Symbol, node: Node) { let type = getTypeOfSymbol(symbol); - let originalType = type; // Only narrow when symbol is variable of type any or an object, union, or type parameter type if (node && symbol.flags & SymbolFlags.Variable) { if (isTypeAny(type) || type.flags & (TypeFlags.ObjectType | TypeFlags.Union | TypeFlags.TypeParameter)) { + let originalType = type; + let nodeStack: {node: Node, child: Node}[] = []; loop: while (node.parent) { let child = node; node = node.parent; - let narrowedType = type; switch (node.kind) { case SyntaxKind.IfStatement: - // In a branch of an if statement, narrow based on controlling expression - if (child !== (node).expression) { - narrowedType = narrowType(type, (node).expression, /*assumeTrue*/ child === (node).thenStatement); - } - break; case SyntaxKind.ConditionalExpression: - // In a branch of a conditional expression, narrow based on controlling condition - if (child !== (node).condition) { - narrowedType = narrowType(type, (node).condition, /*assumeTrue*/ child === (node).whenTrue); - } - break; case SyntaxKind.BinaryExpression: - // In the right operand of an && or ||, narrow based on left operand - if (child === (node).right) { - if ((node).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken) { - narrowedType = narrowType(type, (node).left, /*assumeTrue*/ true); - } - else if ((node).operatorToken.kind === SyntaxKind.BarBarToken) { - narrowedType = narrowType(type, (node).left, /*assumeTrue*/ false); - } - } + nodeStack.push({node, child}); break; case SyntaxKind.SourceFile: case SyntaxKind.ModuleDeclaration: @@ -6318,12 +6300,42 @@ namespace ts { // Stop at the first containing function or module declaration break loop; } - // Use narrowed type if construct contains no assignments to variable - if (narrowedType !== type) { - if (isVariableAssignedWithin(symbol, node)) { + } + + let nodes: {node: Node, child: Node}; + while (nodes = nodeStack.pop()) { + let {node, child} = nodes; + switch (node.kind) { + case SyntaxKind.IfStatement: + // In a branch of an if statement, narrow based on controlling expression + if (child !== (node).expression) { + type = narrowType(type, (node).expression, /*assumeTrue*/ child === (node).thenStatement); + } break; - } - type = narrowedType; + case SyntaxKind.ConditionalExpression: + // In a branch of a conditional expression, narrow based on controlling condition + if (child !== (node).condition) { + type = narrowType(type, (node).condition, /*assumeTrue*/ child === (node).whenTrue); + } + break; + case SyntaxKind.BinaryExpression: + // In the right operand of an && or ||, narrow based on left operand + if (child === (node).right) { + if ((node).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken) { + type = narrowType(type, (node).left, /*assumeTrue*/ true); + } + else if ((node).operatorToken.kind === SyntaxKind.BarBarToken) { + type = narrowType(type, (node).left, /*assumeTrue*/ false); + } + } + break; + default: + Debug.fail("Unreachable!"); + } + + // Use original type if construct contains assignments to variable + if (!nodeStack.length && isVariableAssignedWithin(symbol, node)) { + type = originalType; } } @@ -6365,9 +6377,13 @@ namespace ts { // At this point we can bail if it's not a union if (!(type.flags & TypeFlags.Union)) { // If the active non-union type would be removed from a union by this type guard, return an empty union - return (assumeTrue === !!(type.flags & flags)) ? type : getUnionType(emptyArray); + return filterUnion(type) ? type : getUnionType(emptyArray); + } + return getUnionType(filter((type as UnionType).types, filterUnion), /*noSubtypeReduction*/ true); + + function filterUnion(t: Type) { + return assumeTrue === !!(t.flags & flags); } - return getUnionType(filter((type as UnionType).types, t => assumeTrue === !!(t.flags & flags)), /*noSubtypeReduction*/ true); } function narrowTypeByAnd(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { diff --git a/tests/baselines/reference/typeGuardTautologicalConsistiency.types b/tests/baselines/reference/typeGuardTautologicalConsistiency.types index 9acb1464761..d758dcde22b 100644 --- a/tests/baselines/reference/typeGuardTautologicalConsistiency.types +++ b/tests/baselines/reference/typeGuardTautologicalConsistiency.types @@ -15,7 +15,7 @@ if (typeof stringOrNumber === "number") { >"number" : string stringOrNumber; ->stringOrNumber : string +>stringOrNumber : string | number } } @@ -31,6 +31,6 @@ if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") { >"number" : string stringOrNumber; ->stringOrNumber : number +>stringOrNumber : string | number }