symbols in type guards

This commit is contained in:
Jason Freeman 2015-02-05 12:30:22 -08:00
parent 9f39a5388a
commit df826de042
10 changed files with 171 additions and 1 deletions

View File

@ -4767,7 +4767,7 @@ module ts {
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, /*isOfTypeKind*/ true);
return removeTypesFromUnionType(type, /*typeKind*/ TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.Boolean | TypeFlags.ESSymbol, /*isOfTypeKind*/ true);
}
// Check was for a primitive type, return that primitive type if it is a subtype
if (isTypeSubtypeOf(typeInfo.type, type)) {

View File

@ -0,0 +1,21 @@
//// [symbolType17.ts]
interface Foo { prop }
var x: symbol | Foo;
x;
if (typeof x === "symbol") {
x;
}
else {
x;
}
//// [symbolType17.js]
var x;
x;
if (typeof x === "symbol") {
x;
}
else {
x;
}

View File

@ -0,0 +1,24 @@
=== tests/cases/conformance/es6/Symbols/symbolType17.ts ===
interface Foo { prop }
>Foo : Foo
>prop : any
var x: symbol | Foo;
>x : symbol | Foo
>Foo : Foo
x;
>x : symbol | Foo
if (typeof x === "symbol") {
>typeof x === "symbol" : boolean
>typeof x : string
>x : symbol | Foo
x;
>x : symbol
}
else {
x;
>x : Foo
}

View File

@ -0,0 +1,21 @@
//// [symbolType18.ts]
interface Foo { prop }
var x: symbol | Foo;
x;
if (typeof x === "object") {
x;
}
else {
x;
}
//// [symbolType18.js]
var x;
x;
if (typeof x === "object") {
x;
}
else {
x;
}

View File

@ -0,0 +1,24 @@
=== tests/cases/conformance/es6/Symbols/symbolType18.ts ===
interface Foo { prop }
>Foo : Foo
>prop : any
var x: symbol | Foo;
>x : symbol | Foo
>Foo : Foo
x;
>x : symbol | Foo
if (typeof x === "object") {
>typeof x === "object" : boolean
>typeof x : string
>x : symbol | Foo
x;
>x : Foo
}
else {
x;
>x : symbol | Foo
}

View File

@ -0,0 +1,24 @@
//// [symbolType19.ts]
enum E { }
var x: symbol | E;
x;
if (typeof x === "number") {
x;
}
else {
x;
}
//// [symbolType19.js]
var E;
(function (E) {
})(E || (E = {}));
var x;
x;
if (typeof x === "number") {
x;
}
else {
x;
}

View File

@ -0,0 +1,23 @@
=== tests/cases/conformance/es6/Symbols/symbolType19.ts ===
enum E { }
>E : E
var x: symbol | E;
>x : symbol | E
>E : E
x;
>x : symbol | E
if (typeof x === "number") {
>typeof x === "number" : boolean
>typeof x : string
>x : symbol | E
x;
>x : E
}
else {
x;
>x : symbol
}

View File

@ -0,0 +1,11 @@
//@target: ES6
interface Foo { prop }
var x: symbol | Foo;
x;
if (typeof x === "symbol") {
x;
}
else {
x;
}

View File

@ -0,0 +1,11 @@
//@target: ES6
interface Foo { prop }
var x: symbol | Foo;
x;
if (typeof x === "object") {
x;
}
else {
x;
}

View File

@ -0,0 +1,11 @@
//@target: ES6
enum E { }
var x: symbol | E;
x;
if (typeof x === "number") {
x;
}
else {
x;
}