From 062495c426e549ed68b5c75161e94e10be4ed06e Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 28 Oct 2015 13:48:32 -0700 Subject: [PATCH 1/8] naive change and new tests --- src/compiler/checker.ts | 6 +++--- .../expressions/typeGuards/typeGuardNesting.ts | 4 ++++ .../expressions/typeGuards/typeGuardRedundancy.ts | 9 +++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardRedundancy.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5370e7c4f027d..033614d2b419b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6369,7 +6369,7 @@ namespace ts { // Assumed result is true. If check was not for a primitive type, remove all primitive types if (!typeInfo) { return removeTypesFromUnionType(type, /*typeKind*/ TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.Boolean | TypeFlags.ESSymbol, - /*isOfTypeKind*/ true, /*allowEmptyUnionResult*/ false); + /*isOfTypeKind*/ true, /*allowEmptyUnionResult*/ true); } // Check was for a primitive type, return that primitive type if it is a subtype if (isTypeSubtypeOf(typeInfo.type, type)) { @@ -6377,12 +6377,12 @@ namespace ts { } // Otherwise, remove all types that aren't of the primitive type kind. This can happen when the type is // union of enum types and other types. - return removeTypesFromUnionType(type, /*typeKind*/ typeInfo.flags, /*isOfTypeKind*/ false, /*allowEmptyUnionResult*/ false); + return removeTypesFromUnionType(type, /*typeKind*/ typeInfo.flags, /*isOfTypeKind*/ false, /*allowEmptyUnionResult*/ true); } else { // Assumed result is false. If check was for a primitive type, remove that primitive type if (typeInfo) { - return removeTypesFromUnionType(type, /*typeKind*/ typeInfo.flags, /*isOfTypeKind*/ true, /*allowEmptyUnionResult*/ false); + return removeTypesFromUnionType(type, /*typeKind*/ typeInfo.flags, /*isOfTypeKind*/ true, /*allowEmptyUnionResult*/ true); } // Otherwise we don't have enough information to do anything. return type; diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts new file mode 100644 index 0000000000000..b06b5880800ec --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts @@ -0,0 +1,4 @@ +let strOrBool: string|boolean; +if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') { + var label: string = (typeof strOrBool === 'string') ? strOrBool : "other string"; +} \ No newline at end of file diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardRedundancy.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardRedundancy.ts new file mode 100644 index 0000000000000..4e9e3137d7871 --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardRedundancy.ts @@ -0,0 +1,9 @@ +var x: string|number; + +var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed; + +var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr; + +var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed; + +var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr; \ No newline at end of file From febda00f1b430761df7e0e90bbb99cf8c99e4653 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 28 Oct 2015 16:06:40 -0700 Subject: [PATCH 2/8] Improve type narrowing algorithm with typeof --- src/compiler/checker.ts | 35 +++---- tests/baselines/reference/symbolType18.types | 2 +- tests/baselines/reference/typeGuardNesting.js | 11 +++ .../reference/typeGuardNesting.symbols | 14 +++ .../reference/typeGuardNesting.types | 30 ++++++ .../typeGuardOfFormTypeOfOther.types | 12 +-- .../reference/typeGuardRedundancy.js | 17 ++++ .../reference/typeGuardRedundancy.symbols | 48 ++++++++++ .../reference/typeGuardRedundancy.types | 84 ++++++++++++++++ .../reference/typeGuardsWithAny.errors.txt | 49 ---------- .../reference/typeGuardsWithAny.symbols | 61 ++++++++++++ .../reference/typeGuardsWithAny.types | 96 +++++++++++++++++++ 12 files changed, 382 insertions(+), 77 deletions(-) create mode 100644 tests/baselines/reference/typeGuardNesting.js create mode 100644 tests/baselines/reference/typeGuardNesting.symbols create mode 100644 tests/baselines/reference/typeGuardNesting.types create mode 100644 tests/baselines/reference/typeGuardRedundancy.js create mode 100644 tests/baselines/reference/typeGuardRedundancy.symbols create mode 100644 tests/baselines/reference/typeGuardRedundancy.types delete mode 100644 tests/baselines/reference/typeGuardsWithAny.errors.txt create mode 100644 tests/baselines/reference/typeGuardsWithAny.symbols create mode 100644 tests/baselines/reference/typeGuardsWithAny.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 033614d2b419b..ad6e72d68bdd9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6338,6 +6338,10 @@ namespace ts { // Stop at the first containing function or module declaration break loop; } + // Preserve old top-level behavior - if the branch is really an empty set, revert to prior type + if (narrowedType === getUnionType(emptyArray)) { + narrowedType = type; + } // Use narrowed type if construct contains no assignments to variable if (narrowedType !== type) { if (isVariableAssignedWithin(symbol, node)) { @@ -6352,6 +6356,9 @@ namespace ts { return type; function narrowTypeByEquality(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { + if (!(type.flags & TypeFlags.Union)) { + return type; + } // Check that we have 'typeof ' on the left and string literal on the right if (expr.left.kind !== SyntaxKind.TypeOfExpression || expr.right.kind !== SyntaxKind.StringLiteral) { return type; @@ -6361,31 +6368,17 @@ namespace ts { if (left.expression.kind !== SyntaxKind.Identifier || getResolvedSymbol(left.expression) !== symbol) { return type; } - let typeInfo = primitiveTypeInfo[right.text]; if (expr.operatorToken.kind === SyntaxKind.ExclamationEqualsEqualsToken) { assumeTrue = !assumeTrue; } + let typeInfo = primitiveTypeInfo[right.text]; + let flags = typeInfo ? typeInfo.flags : (assumeTrue = !assumeTrue, TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol | TypeFlags.Boolean); + let union = type as UnionType; if (assumeTrue) { - // Assumed result is true. If check was not for a primitive type, remove all primitive types - if (!typeInfo) { - return removeTypesFromUnionType(type, /*typeKind*/ TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.Boolean | TypeFlags.ESSymbol, - /*isOfTypeKind*/ true, /*allowEmptyUnionResult*/ true); - } - // Check was for a primitive type, return that primitive type if it is a subtype - if (isTypeSubtypeOf(typeInfo.type, type)) { - return typeInfo.type; - } - // Otherwise, remove all types that aren't of the primitive type kind. This can happen when the type is - // union of enum types and other types. - return removeTypesFromUnionType(type, /*typeKind*/ typeInfo.flags, /*isOfTypeKind*/ false, /*allowEmptyUnionResult*/ true); + return getUnionType(filter(union.types, t => !!(t.flags & flags))); } else { - // Assumed result is false. If check was for a primitive type, remove that primitive type - if (typeInfo) { - return removeTypesFromUnionType(type, /*typeKind*/ typeInfo.flags, /*isOfTypeKind*/ true, /*allowEmptyUnionResult*/ true); - } - // Otherwise we don't have enough information to do anything. - return type; + return getUnionType(filter(union.types, t => !(t.flags & flags))); } } @@ -6399,7 +6392,7 @@ namespace ts { // and the second operand was false. We narrow with those assumptions and union the two resulting types. return getUnionType([ narrowType(type, expr.left, /*assumeTrue*/ false), - narrowType(narrowType(type, expr.left, /*assumeTrue*/ true), expr.right, /*assumeTrue*/ false) + narrowType(type, expr.right, /*assumeTrue*/ false) ]); } } @@ -6410,7 +6403,7 @@ namespace ts { // and the second operand was true. We narrow with those assumptions and union the two resulting types. return getUnionType([ narrowType(type, expr.left, /*assumeTrue*/ true), - narrowType(narrowType(type, expr.left, /*assumeTrue*/ false), expr.right, /*assumeTrue*/ true) + narrowType(type, expr.right, /*assumeTrue*/ true) ]); } else { diff --git a/tests/baselines/reference/symbolType18.types b/tests/baselines/reference/symbolType18.types index db4fb30378fca..68c43215136fa 100644 --- a/tests/baselines/reference/symbolType18.types +++ b/tests/baselines/reference/symbolType18.types @@ -21,5 +21,5 @@ if (typeof x === "object") { } else { x; ->x : symbol | Foo +>x : symbol } diff --git a/tests/baselines/reference/typeGuardNesting.js b/tests/baselines/reference/typeGuardNesting.js new file mode 100644 index 0000000000000..4c9a64c2851f5 --- /dev/null +++ b/tests/baselines/reference/typeGuardNesting.js @@ -0,0 +1,11 @@ +//// [typeGuardNesting.ts] +let strOrBool: string|boolean; +if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') { + var label: string = (typeof strOrBool === 'string') ? strOrBool : "other string"; +} + +//// [typeGuardNesting.js] +var strOrBool; +if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') { + var label = (typeof strOrBool === 'string') ? strOrBool : "other string"; +} diff --git a/tests/baselines/reference/typeGuardNesting.symbols b/tests/baselines/reference/typeGuardNesting.symbols new file mode 100644 index 0000000000000..f0cfb1f1eed6f --- /dev/null +++ b/tests/baselines/reference/typeGuardNesting.symbols @@ -0,0 +1,14 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts === +let strOrBool: string|boolean; +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + +if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') { +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + + var label: string = (typeof strOrBool === 'string') ? strOrBool : "other string"; +>label : Symbol(label, Decl(typeGuardNesting.ts, 2, 4)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +} diff --git a/tests/baselines/reference/typeGuardNesting.types b/tests/baselines/reference/typeGuardNesting.types new file mode 100644 index 0000000000000..9f79974635dff --- /dev/null +++ b/tests/baselines/reference/typeGuardNesting.types @@ -0,0 +1,30 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts === +let strOrBool: string|boolean; +>strOrBool : string | boolean + +if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') { +>(typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string' : boolean +>(typeof strOrBool === 'boolean' && !strOrBool) : boolean +>typeof strOrBool === 'boolean' && !strOrBool : boolean +>typeof strOrBool === 'boolean' : boolean +>typeof strOrBool : string +>strOrBool : string | boolean +>'boolean' : string +>!strOrBool : boolean +>strOrBool : boolean +>typeof strOrBool === 'string' : boolean +>typeof strOrBool : string +>strOrBool : string | boolean +>'string' : string + + var label: string = (typeof strOrBool === 'string') ? strOrBool : "other string"; +>label : string +>(typeof strOrBool === 'string') ? strOrBool : "other string" : string +>(typeof strOrBool === 'string') : boolean +>typeof strOrBool === 'string' : boolean +>typeof strOrBool : string +>strOrBool : boolean | string +>'string' : string +>strOrBool : string +>"other string" : string +} diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfOther.types b/tests/baselines/reference/typeGuardOfFormTypeOfOther.types index e0fd443ef6391..8150c3ec25d5a 100644 --- a/tests/baselines/reference/typeGuardOfFormTypeOfOther.types +++ b/tests/baselines/reference/typeGuardOfFormTypeOfOther.types @@ -63,7 +63,7 @@ else { var r2: string | C = strOrC; // string | C >r2 : string | C >C : C ->strOrC : string | C +>strOrC : string } if (typeof numOrC === "Object") { >typeof numOrC === "Object" : boolean @@ -80,7 +80,7 @@ else { var r3: number | C = numOrC; // number | C >r3 : number | C >C : C ->numOrC : number | C +>numOrC : number } if (typeof boolOrC === "Object") { >typeof boolOrC === "Object" : boolean @@ -97,7 +97,7 @@ else { var r4: boolean | C = boolOrC; // boolean | C >r4 : boolean | C >C : C ->boolOrC : boolean | C +>boolOrC : boolean } // Narrowing occurs only if target type is a subtype of variable type @@ -129,7 +129,7 @@ if (typeof strOrC !== "Object") { var r2: string | C = strOrC; // string | C >r2 : string | C >C : C ->strOrC : string | C +>strOrC : string } else { c = strOrC; // C @@ -146,7 +146,7 @@ if (typeof numOrC !== "Object") { var r3: number | C = numOrC; // number | C >r3 : number | C >C : C ->numOrC : number | C +>numOrC : number } else { c = numOrC; // C @@ -163,7 +163,7 @@ if (typeof boolOrC !== "Object") { var r4: boolean | C = boolOrC; // boolean | C >r4 : boolean | C >C : C ->boolOrC : boolean | C +>boolOrC : boolean } else { c = boolOrC; // C diff --git a/tests/baselines/reference/typeGuardRedundancy.js b/tests/baselines/reference/typeGuardRedundancy.js new file mode 100644 index 0000000000000..1dfa964cbc981 --- /dev/null +++ b/tests/baselines/reference/typeGuardRedundancy.js @@ -0,0 +1,17 @@ +//// [typeGuardRedundancy.ts] +var x: string|number; + +var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed; + +var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr; + +var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed; + +var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr; + +//// [typeGuardRedundancy.js] +var x; +var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed; +var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr; +var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed; +var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr; diff --git a/tests/baselines/reference/typeGuardRedundancy.symbols b/tests/baselines/reference/typeGuardRedundancy.symbols new file mode 100644 index 0000000000000..356061407ad6c --- /dev/null +++ b/tests/baselines/reference/typeGuardRedundancy.symbols @@ -0,0 +1,48 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardRedundancy.ts === +var x: string|number; +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) + +var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed; +>r1 : Symbol(r1, Decl(typeGuardRedundancy.ts, 2, 3)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>substr : Symbol(String.substr, Decl(lib.d.ts, --, --)) +>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --)) + +var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr; +>r2 : Symbol(r2, Decl(typeGuardRedundancy.ts, 4, 3)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --)) +>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>substr : Symbol(String.substr, Decl(lib.d.ts, --, --)) + +var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed; +>r3 : Symbol(r3, Decl(typeGuardRedundancy.ts, 6, 3)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>substr : Symbol(String.substr, Decl(lib.d.ts, --, --)) +>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --)) + +var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr; +>r4 : Symbol(r4, Decl(typeGuardRedundancy.ts, 8, 3)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>x.toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>toFixed : Symbol(Number.toFixed, Decl(lib.d.ts, --, --)) +>x.substr : Symbol(String.substr, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(typeGuardRedundancy.ts, 0, 3)) +>substr : Symbol(String.substr, Decl(lib.d.ts, --, --)) + diff --git a/tests/baselines/reference/typeGuardRedundancy.types b/tests/baselines/reference/typeGuardRedundancy.types new file mode 100644 index 0000000000000..1507ceb850ef3 --- /dev/null +++ b/tests/baselines/reference/typeGuardRedundancy.types @@ -0,0 +1,84 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardRedundancy.ts === +var x: string|number; +>x : string | number + +var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed; +>r1 : (from: number, length?: number) => string +>typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed : (from: number, length?: number) => string +>typeof x === "string" && typeof x === "string" : boolean +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string +>typeof x === "string" : boolean +>typeof x : string +>x : string +>"string" : string +>x.substr : (from: number, length?: number) => string +>x : string +>substr : (from: number, length?: number) => string +>x.toFixed : (fractionDigits?: number) => string +>x : number +>toFixed : (fractionDigits?: number) => string + +var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr; +>r2 : (fractionDigits?: number) => string +>!(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr : (fractionDigits?: number) => string +>!(typeof x === "string" && typeof x === "string") : boolean +>(typeof x === "string" && typeof x === "string") : boolean +>typeof x === "string" && typeof x === "string" : boolean +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string +>typeof x === "string" : boolean +>typeof x : string +>x : string +>"string" : string +>x.toFixed : (fractionDigits?: number) => string +>x : number +>toFixed : (fractionDigits?: number) => string +>x.substr : (from: number, length?: number) => string +>x : string +>substr : (from: number, length?: number) => string + +var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed; +>r3 : (from: number, length?: number) => string +>typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed : (from: number, length?: number) => string +>typeof x === "string" || typeof x === "string" : boolean +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string +>typeof x === "string" : boolean +>typeof x : string +>x : number +>"string" : string +>x.substr : (from: number, length?: number) => string +>x : string +>substr : (from: number, length?: number) => string +>x.toFixed : (fractionDigits?: number) => string +>x : number +>toFixed : (fractionDigits?: number) => string + +var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr; +>r4 : (fractionDigits?: number) => string +>!(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr : (fractionDigits?: number) => string +>!(typeof x === "string" || typeof x === "string") : boolean +>(typeof x === "string" || typeof x === "string") : boolean +>typeof x === "string" || typeof x === "string" : boolean +>typeof x === "string" : boolean +>typeof x : string +>x : string | number +>"string" : string +>typeof x === "string" : boolean +>typeof x : string +>x : number +>"string" : string +>x.toFixed : (fractionDigits?: number) => string +>x : number +>toFixed : (fractionDigits?: number) => string +>x.substr : (from: number, length?: number) => string +>x : string +>substr : (from: number, length?: number) => string + diff --git a/tests/baselines/reference/typeGuardsWithAny.errors.txt b/tests/baselines/reference/typeGuardsWithAny.errors.txt deleted file mode 100644 index 653c89c7554ad..0000000000000 --- a/tests/baselines/reference/typeGuardsWithAny.errors.txt +++ /dev/null @@ -1,49 +0,0 @@ -tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(11,7): error TS2339: Property 'p' does not exist on type 'string'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(18,7): error TS2339: Property 'p' does not exist on type 'number'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(25,7): error TS2339: Property 'p' does not exist on type 'boolean'. - - -==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts (3 errors) ==== - var x: any = { p: 0 }; - - if (x instanceof Object) { - x.p; // No error, type any unaffected by instanceof type guard - } - else { - x.p; // No error, type any unaffected by instanceof type guard - } - - if (typeof x === "string") { - x.p; // Error, type any narrowed by primitive type check - ~ -!!! error TS2339: Property 'p' does not exist on type 'string'. - } - else { - x.p; // No error, type unaffected in this branch - } - - if (typeof x === "number") { - x.p; // Error, type any narrowed by primitive type check - ~ -!!! error TS2339: Property 'p' does not exist on type 'number'. - } - else { - x.p; // No error, type unaffected in this branch - } - - if (typeof x === "boolean") { - x.p; // Error, type any narrowed by primitive type check - ~ -!!! error TS2339: Property 'p' does not exist on type 'boolean'. - } - else { - x.p; // No error, type unaffected in this branch - } - - if (typeof x === "object") { - x.p; // No error, type any only affected by primitive type check - } - else { - x.p; // No error, type unaffected in this branch - } - \ No newline at end of file diff --git a/tests/baselines/reference/typeGuardsWithAny.symbols b/tests/baselines/reference/typeGuardsWithAny.symbols new file mode 100644 index 0000000000000..90405d762a1d1 --- /dev/null +++ b/tests/baselines/reference/typeGuardsWithAny.symbols @@ -0,0 +1,61 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts === +var x: any = { p: 0 }; +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +>p : Symbol(p, Decl(typeGuardsWithAny.ts, 0, 14)) + +if (x instanceof Object) { +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + + x.p; // No error, type any unaffected by instanceof type guard +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} +else { + x.p; // No error, type any unaffected by instanceof type guard +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} + +if (typeof x === "string") { +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) + + x.p; // Error, type any narrowed by primitive type check +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} +else { + x.p; // No error, type unaffected in this branch +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} + +if (typeof x === "number") { +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) + + x.p; // Error, type any narrowed by primitive type check +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} +else { + x.p; // No error, type unaffected in this branch +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} + +if (typeof x === "boolean") { +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) + + x.p; // Error, type any narrowed by primitive type check +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} +else { + x.p; // No error, type unaffected in this branch +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} + +if (typeof x === "object") { +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) + + x.p; // No error, type any only affected by primitive type check +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} +else { + x.p; // No error, type unaffected in this branch +>x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) +} + diff --git a/tests/baselines/reference/typeGuardsWithAny.types b/tests/baselines/reference/typeGuardsWithAny.types new file mode 100644 index 0000000000000..12a4326e99db8 --- /dev/null +++ b/tests/baselines/reference/typeGuardsWithAny.types @@ -0,0 +1,96 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts === +var x: any = { p: 0 }; +>x : any +>{ p: 0 } : { p: number; } +>p : number +>0 : number + +if (x instanceof Object) { +>x instanceof Object : boolean +>x : any +>Object : ObjectConstructor + + x.p; // No error, type any unaffected by instanceof type guard +>x.p : any +>x : any +>p : any +} +else { + x.p; // No error, type any unaffected by instanceof type guard +>x.p : any +>x : any +>p : any +} + +if (typeof x === "string") { +>typeof x === "string" : boolean +>typeof x : string +>x : any +>"string" : string + + x.p; // Error, type any narrowed by primitive type check +>x.p : any +>x : any +>p : any +} +else { + x.p; // No error, type unaffected in this branch +>x.p : any +>x : any +>p : any +} + +if (typeof x === "number") { +>typeof x === "number" : boolean +>typeof x : string +>x : any +>"number" : string + + x.p; // Error, type any narrowed by primitive type check +>x.p : any +>x : any +>p : any +} +else { + x.p; // No error, type unaffected in this branch +>x.p : any +>x : any +>p : any +} + +if (typeof x === "boolean") { +>typeof x === "boolean" : boolean +>typeof x : string +>x : any +>"boolean" : string + + x.p; // Error, type any narrowed by primitive type check +>x.p : any +>x : any +>p : any +} +else { + x.p; // No error, type unaffected in this branch +>x.p : any +>x : any +>p : any +} + +if (typeof x === "object") { +>typeof x === "object" : boolean +>typeof x : string +>x : any +>"object" : string + + x.p; // No error, type any only affected by primitive type check +>x.p : any +>x : any +>p : any +} +else { + x.p; // No error, type unaffected in this branch +>x.p : any +>x : any +>p : any +} + From 168c664639a6c7d01439f39b1d3bd3edd58bada3 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 28 Oct 2015 16:29:36 -0700 Subject: [PATCH 3/8] restore any narrowing --- src/compiler/checker.ts | 11 ++- .../reference/typeGuardsWithAny.errors.txt | 49 ++++++++++ .../reference/typeGuardsWithAny.symbols | 61 ------------ .../reference/typeGuardsWithAny.types | 96 ------------------- 4 files changed, 57 insertions(+), 160 deletions(-) create mode 100644 tests/baselines/reference/typeGuardsWithAny.errors.txt delete mode 100644 tests/baselines/reference/typeGuardsWithAny.symbols delete mode 100644 tests/baselines/reference/typeGuardsWithAny.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ad6e72d68bdd9..93b644b07a4b0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6356,9 +6356,6 @@ namespace ts { return type; function narrowTypeByEquality(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { - if (!(type.flags & TypeFlags.Union)) { - return type; - } // Check that we have 'typeof ' on the left and string literal on the right if (expr.left.kind !== SyntaxKind.TypeOfExpression || expr.right.kind !== SyntaxKind.StringLiteral) { return type; @@ -6372,6 +6369,14 @@ namespace ts { assumeTrue = !assumeTrue; } let typeInfo = primitiveTypeInfo[right.text]; + // If the type to be narrowed is any and we're affirmatively checking against a primitive, return the primitive + if (!!(type.flags & TypeFlags.Any) && typeInfo && assumeTrue) { + return typeInfo.type; + } + // At this point we can bail if it's not a union + if (!(type.flags & TypeFlags.Union)) { + return type; + } let flags = typeInfo ? typeInfo.flags : (assumeTrue = !assumeTrue, TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol | TypeFlags.Boolean); let union = type as UnionType; if (assumeTrue) { diff --git a/tests/baselines/reference/typeGuardsWithAny.errors.txt b/tests/baselines/reference/typeGuardsWithAny.errors.txt new file mode 100644 index 0000000000000..653c89c7554ad --- /dev/null +++ b/tests/baselines/reference/typeGuardsWithAny.errors.txt @@ -0,0 +1,49 @@ +tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(11,7): error TS2339: Property 'p' does not exist on type 'string'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(18,7): error TS2339: Property 'p' does not exist on type 'number'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(25,7): error TS2339: Property 'p' does not exist on type 'boolean'. + + +==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts (3 errors) ==== + var x: any = { p: 0 }; + + if (x instanceof Object) { + x.p; // No error, type any unaffected by instanceof type guard + } + else { + x.p; // No error, type any unaffected by instanceof type guard + } + + if (typeof x === "string") { + x.p; // Error, type any narrowed by primitive type check + ~ +!!! error TS2339: Property 'p' does not exist on type 'string'. + } + else { + x.p; // No error, type unaffected in this branch + } + + if (typeof x === "number") { + x.p; // Error, type any narrowed by primitive type check + ~ +!!! error TS2339: Property 'p' does not exist on type 'number'. + } + else { + x.p; // No error, type unaffected in this branch + } + + if (typeof x === "boolean") { + x.p; // Error, type any narrowed by primitive type check + ~ +!!! error TS2339: Property 'p' does not exist on type 'boolean'. + } + else { + x.p; // No error, type unaffected in this branch + } + + if (typeof x === "object") { + x.p; // No error, type any only affected by primitive type check + } + else { + x.p; // No error, type unaffected in this branch + } + \ No newline at end of file diff --git a/tests/baselines/reference/typeGuardsWithAny.symbols b/tests/baselines/reference/typeGuardsWithAny.symbols deleted file mode 100644 index 90405d762a1d1..0000000000000 --- a/tests/baselines/reference/typeGuardsWithAny.symbols +++ /dev/null @@ -1,61 +0,0 @@ -=== tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts === -var x: any = { p: 0 }; ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) ->p : Symbol(p, Decl(typeGuardsWithAny.ts, 0, 14)) - -if (x instanceof Object) { ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) ->Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) - - x.p; // No error, type any unaffected by instanceof type guard ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} -else { - x.p; // No error, type any unaffected by instanceof type guard ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} - -if (typeof x === "string") { ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) - - x.p; // Error, type any narrowed by primitive type check ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} -else { - x.p; // No error, type unaffected in this branch ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} - -if (typeof x === "number") { ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) - - x.p; // Error, type any narrowed by primitive type check ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} -else { - x.p; // No error, type unaffected in this branch ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} - -if (typeof x === "boolean") { ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) - - x.p; // Error, type any narrowed by primitive type check ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} -else { - x.p; // No error, type unaffected in this branch ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} - -if (typeof x === "object") { ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) - - x.p; // No error, type any only affected by primitive type check ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} -else { - x.p; // No error, type unaffected in this branch ->x : Symbol(x, Decl(typeGuardsWithAny.ts, 0, 3)) -} - diff --git a/tests/baselines/reference/typeGuardsWithAny.types b/tests/baselines/reference/typeGuardsWithAny.types deleted file mode 100644 index 12a4326e99db8..0000000000000 --- a/tests/baselines/reference/typeGuardsWithAny.types +++ /dev/null @@ -1,96 +0,0 @@ -=== tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts === -var x: any = { p: 0 }; ->x : any ->{ p: 0 } : { p: number; } ->p : number ->0 : number - -if (x instanceof Object) { ->x instanceof Object : boolean ->x : any ->Object : ObjectConstructor - - x.p; // No error, type any unaffected by instanceof type guard ->x.p : any ->x : any ->p : any -} -else { - x.p; // No error, type any unaffected by instanceof type guard ->x.p : any ->x : any ->p : any -} - -if (typeof x === "string") { ->typeof x === "string" : boolean ->typeof x : string ->x : any ->"string" : string - - x.p; // Error, type any narrowed by primitive type check ->x.p : any ->x : any ->p : any -} -else { - x.p; // No error, type unaffected in this branch ->x.p : any ->x : any ->p : any -} - -if (typeof x === "number") { ->typeof x === "number" : boolean ->typeof x : string ->x : any ->"number" : string - - x.p; // Error, type any narrowed by primitive type check ->x.p : any ->x : any ->p : any -} -else { - x.p; // No error, type unaffected in this branch ->x.p : any ->x : any ->p : any -} - -if (typeof x === "boolean") { ->typeof x === "boolean" : boolean ->typeof x : string ->x : any ->"boolean" : string - - x.p; // Error, type any narrowed by primitive type check ->x.p : any ->x : any ->p : any -} -else { - x.p; // No error, type unaffected in this branch ->x.p : any ->x : any ->p : any -} - -if (typeof x === "object") { ->typeof x === "object" : boolean ->typeof x : string ->x : any ->"object" : string - - x.p; // No error, type any only affected by primitive type check ->x.p : any ->x : any ->p : any -} -else { - x.p; // No error, type unaffected in this branch ->x.p : any ->x : any ->p : any -} - From 9f12bc8a1b0d100e87d336d8303fe5cf9fe09045 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 29 Oct 2015 17:51:36 -0700 Subject: [PATCH 4/8] feedback from pr, new tests --- src/compiler/checker.ts | 61 +++++------ tests/baselines/reference/typeGuardEnums.js | 41 +++++++ .../reference/typeGuardEnums.symbols | 34 ++++++ .../baselines/reference/typeGuardEnums.types | 40 +++++++ tests/baselines/reference/typeGuardNesting.js | 26 ++++- .../reference/typeGuardNesting.symbols | 44 +++++++- .../reference/typeGuardNesting.types | 100 +++++++++++++++++- .../reference/typeGuardOfFormTypeOfOther.js | 24 ++--- .../typeGuardOfFormTypeOfOther.symbols | 18 ++-- .../typeGuardOfFormTypeOfOther.types | 30 +++--- .../typeGuardTautologicalConsistiency.js | 24 +++++ .../typeGuardTautologicalConsistiency.symbols | 23 ++++ .../typeGuardTautologicalConsistiency.types | 36 +++++++ .../expressions/typeGuards/typeGuardEnums.ts | 18 ++++ .../typeGuards/typeGuardNesting.ts | 14 ++- .../typeGuards/typeGuardOfFormTypeOfOther.ts | 12 +-- .../typeGuardTautologicalConsistiency.ts | 11 ++ 17 files changed, 463 insertions(+), 93 deletions(-) create mode 100644 tests/baselines/reference/typeGuardEnums.js create mode 100644 tests/baselines/reference/typeGuardEnums.symbols create mode 100644 tests/baselines/reference/typeGuardEnums.types create mode 100644 tests/baselines/reference/typeGuardTautologicalConsistiency.js create mode 100644 tests/baselines/reference/typeGuardTautologicalConsistiency.symbols create mode 100644 tests/baselines/reference/typeGuardTautologicalConsistiency.types create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardEnums.ts create mode 100644 tests/cases/conformance/expressions/typeGuards/typeGuardTautologicalConsistiency.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 93b644b07a4b0..4e8cb7cc340f6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6177,27 +6177,6 @@ namespace ts { Debug.fail("should not get here"); } - // For a union type, remove all constituent types that are of the given type kind (when isOfTypeKind is true) - // or not of the given type kind (when isOfTypeKind is false) - function removeTypesFromUnionType(type: Type, typeKind: TypeFlags, isOfTypeKind: boolean, allowEmptyUnionResult: boolean): Type { - if (type.flags & TypeFlags.Union) { - let types = (type).types; - if (forEach(types, t => !!(t.flags & typeKind) === isOfTypeKind)) { - // Above we checked if we have anything to remove, now use the opposite test to do the removal - let narrowedType = getUnionType(filter(types, t => !(t.flags & typeKind) === isOfTypeKind)); - if (allowEmptyUnionResult || narrowedType !== emptyObjectType) { - return narrowedType; - } - } - } - else if (allowEmptyUnionResult && !!(type.flags & typeKind) === isOfTypeKind) { - // Use getUnionType(emptyArray) instead of emptyObjectType in case the way empty union types - // are represented ever changes. - return getUnionType(emptyArray); - } - return type; - } - function hasInitializer(node: VariableLikeDeclaration): boolean { return !!(node.initializer || isBindingPattern(node.parent) && hasInitializer(node.parent.parent)); } @@ -6296,6 +6275,7 @@ 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)) { @@ -6338,10 +6318,6 @@ namespace ts { // Stop at the first containing function or module declaration break loop; } - // Preserve old top-level behavior - if the branch is really an empty set, revert to prior type - if (narrowedType === getUnionType(emptyArray)) { - narrowedType = type; - } // Use narrowed type if construct contains no assignments to variable if (narrowedType !== type) { if (isVariableAssignedWithin(symbol, node)) { @@ -6350,6 +6326,11 @@ namespace ts { type = narrowedType; } } + + // Preserve old top-level behavior - if the branch is really an empty set, revert to prior type + if (type === getUnionType(emptyArray)) { + type = originalType; + } } } @@ -6369,22 +6350,24 @@ namespace ts { assumeTrue = !assumeTrue; } let typeInfo = primitiveTypeInfo[right.text]; - // If the type to be narrowed is any and we're affirmatively checking against a primitive, return the primitive + // If the type to be narrowed is any and we're checking a primitive with assumeTrue=true, return the primitive if (!!(type.flags & TypeFlags.Any) && typeInfo && assumeTrue) { return typeInfo.type; } - // At this point we can bail if it's not a union - if (!(type.flags & TypeFlags.Union)) { - return type; - } - let flags = typeInfo ? typeInfo.flags : (assumeTrue = !assumeTrue, TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol | TypeFlags.Boolean); - let union = type as UnionType; - if (assumeTrue) { - return getUnionType(filter(union.types, t => !!(t.flags & flags))); + let flags: TypeFlags; + if (typeInfo) { + flags = typeInfo.flags; } else { - return getUnionType(filter(union.types, t => !(t.flags & flags))); + assumeTrue = !assumeTrue; + flags = TypeFlags.NumberLike | TypeFlags.StringLike | TypeFlags.ESSymbol | TypeFlags.Boolean; } + // 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 getUnionType(filter((type as UnionType).types, t => assumeTrue === !!(t.flags & flags)), /*noSubtypeReduction*/ true); } function narrowTypeByAnd(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type { @@ -12554,7 +12537,13 @@ namespace ts { // After we remove all types that are StringLike, we will know if there was a string constituent // based on whether the remaining type is the same as the initial type. - let arrayType = removeTypesFromUnionType(arrayOrStringType, TypeFlags.StringLike, /*isTypeOfKind*/ true, /*allowEmptyUnionResult*/ true); + let arrayType = arrayOrStringType; + if (arrayOrStringType.flags & TypeFlags.Union) { + arrayType = getUnionType(filter((arrayOrStringType as UnionType).types, t => !(t.flags & TypeFlags.StringLike))); + } + else if (arrayOrStringType.flags & TypeFlags.StringLike) { + arrayType = getUnionType(emptyArray); + } let hasStringConstituent = arrayOrStringType !== arrayType; let reportedError = false; diff --git a/tests/baselines/reference/typeGuardEnums.js b/tests/baselines/reference/typeGuardEnums.js new file mode 100644 index 0000000000000..1eb4b7552c4c9 --- /dev/null +++ b/tests/baselines/reference/typeGuardEnums.js @@ -0,0 +1,41 @@ +//// [typeGuardEnums.ts] +enum E {} +enum V {} + +let x: number|string|E|V; + +if (typeof x === "number") { + x; // number|E|V +} +else { + x; // string +} + +if (typeof x !== "number") { + x; // string +} +else { + x; // number|E|V +} + + +//// [typeGuardEnums.js] +var E; +(function (E) { +})(E || (E = {})); +var V; +(function (V) { +})(V || (V = {})); +var x; +if (typeof x === "number") { + x; // number|E|V +} +else { + x; // string +} +if (typeof x !== "number") { + x; // string +} +else { + x; // number|E|V +} diff --git a/tests/baselines/reference/typeGuardEnums.symbols b/tests/baselines/reference/typeGuardEnums.symbols new file mode 100644 index 0000000000000..8f1a396c481a1 --- /dev/null +++ b/tests/baselines/reference/typeGuardEnums.symbols @@ -0,0 +1,34 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardEnums.ts === +enum E {} +>E : Symbol(E, Decl(typeGuardEnums.ts, 0, 0)) + +enum V {} +>V : Symbol(V, Decl(typeGuardEnums.ts, 0, 9)) + +let x: number|string|E|V; +>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3)) +>E : Symbol(E, Decl(typeGuardEnums.ts, 0, 0)) +>V : Symbol(V, Decl(typeGuardEnums.ts, 0, 9)) + +if (typeof x === "number") { +>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3)) + + x; // number|E|V +>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3)) +} +else { + x; // string +>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3)) +} + +if (typeof x !== "number") { +>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3)) + + x; // string +>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3)) +} +else { + x; // number|E|V +>x : Symbol(x, Decl(typeGuardEnums.ts, 3, 3)) +} + diff --git a/tests/baselines/reference/typeGuardEnums.types b/tests/baselines/reference/typeGuardEnums.types new file mode 100644 index 0000000000000..1d39a81d78acd --- /dev/null +++ b/tests/baselines/reference/typeGuardEnums.types @@ -0,0 +1,40 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardEnums.ts === +enum E {} +>E : E + +enum V {} +>V : V + +let x: number|string|E|V; +>x : number | string | E | V +>E : E +>V : V + +if (typeof x === "number") { +>typeof x === "number" : boolean +>typeof x : string +>x : number | string | E | V +>"number" : string + + x; // number|E|V +>x : number | E | V +} +else { + x; // string +>x : string +} + +if (typeof x !== "number") { +>typeof x !== "number" : boolean +>typeof x : string +>x : number | string | E | V +>"number" : string + + x; // string +>x : string +} +else { + x; // number|E|V +>x : number | E | V +} + diff --git a/tests/baselines/reference/typeGuardNesting.js b/tests/baselines/reference/typeGuardNesting.js index 4c9a64c2851f5..364653d02738c 100644 --- a/tests/baselines/reference/typeGuardNesting.js +++ b/tests/baselines/reference/typeGuardNesting.js @@ -1,11 +1,31 @@ //// [typeGuardNesting.ts] let strOrBool: string|boolean; if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') { - var label: string = (typeof strOrBool === 'string') ? strOrBool : "other string"; -} + let label: string = (typeof strOrBool === 'string') ? strOrBool : "string"; + let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false; + let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; + let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false; +} + +if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') { + let label: string = (typeof strOrBool === 'string') ? strOrBool : "string"; + let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false; + let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; + let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false; +} + //// [typeGuardNesting.js] var strOrBool; if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') { - var label = (typeof strOrBool === 'string') ? strOrBool : "other string"; + var label = (typeof strOrBool === 'string') ? strOrBool : "string"; + var bool = (typeof strOrBool === 'boolean') ? strOrBool : false; + var label2 = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; + var bool2 = (typeof strOrBool !== 'string') ? strOrBool : false; +} +if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') { + var label = (typeof strOrBool === 'string') ? strOrBool : "string"; + var bool = (typeof strOrBool === 'boolean') ? strOrBool : false; + var label2 = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; + var bool2 = (typeof strOrBool !== 'string') ? strOrBool : false; } diff --git a/tests/baselines/reference/typeGuardNesting.symbols b/tests/baselines/reference/typeGuardNesting.symbols index f0cfb1f1eed6f..427db81f3ed56 100644 --- a/tests/baselines/reference/typeGuardNesting.symbols +++ b/tests/baselines/reference/typeGuardNesting.symbols @@ -7,8 +7,50 @@ if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'stri >strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) >strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) - var label: string = (typeof strOrBool === 'string') ? strOrBool : "other string"; + let label: string = (typeof strOrBool === 'string') ? strOrBool : "string"; >label : Symbol(label, Decl(typeGuardNesting.ts, 2, 4)) >strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + + let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false; +>bool : Symbol(bool, Decl(typeGuardNesting.ts, 3, 4)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + + let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; +>label2 : Symbol(label2, Decl(typeGuardNesting.ts, 4, 4)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + + let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false; +>bool2 : Symbol(bool2, Decl(typeGuardNesting.ts, 5, 4)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) >strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) } + +if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') { +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + + let label: string = (typeof strOrBool === 'string') ? strOrBool : "string"; +>label : Symbol(label, Decl(typeGuardNesting.ts, 9, 4)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + + let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false; +>bool : Symbol(bool, Decl(typeGuardNesting.ts, 10, 4)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + + let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; +>label2 : Symbol(label2, Decl(typeGuardNesting.ts, 11, 4)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) + + let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false; +>bool2 : Symbol(bool2, Decl(typeGuardNesting.ts, 12, 4)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +>strOrBool : Symbol(strOrBool, Decl(typeGuardNesting.ts, 0, 3)) +} + diff --git a/tests/baselines/reference/typeGuardNesting.types b/tests/baselines/reference/typeGuardNesting.types index 9f79974635dff..255e96da89eb8 100644 --- a/tests/baselines/reference/typeGuardNesting.types +++ b/tests/baselines/reference/typeGuardNesting.types @@ -17,14 +17,108 @@ if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'stri >strOrBool : string | boolean >'string' : string - var label: string = (typeof strOrBool === 'string') ? strOrBool : "other string"; + let label: string = (typeof strOrBool === 'string') ? strOrBool : "string"; >label : string ->(typeof strOrBool === 'string') ? strOrBool : "other string" : string +>(typeof strOrBool === 'string') ? strOrBool : "string" : string >(typeof strOrBool === 'string') : boolean >typeof strOrBool === 'string' : boolean >typeof strOrBool : string >strOrBool : boolean | string >'string' : string >strOrBool : string ->"other string" : string +>"string" : string + + let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false; +>bool : boolean +>(typeof strOrBool === 'boolean') ? strOrBool : false : boolean +>(typeof strOrBool === 'boolean') : boolean +>typeof strOrBool === 'boolean' : boolean +>typeof strOrBool : string +>strOrBool : boolean | string +>'boolean' : string +>strOrBool : boolean +>false : boolean + + let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; +>label2 : string +>(typeof strOrBool !== 'boolean') ? strOrBool : "string" : string +>(typeof strOrBool !== 'boolean') : boolean +>typeof strOrBool !== 'boolean' : boolean +>typeof strOrBool : string +>strOrBool : boolean | string +>'boolean' : string +>strOrBool : string +>"string" : string + + let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false; +>bool2 : boolean +>(typeof strOrBool !== 'string') ? strOrBool : false : boolean +>(typeof strOrBool !== 'string') : boolean +>typeof strOrBool !== 'string' : boolean +>typeof strOrBool : string +>strOrBool : boolean | string +>'string' : string +>strOrBool : boolean +>false : boolean } + +if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') { +>(typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean' : boolean +>(typeof strOrBool !== 'string' && !strOrBool) : boolean +>typeof strOrBool !== 'string' && !strOrBool : boolean +>typeof strOrBool !== 'string' : boolean +>typeof strOrBool : string +>strOrBool : string | boolean +>'string' : string +>!strOrBool : boolean +>strOrBool : boolean +>typeof strOrBool !== 'boolean' : boolean +>typeof strOrBool : string +>strOrBool : string | boolean +>'boolean' : string + + let label: string = (typeof strOrBool === 'string') ? strOrBool : "string"; +>label : string +>(typeof strOrBool === 'string') ? strOrBool : "string" : string +>(typeof strOrBool === 'string') : boolean +>typeof strOrBool === 'string' : boolean +>typeof strOrBool : string +>strOrBool : boolean | string +>'string' : string +>strOrBool : string +>"string" : string + + let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false; +>bool : boolean +>(typeof strOrBool === 'boolean') ? strOrBool : false : boolean +>(typeof strOrBool === 'boolean') : boolean +>typeof strOrBool === 'boolean' : boolean +>typeof strOrBool : string +>strOrBool : boolean | string +>'boolean' : string +>strOrBool : boolean +>false : boolean + + let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; +>label2 : string +>(typeof strOrBool !== 'boolean') ? strOrBool : "string" : string +>(typeof strOrBool !== 'boolean') : boolean +>typeof strOrBool !== 'boolean' : boolean +>typeof strOrBool : string +>strOrBool : boolean | string +>'boolean' : string +>strOrBool : string +>"string" : string + + let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false; +>bool2 : boolean +>(typeof strOrBool !== 'string') ? strOrBool : false : boolean +>(typeof strOrBool !== 'string') : boolean +>typeof strOrBool !== 'string' : boolean +>typeof strOrBool : string +>strOrBool : boolean | string +>'string' : string +>strOrBool : boolean +>false : boolean +} + diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfOther.js b/tests/baselines/reference/typeGuardOfFormTypeOfOther.js index 3bb1a2091f6cd..d1cb49941961f 100644 --- a/tests/baselines/reference/typeGuardOfFormTypeOfOther.js +++ b/tests/baselines/reference/typeGuardOfFormTypeOfOther.js @@ -23,19 +23,19 @@ if (typeof strOrC === "Object") { c = strOrC; // C } else { - var r2: string | C = strOrC; // string | C + var r2: string = strOrC; // string } if (typeof numOrC === "Object") { c = numOrC; // C } else { - var r3: number | C = numOrC; // number | C + var r3: number = numOrC; // number } if (typeof boolOrC === "Object") { c = boolOrC; // C } else { - var r4: boolean | C = boolOrC; // boolean | C + var r4: boolean = boolOrC; // boolean } // Narrowing occurs only if target type is a subtype of variable type @@ -50,19 +50,19 @@ else { // - when true, narrows the type of x by typeof x === s when false, or // - when false, narrows the type of x by typeof x === s when true. if (typeof strOrC !== "Object") { - var r2: string | C = strOrC; // string | C + var r2: string = strOrC; // string } else { c = strOrC; // C } if (typeof numOrC !== "Object") { - var r3: number | C = numOrC; // number | C + var r3: number = numOrC; // number } else { c = numOrC; // C } if (typeof boolOrC !== "Object") { - var r4: boolean | C = boolOrC; // boolean | C + var r4: boolean = boolOrC; // boolean } else { c = boolOrC; // C @@ -104,19 +104,19 @@ if (typeof strOrC === "Object") { c = strOrC; // C } else { - var r2 = strOrC; // string | C + var r2 = strOrC; // string } if (typeof numOrC === "Object") { c = numOrC; // C } else { - var r3 = numOrC; // number | C + var r3 = numOrC; // number } if (typeof boolOrC === "Object") { c = boolOrC; // C } else { - var r4 = boolOrC; // boolean | C + var r4 = boolOrC; // boolean } // Narrowing occurs only if target type is a subtype of variable type if (typeof strOrNumOrBool === "Object") { @@ -129,19 +129,19 @@ else { // - when true, narrows the type of x by typeof x === s when false, or // - when false, narrows the type of x by typeof x === s when true. if (typeof strOrC !== "Object") { - var r2 = strOrC; // string | C + var r2 = strOrC; // string } else { c = strOrC; // C } if (typeof numOrC !== "Object") { - var r3 = numOrC; // number | C + var r3 = numOrC; // number } else { c = numOrC; // C } if (typeof boolOrC !== "Object") { - var r4 = boolOrC; // boolean | C + var r4 = boolOrC; // boolean } else { c = boolOrC; // C diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfOther.symbols b/tests/baselines/reference/typeGuardOfFormTypeOfOther.symbols index e3ecffdc2e9c2..eb120d468dcb4 100644 --- a/tests/baselines/reference/typeGuardOfFormTypeOfOther.symbols +++ b/tests/baselines/reference/typeGuardOfFormTypeOfOther.symbols @@ -56,9 +56,8 @@ if (typeof strOrC === "Object") { >strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfOther.ts, 9, 3)) } else { - var r2: string | C = strOrC; // string | C + var r2: string = strOrC; // string >r2 : Symbol(r2, Decl(typeGuardOfFormTypeOfOther.ts, 24, 7), Decl(typeGuardOfFormTypeOfOther.ts, 51, 7)) ->C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0)) >strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfOther.ts, 9, 3)) } if (typeof numOrC === "Object") { @@ -69,9 +68,8 @@ if (typeof numOrC === "Object") { >numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfOther.ts, 10, 3)) } else { - var r3: number | C = numOrC; // number | C + var r3: number = numOrC; // number >r3 : Symbol(r3, Decl(typeGuardOfFormTypeOfOther.ts, 30, 7), Decl(typeGuardOfFormTypeOfOther.ts, 57, 7)) ->C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0)) >numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfOther.ts, 10, 3)) } if (typeof boolOrC === "Object") { @@ -82,9 +80,8 @@ if (typeof boolOrC === "Object") { >boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfOther.ts, 11, 3)) } else { - var r4: boolean | C = boolOrC; // boolean | C + var r4: boolean = boolOrC; // boolean >r4 : Symbol(r4, Decl(typeGuardOfFormTypeOfOther.ts, 36, 7), Decl(typeGuardOfFormTypeOfOther.ts, 63, 7)) ->C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0)) >boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfOther.ts, 11, 3)) } @@ -108,9 +105,8 @@ else { if (typeof strOrC !== "Object") { >strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfOther.ts, 9, 3)) - var r2: string | C = strOrC; // string | C + var r2: string = strOrC; // string >r2 : Symbol(r2, Decl(typeGuardOfFormTypeOfOther.ts, 24, 7), Decl(typeGuardOfFormTypeOfOther.ts, 51, 7)) ->C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0)) >strOrC : Symbol(strOrC, Decl(typeGuardOfFormTypeOfOther.ts, 9, 3)) } else { @@ -121,9 +117,8 @@ else { if (typeof numOrC !== "Object") { >numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfOther.ts, 10, 3)) - var r3: number | C = numOrC; // number | C + var r3: number = numOrC; // number >r3 : Symbol(r3, Decl(typeGuardOfFormTypeOfOther.ts, 30, 7), Decl(typeGuardOfFormTypeOfOther.ts, 57, 7)) ->C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0)) >numOrC : Symbol(numOrC, Decl(typeGuardOfFormTypeOfOther.ts, 10, 3)) } else { @@ -134,9 +129,8 @@ else { if (typeof boolOrC !== "Object") { >boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfOther.ts, 11, 3)) - var r4: boolean | C = boolOrC; // boolean | C + var r4: boolean = boolOrC; // boolean >r4 : Symbol(r4, Decl(typeGuardOfFormTypeOfOther.ts, 36, 7), Decl(typeGuardOfFormTypeOfOther.ts, 63, 7)) ->C : Symbol(C, Decl(typeGuardOfFormTypeOfOther.ts, 0, 0)) >boolOrC : Symbol(boolOrC, Decl(typeGuardOfFormTypeOfOther.ts, 11, 3)) } else { diff --git a/tests/baselines/reference/typeGuardOfFormTypeOfOther.types b/tests/baselines/reference/typeGuardOfFormTypeOfOther.types index 8150c3ec25d5a..5cec356719493 100644 --- a/tests/baselines/reference/typeGuardOfFormTypeOfOther.types +++ b/tests/baselines/reference/typeGuardOfFormTypeOfOther.types @@ -60,9 +60,8 @@ if (typeof strOrC === "Object") { >strOrC : C } else { - var r2: string | C = strOrC; // string | C ->r2 : string | C ->C : C + var r2: string = strOrC; // string +>r2 : string >strOrC : string } if (typeof numOrC === "Object") { @@ -77,9 +76,8 @@ if (typeof numOrC === "Object") { >numOrC : C } else { - var r3: number | C = numOrC; // number | C ->r3 : number | C ->C : C + var r3: number = numOrC; // number +>r3 : number >numOrC : number } if (typeof boolOrC === "Object") { @@ -94,9 +92,8 @@ if (typeof boolOrC === "Object") { >boolOrC : C } else { - var r4: boolean | C = boolOrC; // boolean | C ->r4 : boolean | C ->C : C + var r4: boolean = boolOrC; // boolean +>r4 : boolean >boolOrC : boolean } @@ -126,9 +123,8 @@ if (typeof strOrC !== "Object") { >strOrC : string | C >"Object" : string - var r2: string | C = strOrC; // string | C ->r2 : string | C ->C : C + var r2: string = strOrC; // string +>r2 : string >strOrC : string } else { @@ -143,9 +139,8 @@ if (typeof numOrC !== "Object") { >numOrC : number | C >"Object" : string - var r3: number | C = numOrC; // number | C ->r3 : number | C ->C : C + var r3: number = numOrC; // number +>r3 : number >numOrC : number } else { @@ -160,9 +155,8 @@ if (typeof boolOrC !== "Object") { >boolOrC : boolean | C >"Object" : string - var r4: boolean | C = boolOrC; // boolean | C ->r4 : boolean | C ->C : C + var r4: boolean = boolOrC; // boolean +>r4 : boolean >boolOrC : boolean } else { diff --git a/tests/baselines/reference/typeGuardTautologicalConsistiency.js b/tests/baselines/reference/typeGuardTautologicalConsistiency.js new file mode 100644 index 0000000000000..1b7fa2021c534 --- /dev/null +++ b/tests/baselines/reference/typeGuardTautologicalConsistiency.js @@ -0,0 +1,24 @@ +//// [typeGuardTautologicalConsistiency.ts] +let stringOrNumber: string | number; + +if (typeof stringOrNumber === "number") { + if (typeof stringOrNumber !== "number") { + stringOrNumber; + } +} + +if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") { + stringOrNumber; +} + + +//// [typeGuardTautologicalConsistiency.js] +var stringOrNumber; +if (typeof stringOrNumber === "number") { + if (typeof stringOrNumber !== "number") { + stringOrNumber; + } +} +if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") { + stringOrNumber; +} diff --git a/tests/baselines/reference/typeGuardTautologicalConsistiency.symbols b/tests/baselines/reference/typeGuardTautologicalConsistiency.symbols new file mode 100644 index 0000000000000..9ee82a5413fe3 --- /dev/null +++ b/tests/baselines/reference/typeGuardTautologicalConsistiency.symbols @@ -0,0 +1,23 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardTautologicalConsistiency.ts === +let stringOrNumber: string | number; +>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3)) + +if (typeof stringOrNumber === "number") { +>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3)) + + if (typeof stringOrNumber !== "number") { +>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3)) + + stringOrNumber; +>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3)) + } +} + +if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") { +>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3)) +>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3)) + + stringOrNumber; +>stringOrNumber : Symbol(stringOrNumber, Decl(typeGuardTautologicalConsistiency.ts, 0, 3)) +} + diff --git a/tests/baselines/reference/typeGuardTautologicalConsistiency.types b/tests/baselines/reference/typeGuardTautologicalConsistiency.types new file mode 100644 index 0000000000000..9acb1464761d1 --- /dev/null +++ b/tests/baselines/reference/typeGuardTautologicalConsistiency.types @@ -0,0 +1,36 @@ +=== tests/cases/conformance/expressions/typeGuards/typeGuardTautologicalConsistiency.ts === +let stringOrNumber: string | number; +>stringOrNumber : string | number + +if (typeof stringOrNumber === "number") { +>typeof stringOrNumber === "number" : boolean +>typeof stringOrNumber : string +>stringOrNumber : string | number +>"number" : string + + if (typeof stringOrNumber !== "number") { +>typeof stringOrNumber !== "number" : boolean +>typeof stringOrNumber : string +>stringOrNumber : number +>"number" : string + + stringOrNumber; +>stringOrNumber : string + } +} + +if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") { +>typeof stringOrNumber === "number" && typeof stringOrNumber !== "number" : boolean +>typeof stringOrNumber === "number" : boolean +>typeof stringOrNumber : string +>stringOrNumber : string | number +>"number" : string +>typeof stringOrNumber !== "number" : boolean +>typeof stringOrNumber : string +>stringOrNumber : number +>"number" : string + + stringOrNumber; +>stringOrNumber : number +} + diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardEnums.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardEnums.ts new file mode 100644 index 0000000000000..54e9a1bd8fe4b --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardEnums.ts @@ -0,0 +1,18 @@ +enum E {} +enum V {} + +let x: number|string|E|V; + +if (typeof x === "number") { + x; // number|E|V +} +else { + x; // string +} + +if (typeof x !== "number") { + x; // string +} +else { + x; // number|E|V +} diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts index b06b5880800ec..6423af18a5adc 100644 --- a/tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardNesting.ts @@ -1,4 +1,14 @@ let strOrBool: string|boolean; if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') { - var label: string = (typeof strOrBool === 'string') ? strOrBool : "other string"; -} \ No newline at end of file + let label: string = (typeof strOrBool === 'string') ? strOrBool : "string"; + let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false; + let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; + let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false; +} + +if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') { + let label: string = (typeof strOrBool === 'string') ? strOrBool : "string"; + let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false; + let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string"; + let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false; +} diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts index 9d7d555fc1850..ac3403d8151ad 100644 --- a/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts @@ -22,19 +22,19 @@ if (typeof strOrC === "Object") { c = strOrC; // C } else { - var r2: string | C = strOrC; // string | C + var r2: string = strOrC; // string } if (typeof numOrC === "Object") { c = numOrC; // C } else { - var r3: number | C = numOrC; // number | C + var r3: number = numOrC; // number } if (typeof boolOrC === "Object") { c = boolOrC; // C } else { - var r4: boolean | C = boolOrC; // boolean | C + var r4: boolean = boolOrC; // boolean } // Narrowing occurs only if target type is a subtype of variable type @@ -49,19 +49,19 @@ else { // - when true, narrows the type of x by typeof x === s when false, or // - when false, narrows the type of x by typeof x === s when true. if (typeof strOrC !== "Object") { - var r2: string | C = strOrC; // string | C + var r2: string = strOrC; // string } else { c = strOrC; // C } if (typeof numOrC !== "Object") { - var r3: number | C = numOrC; // number | C + var r3: number = numOrC; // number } else { c = numOrC; // C } if (typeof boolOrC !== "Object") { - var r4: boolean | C = boolOrC; // boolean | C + var r4: boolean = boolOrC; // boolean } else { c = boolOrC; // C diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardTautologicalConsistiency.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardTautologicalConsistiency.ts new file mode 100644 index 0000000000000..9a44603d2d43b --- /dev/null +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardTautologicalConsistiency.ts @@ -0,0 +1,11 @@ +let stringOrNumber: string | number; + +if (typeof stringOrNumber === "number") { + if (typeof stringOrNumber !== "number") { + stringOrNumber; + } +} + +if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") { + stringOrNumber; +} From 19e796dcbd61d77601f27c9cb4bd4fea396d633d Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 29 Oct 2015 18:46:06 -0700 Subject: [PATCH 5/8] More correctness --- src/compiler/checker.ts | 64 ++++++++++++------- .../typeGuardTautologicalConsistiency.types | 4 +- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4e8cb7cc340f6..30edecf3b40b9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6275,55 +6275,67 @@ 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: + case SyntaxKind.ConditionalExpression: + case SyntaxKind.BinaryExpression: + nodeStack.push({node, child}); + break; + case SyntaxKind.SourceFile: + case SyntaxKind.ModuleDeclaration: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.MethodDeclaration: + case SyntaxKind.MethodSignature: + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: + case SyntaxKind.Constructor: + // Stop at the first containing function or module declaration + break loop; + } + } + + 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) { - narrowedType = narrowType(type, (node).expression, /*assumeTrue*/ child === (node).thenStatement); + type = 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); + 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) { - narrowedType = narrowType(type, (node).left, /*assumeTrue*/ true); + type = narrowType(type, (node).left, /*assumeTrue*/ true); } else if ((node).operatorToken.kind === SyntaxKind.BarBarToken) { - narrowedType = narrowType(type, (node).left, /*assumeTrue*/ false); + type = narrowType(type, (node).left, /*assumeTrue*/ false); } } break; - case SyntaxKind.SourceFile: - case SyntaxKind.ModuleDeclaration: - case SyntaxKind.FunctionDeclaration: - case SyntaxKind.MethodDeclaration: - case SyntaxKind.MethodSignature: - case SyntaxKind.GetAccessor: - case SyntaxKind.SetAccessor: - case SyntaxKind.Constructor: - // Stop at the first containing function or module declaration - break loop; + default: + Debug.fail("Unreachable!"); } - // Use narrowed type if construct contains no assignments to variable - if (narrowedType !== type) { - if (isVariableAssignedWithin(symbol, node)) { - break; - } - type = narrowedType; + + // 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 9acb1464761d1..d758dcde22bc8 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 } From 0d0c05d1d3c4ef94fc15d17b5f2602729539ec8e Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 29 Oct 2015 18:50:11 -0700 Subject: [PATCH 6/8] that feling when you ctrl-z 1 too many times --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 30edecf3b40b9..8ae8ab3d54c26 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6334,7 +6334,7 @@ namespace ts { } // Use original type if construct contains assignments to variable - if (!nodeStack.length && isVariableAssignedWithin(symbol, node)) { + if (type != originalType && isVariableAssignedWithin(symbol, node)) { type = originalType; } } From d48a4f0cf184f5ff045d027693282c3b1b03c003 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 2 Nov 2015 10:48:59 -0800 Subject: [PATCH 7/8] fix nits --- src/compiler/checker.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8ae8ab3d54c26..b77faadd01120 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -112,6 +112,7 @@ namespace ts { let circularType = createIntrinsicType(TypeFlags.Any, "__circular__"); let emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + let emptyUnionType = emptyObjectType; let emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); emptyGenericType.instantiations = {}; @@ -4321,7 +4322,7 @@ namespace ts { // a named type that circularly references itself. function getUnionType(types: Type[], noSubtypeReduction?: boolean): Type { if (types.length === 0) { - return emptyObjectType; + return emptyUnionType; } let typeSet: Type[] = []; addTypesToSet(typeSet, types, TypeFlags.Union); @@ -6278,8 +6279,8 @@ namespace ts { // 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}[] = []; + const originalType = type; + const nodeStack: {node: Node, child: Node}[] = []; loop: while (node.parent) { let child = node; node = node.parent; @@ -6304,7 +6305,7 @@ namespace ts { let nodes: {node: Node, child: Node}; while (nodes = nodeStack.pop()) { - let {node, child} = nodes; + const {node, child} = nodes; switch (node.kind) { case SyntaxKind.IfStatement: // In a branch of an if statement, narrow based on controlling expression @@ -6334,13 +6335,13 @@ namespace ts { } // Use original type if construct contains assignments to variable - if (type != originalType && isVariableAssignedWithin(symbol, node)) { + if (type !== originalType && isVariableAssignedWithin(symbol, node)) { type = originalType; } } // Preserve old top-level behavior - if the branch is really an empty set, revert to prior type - if (type === getUnionType(emptyArray)) { + if (type === emptyUnionType) { type = originalType; } } @@ -6368,7 +6369,7 @@ namespace ts { } let flags: TypeFlags; if (typeInfo) { - flags = typeInfo.flags; + flags = typeInfo.flags; } else { assumeTrue = !assumeTrue; @@ -6377,12 +6378,12 @@ 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 filterUnion(type) ? type : getUnionType(emptyArray); + return filterUnion(type) ? type : emptyUnionType; } return getUnionType(filter((type as UnionType).types, filterUnion), /*noSubtypeReduction*/ true); - function filterUnion(t: Type) { - return assumeTrue === !!(t.flags & flags); + function filterUnion(type: Type) { + return assumeTrue === !!(type.flags & flags); } } @@ -12558,7 +12559,7 @@ namespace ts { arrayType = getUnionType(filter((arrayOrStringType as UnionType).types, t => !(t.flags & TypeFlags.StringLike))); } else if (arrayOrStringType.flags & TypeFlags.StringLike) { - arrayType = getUnionType(emptyArray); + arrayType = emptyUnionType; } let hasStringConstituent = arrayOrStringType !== arrayType; From 77c69daf2e7d67f8ec54947983ce331e672c454c Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 3 Nov 2015 14:35:11 -0800 Subject: [PATCH 8/8] const all the things --- src/compiler/checker.ts | 101 ++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b77faadd01120..4bdbebaa50b17 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -33,26 +33,26 @@ namespace ts { // they no longer need the information (for example, if the user started editing again). let cancellationToken: CancellationToken; - let Symbol = objectAllocator.getSymbolConstructor(); - let Type = objectAllocator.getTypeConstructor(); - let Signature = objectAllocator.getSignatureConstructor(); + const Symbol = objectAllocator.getSymbolConstructor(); + const Type = objectAllocator.getTypeConstructor(); + const Signature = objectAllocator.getSignatureConstructor(); let typeCount = 0; let symbolCount = 0; - let emptyArray: any[] = []; - let emptySymbols: SymbolTable = {}; + const emptyArray: any[] = []; + const emptySymbols: SymbolTable = {}; - let compilerOptions = host.getCompilerOptions(); - let languageVersion = compilerOptions.target || ScriptTarget.ES3; - let modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None; + const compilerOptions = host.getCompilerOptions(); + const languageVersion = compilerOptions.target || ScriptTarget.ES3; + const modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None; - let emitResolver = createResolver(); + const emitResolver = createResolver(); - let undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined"); - let argumentsSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "arguments"); + const undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined"); + const argumentsSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "arguments"); - let checker: TypeChecker = { + const checker: TypeChecker = { getNodeCount: () => sum(host.getSourceFiles(), "nodeCount"), getIdentifierCount: () => sum(host.getSourceFiles(), "identifierCount"), getSymbolCount: () => sum(host.getSourceFiles(), "symbolCount") + symbolCount, @@ -97,36 +97,35 @@ namespace ts { isOptionalParameter }; - let unknownSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "unknown"); - let resolvingSymbol = createSymbol(SymbolFlags.Transient, "__resolving__"); - - let anyType = createIntrinsicType(TypeFlags.Any, "any"); - let stringType = createIntrinsicType(TypeFlags.String, "string"); - let numberType = createIntrinsicType(TypeFlags.Number, "number"); - let booleanType = createIntrinsicType(TypeFlags.Boolean, "boolean"); - let esSymbolType = createIntrinsicType(TypeFlags.ESSymbol, "symbol"); - let voidType = createIntrinsicType(TypeFlags.Void, "void"); - let undefinedType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsUndefinedOrNull, "undefined"); - let nullType = createIntrinsicType(TypeFlags.Null | TypeFlags.ContainsUndefinedOrNull, "null"); - let unknownType = createIntrinsicType(TypeFlags.Any, "unknown"); - let circularType = createIntrinsicType(TypeFlags.Any, "__circular__"); - - let emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - let emptyUnionType = emptyObjectType; - let emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + const unknownSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "unknown"); + const resolvingSymbol = createSymbol(SymbolFlags.Transient, "__resolving__"); + + const anyType = createIntrinsicType(TypeFlags.Any, "any"); + const stringType = createIntrinsicType(TypeFlags.String, "string"); + const numberType = createIntrinsicType(TypeFlags.Number, "number"); + const booleanType = createIntrinsicType(TypeFlags.Boolean, "boolean"); + const esSymbolType = createIntrinsicType(TypeFlags.ESSymbol, "symbol"); + const voidType = createIntrinsicType(TypeFlags.Void, "void"); + const undefinedType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsUndefinedOrNull, "undefined"); + const nullType = createIntrinsicType(TypeFlags.Null | TypeFlags.ContainsUndefinedOrNull, "null"); + const unknownType = createIntrinsicType(TypeFlags.Any, "unknown"); + + const emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + const emptyUnionType = emptyObjectType; + const emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); emptyGenericType.instantiations = {}; - let anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + const anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); // The anyFunctionType contains the anyFunctionType by definition. The flag is further propagated // in getPropagatingFlagsOfTypes, and it is checked in inferFromTypes. anyFunctionType.flags |= TypeFlags.ContainsAnyFunctionType; - let noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + const noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - let anySignature = createSignature(undefined, undefined, emptyArray, anyType, undefined, 0, false, false); - let unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, undefined, 0, false, false); + const anySignature = createSignature(undefined, undefined, emptyArray, anyType, undefined, 0, false, false); + const unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, undefined, 0, false, false); - let globals: SymbolTable = {}; + const globals: SymbolTable = {}; let globalESSymbolConstructorSymbol: Symbol; @@ -161,29 +160,29 @@ namespace ts { let getGlobalPromiseConstructorLikeType: () => ObjectType; let getGlobalThenableType: () => ObjectType; - let tupleTypes: Map = {}; - let unionTypes: Map = {}; - let intersectionTypes: Map = {}; - let stringLiteralTypes: Map = {}; + const tupleTypes: Map = {}; + const unionTypes: Map = {}; + const intersectionTypes: Map = {}; + const stringLiteralTypes: Map = {}; let emitExtends = false; let emitDecorate = false; let emitParam = false; let emitAwaiter = false; let emitGenerator = false; - let resolutionTargets: TypeSystemEntity[] = []; - let resolutionResults: boolean[] = []; - let resolutionPropertyNames: TypeSystemPropertyName[] = []; + const resolutionTargets: TypeSystemEntity[] = []; + const resolutionResults: boolean[] = []; + const resolutionPropertyNames: TypeSystemPropertyName[] = []; - let mergedSymbols: Symbol[] = []; - let symbolLinks: SymbolLinks[] = []; - let nodeLinks: NodeLinks[] = []; - let potentialThisCollisions: Node[] = []; - let awaitedTypeStack: number[] = []; + const mergedSymbols: Symbol[] = []; + const symbolLinks: SymbolLinks[] = []; + const nodeLinks: NodeLinks[] = []; + const potentialThisCollisions: Node[] = []; + const awaitedTypeStack: number[] = []; - let diagnostics = createDiagnosticCollection(); + const diagnostics = createDiagnosticCollection(); - let primitiveTypeInfo: Map<{ type: Type; flags: TypeFlags }> = { + const primitiveTypeInfo: Map<{ type: Type; flags: TypeFlags }> = { "string": { type: stringType, flags: TypeFlags.StringLike @@ -210,9 +209,9 @@ namespace ts { Element: "Element" }; - let subtypeRelation: Map = {}; - let assignableRelation: Map = {}; - let identityRelation: Map = {}; + const subtypeRelation: Map = {}; + const assignableRelation: Map = {}; + const identityRelation: Map = {}; // This is for caching the result of getSymbolDisplayBuilder. Do not access directly. let _displayBuilder: SymbolDisplayBuilder;