Skip to content

Commit df826de

Browse files
committed
symbols in type guards
1 parent 9f39a53 commit df826de

File tree

10 files changed

+171
-1
lines changed

10 files changed

+171
-1
lines changed

Diff for: src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4767,7 +4767,7 @@ module ts {
47674767
if (assumeTrue) {
47684768
// Assumed result is true. If check was not for a primitive type, remove all primitive types
47694769
if (!typeInfo) {
4770-
return removeTypesFromUnionType(type, /*typeKind*/ TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.Boolean, /*isOfTypeKind*/ true);
4770+
return removeTypesFromUnionType(type, /*typeKind*/ TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.Boolean | TypeFlags.ESSymbol, /*isOfTypeKind*/ true);
47714771
}
47724772
// Check was for a primitive type, return that primitive type if it is a subtype
47734773
if (isTypeSubtypeOf(typeInfo.type, type)) {

Diff for: tests/baselines/reference/symbolType17.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [symbolType17.ts]
2+
interface Foo { prop }
3+
var x: symbol | Foo;
4+
5+
x;
6+
if (typeof x === "symbol") {
7+
x;
8+
}
9+
else {
10+
x;
11+
}
12+
13+
//// [symbolType17.js]
14+
var x;
15+
x;
16+
if (typeof x === "symbol") {
17+
x;
18+
}
19+
else {
20+
x;
21+
}

Diff for: tests/baselines/reference/symbolType17.types

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/conformance/es6/Symbols/symbolType17.ts ===
2+
interface Foo { prop }
3+
>Foo : Foo
4+
>prop : any
5+
6+
var x: symbol | Foo;
7+
>x : symbol | Foo
8+
>Foo : Foo
9+
10+
x;
11+
>x : symbol | Foo
12+
13+
if (typeof x === "symbol") {
14+
>typeof x === "symbol" : boolean
15+
>typeof x : string
16+
>x : symbol | Foo
17+
18+
x;
19+
>x : symbol
20+
}
21+
else {
22+
x;
23+
>x : Foo
24+
}

Diff for: tests/baselines/reference/symbolType18.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [symbolType18.ts]
2+
interface Foo { prop }
3+
var x: symbol | Foo;
4+
5+
x;
6+
if (typeof x === "object") {
7+
x;
8+
}
9+
else {
10+
x;
11+
}
12+
13+
//// [symbolType18.js]
14+
var x;
15+
x;
16+
if (typeof x === "object") {
17+
x;
18+
}
19+
else {
20+
x;
21+
}

Diff for: tests/baselines/reference/symbolType18.types

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/conformance/es6/Symbols/symbolType18.ts ===
2+
interface Foo { prop }
3+
>Foo : Foo
4+
>prop : any
5+
6+
var x: symbol | Foo;
7+
>x : symbol | Foo
8+
>Foo : Foo
9+
10+
x;
11+
>x : symbol | Foo
12+
13+
if (typeof x === "object") {
14+
>typeof x === "object" : boolean
15+
>typeof x : string
16+
>x : symbol | Foo
17+
18+
x;
19+
>x : Foo
20+
}
21+
else {
22+
x;
23+
>x : symbol | Foo
24+
}

Diff for: tests/baselines/reference/symbolType19.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [symbolType19.ts]
2+
enum E { }
3+
var x: symbol | E;
4+
5+
x;
6+
if (typeof x === "number") {
7+
x;
8+
}
9+
else {
10+
x;
11+
}
12+
13+
//// [symbolType19.js]
14+
var E;
15+
(function (E) {
16+
})(E || (E = {}));
17+
var x;
18+
x;
19+
if (typeof x === "number") {
20+
x;
21+
}
22+
else {
23+
x;
24+
}

Diff for: tests/baselines/reference/symbolType19.types

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/conformance/es6/Symbols/symbolType19.ts ===
2+
enum E { }
3+
>E : E
4+
5+
var x: symbol | E;
6+
>x : symbol | E
7+
>E : E
8+
9+
x;
10+
>x : symbol | E
11+
12+
if (typeof x === "number") {
13+
>typeof x === "number" : boolean
14+
>typeof x : string
15+
>x : symbol | E
16+
17+
x;
18+
>x : E
19+
}
20+
else {
21+
x;
22+
>x : symbol
23+
}

Diff for: tests/cases/conformance/es6/Symbols/symbolType17.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@target: ES6
2+
interface Foo { prop }
3+
var x: symbol | Foo;
4+
5+
x;
6+
if (typeof x === "symbol") {
7+
x;
8+
}
9+
else {
10+
x;
11+
}

Diff for: tests/cases/conformance/es6/Symbols/symbolType18.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@target: ES6
2+
interface Foo { prop }
3+
var x: symbol | Foo;
4+
5+
x;
6+
if (typeof x === "object") {
7+
x;
8+
}
9+
else {
10+
x;
11+
}

Diff for: tests/cases/conformance/es6/Symbols/symbolType19.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@target: ES6
2+
enum E { }
3+
var x: symbol | E;
4+
5+
x;
6+
if (typeof x === "number") {
7+
x;
8+
}
9+
else {
10+
x;
11+
}

0 commit comments

Comments
 (0)