Skip to content

Commit e90f5e0

Browse files
committed
More tests of enum assignability
1. Numeric literal <-> enum literal assignability 2. Computed enum <-> union enum assignability 3. Also rebaseline error reporting of existing enum cases.
1 parent 8f40620 commit e90f5e0

9 files changed

+280
-19
lines changed

tests/baselines/reference/enumAssignmentCompat3.errors.txt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
tests/cases/compiler/enumAssignmentCompat3.ts(68,1): error TS2322: Type 'Abcd.E' is not assignable to type 'First.E'.
1+
tests/cases/compiler/enumAssignmentCompat3.ts(68,1): error TS2324: Property 'd' is missing in type 'First.E'.
2+
tests/cases/compiler/enumAssignmentCompat3.ts(70,1): error TS2322: Type 'Cd.E' is not assignable to type 'First.E'.
23
Property 'd' is missing in type 'First.E'.
3-
tests/cases/compiler/enumAssignmentCompat3.ts(70,1): error TS2324: Property 'd' is missing in type 'First.E'.
44
tests/cases/compiler/enumAssignmentCompat3.ts(71,1): error TS2322: Type 'Nope' is not assignable to type 'E'.
5-
tests/cases/compiler/enumAssignmentCompat3.ts(75,1): error TS2322: Type 'First.E' is not assignable to type 'Ab.E'.
6-
Property 'c' is missing in type 'Ab.E'.
5+
tests/cases/compiler/enumAssignmentCompat3.ts(75,1): error TS2324: Property 'c' is missing in type 'Ab.E'.
76
tests/cases/compiler/enumAssignmentCompat3.ts(76,1): error TS2322: Type 'First.E' is not assignable to type 'Cd.E'.
8-
Property 'a' is missing in type 'Cd.E'.
97
tests/cases/compiler/enumAssignmentCompat3.ts(77,1): error TS2322: Type 'E' is not assignable to type 'Nope'.
108
tests/cases/compiler/enumAssignmentCompat3.ts(82,1): error TS2322: Type 'Const.E' is not assignable to type 'First.E'.
119
tests/cases/compiler/enumAssignmentCompat3.ts(83,1): error TS2322: Type 'First.E' is not assignable to type 'Const.E'.
12-
tests/cases/compiler/enumAssignmentCompat3.ts(86,1): error TS2322: Type 'Merged.E' is not assignable to type 'First.E'.
13-
Property 'd' is missing in type 'First.E'.
10+
tests/cases/compiler/enumAssignmentCompat3.ts(86,1): error TS2324: Property 'd' is missing in type 'First.E'.
1411

1512

1613
==== tests/cases/compiler/enumAssignmentCompat3.ts (9 errors) ====
@@ -83,12 +80,12 @@ tests/cases/compiler/enumAssignmentCompat3.ts(86,1): error TS2322: Type 'Merged.
8380
abc = secondAbc; // ok
8481
abc = secondAbcd; // missing 'd'
8582
~~~
86-
!!! error TS2322: Type 'Abcd.E' is not assignable to type 'First.E'.
87-
!!! error TS2322: Property 'd' is missing in type 'First.E'.
83+
!!! error TS2324: Property 'd' is missing in type 'First.E'.
8884
abc = secondAb; // ok
8985
abc = secondCd; // missing 'd'
9086
~~~
91-
!!! error TS2324: Property 'd' is missing in type 'First.E'.
87+
!!! error TS2322: Type 'Cd.E' is not assignable to type 'First.E'.
88+
!!! error TS2322: Property 'd' is missing in type 'First.E'.
9289
abc = nope; // nope!
9390
~~~
9491
!!! error TS2322: Type 'Nope' is not assignable to type 'E'.
@@ -97,12 +94,10 @@ tests/cases/compiler/enumAssignmentCompat3.ts(86,1): error TS2322: Type 'Merged.
9794
secondAbcd = abc; // ok
9895
secondAb = abc; // missing 'c'
9996
~~~~~~~~
100-
!!! error TS2322: Type 'First.E' is not assignable to type 'Ab.E'.
101-
!!! error TS2322: Property 'c' is missing in type 'Ab.E'.
97+
!!! error TS2324: Property 'c' is missing in type 'Ab.E'.
10298
secondCd = abc; // missing 'a' and 'b'
10399
~~~~~~~~
104100
!!! error TS2322: Type 'First.E' is not assignable to type 'Cd.E'.
105-
!!! error TS2322: Property 'a' is missing in type 'Cd.E'.
106101
nope = abc; // nope!
107102
~~~~
108103
!!! error TS2322: Type 'E' is not assignable to type 'Nope'.
@@ -120,8 +115,7 @@ tests/cases/compiler/enumAssignmentCompat3.ts(86,1): error TS2322: Type 'Merged.
120115
// merged enums compare all their members
121116
abc = merged; // missing 'd'
122117
~~~
123-
!!! error TS2322: Type 'Merged.E' is not assignable to type 'First.E'.
124-
!!! error TS2322: Property 'd' is missing in type 'First.E'.
118+
!!! error TS2324: Property 'd' is missing in type 'First.E'.
125119
merged = abc; // ok
126120
abc = merged2; // ok
127121
merged2 = abc; // ok
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
tests/cases/compiler/enumAssignmentCompat5.ts(14,1): error TS2322: Type '2' is not assignable to type 'E.A'.
2+
tests/cases/compiler/enumAssignmentCompat5.ts(20,9): error TS2535: Enum type 'Computed' has members with initializers that are not literals.
3+
4+
5+
==== tests/cases/compiler/enumAssignmentCompat5.ts (2 errors) ====
6+
enum E {
7+
A, B, C
8+
}
9+
enum Computed {
10+
A = 1 << 1,
11+
B = 1 << 2,
12+
C = 1 << 3,
13+
}
14+
let n: number;
15+
let e: E = n; // ok because it's too inconvenient otherwise
16+
e = 0; // ok, in range
17+
e = 4; // ok, out of range, but allowed computed enums don't have all members
18+
let a: E.A = 0; // ok, A === 0
19+
a = 2; // error, 2 !== 0
20+
~
21+
!!! error TS2322: Type '2' is not assignable to type 'E.A'.
22+
a = n; // ok
23+
24+
let c: Computed = n; // ok
25+
c = n; // ok
26+
c = 4; // ok
27+
let ca: Computed.A = 1; // error, Computed.A isn't a literal type because Computed has no enum literals
28+
~~~~~~~~~~
29+
!!! error TS2535: Enum type 'Computed' has members with initializers that are not literals.
30+
31+
32+
33+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [enumAssignmentCompat5.ts]
2+
enum E {
3+
A, B, C
4+
}
5+
enum Computed {
6+
A = 1 << 1,
7+
B = 1 << 2,
8+
C = 1 << 3,
9+
}
10+
let n: number;
11+
let e: E = n; // ok because it's too inconvenient otherwise
12+
e = 0; // ok, in range
13+
e = 4; // ok, out of range, but allowed computed enums don't have all members
14+
let a: E.A = 0; // ok, A === 0
15+
a = 2; // error, 2 !== 0
16+
a = n; // ok
17+
18+
let c: Computed = n; // ok
19+
c = n; // ok
20+
c = 4; // ok
21+
let ca: Computed.A = 1; // error, Computed.A isn't a literal type because Computed has no enum literals
22+
23+
24+
25+
26+
27+
//// [enumAssignmentCompat5.js]
28+
var E;
29+
(function (E) {
30+
E[E["A"] = 0] = "A";
31+
E[E["B"] = 1] = "B";
32+
E[E["C"] = 2] = "C";
33+
})(E || (E = {}));
34+
var Computed;
35+
(function (Computed) {
36+
Computed[Computed["A"] = 2] = "A";
37+
Computed[Computed["B"] = 4] = "B";
38+
Computed[Computed["C"] = 8] = "C";
39+
})(Computed || (Computed = {}));
40+
var n;
41+
var e = n; // ok because it's too inconvenient otherwise
42+
e = 0; // ok, in range
43+
e = 4; // ok, out of range, but allowed computed enums don't have all members
44+
var a = 0; // ok, A === 0
45+
a = 2; // error, 2 !== 0
46+
a = n; // ok
47+
var c = n; // ok
48+
c = n; // ok
49+
c = 4; // ok
50+
var ca = 1; // error, Computed.A isn't a literal type because Computed has no enum literals
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
tests/cases/compiler/enumLiteralAssignableToEnumInsideUnion.ts(25,7): error TS2322: Type 'Foo' is not assignable to type 'boolean | Foo.B'.
2+
tests/cases/compiler/enumLiteralAssignableToEnumInsideUnion.ts(26,7): error TS2322: Type 'Foo' is not assignable to type 'boolean | Foo.A'.
3+
4+
5+
==== tests/cases/compiler/enumLiteralAssignableToEnumInsideUnion.ts (2 errors) ====
6+
module X {
7+
export enum Foo {
8+
A, B
9+
}
10+
}
11+
module Y {
12+
export enum Foo {
13+
A, B
14+
}
15+
}
16+
module Z {
17+
export enum Foo {
18+
A = 1 << 1,
19+
B = 1 << 2,
20+
}
21+
}
22+
module Ka {
23+
export enum Foo {
24+
A = 1 << 10,
25+
B = 1 << 11,
26+
}
27+
}
28+
const e1: X.Foo | boolean = Z.Foo.A; // ok
29+
const e2: X.Foo.A | X.Foo.B | boolean = Z.Foo.A; // ok, X.Foo is equivalent to X.Foo.A | X.Foo.B
30+
const e3: X.Foo.B | boolean = Z.Foo.A; // not legal
31+
~~
32+
!!! error TS2322: Type 'Foo' is not assignable to type 'boolean | Foo.B'.
33+
const e4: X.Foo.A | boolean = Z.Foo.A; // not legal either because Z.Foo is computed and Z.Foo.A is not necessarily assignable to X.Foo.A
34+
~~
35+
!!! error TS2322: Type 'Foo' is not assignable to type 'boolean | Foo.A'.
36+
const e5: Ka.Foo | boolean = Z.Foo.A; // ok
37+

tests/baselines/reference/enumLiteralAssignableToEnumInsideUnion.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,23 @@ module Y {
99
A, B
1010
}
1111
}
12-
const y: X.Foo | boolean = Y.Foo.A;
12+
module Z {
13+
export enum Foo {
14+
A = 1 << 1,
15+
B = 1 << 2,
16+
}
17+
}
18+
module Ka {
19+
export enum Foo {
20+
A = 1 << 10,
21+
B = 1 << 11,
22+
}
23+
}
24+
const e1: X.Foo | boolean = Z.Foo.A; // ok
25+
const e2: X.Foo.A | X.Foo.B | boolean = Z.Foo.A; // ok, X.Foo is equivalent to X.Foo.A | X.Foo.B
26+
const e3: X.Foo.B | boolean = Z.Foo.A; // not legal
27+
const e4: X.Foo.A | boolean = Z.Foo.A; // not legal either because Z.Foo is computed and Z.Foo.A is not necessarily assignable to X.Foo.A
28+
const e5: Ka.Foo | boolean = Z.Foo.A; // ok
1329

1430

1531
//// [enumLiteralAssignableToEnumInsideUnion.js]
@@ -29,4 +45,24 @@ var Y;
2945
})(Y.Foo || (Y.Foo = {}));
3046
var Foo = Y.Foo;
3147
})(Y || (Y = {}));
32-
var y = Y.Foo.A;
48+
var Z;
49+
(function (Z) {
50+
(function (Foo) {
51+
Foo[Foo["A"] = 2] = "A";
52+
Foo[Foo["B"] = 4] = "B";
53+
})(Z.Foo || (Z.Foo = {}));
54+
var Foo = Z.Foo;
55+
})(Z || (Z = {}));
56+
var Ka;
57+
(function (Ka) {
58+
(function (Foo) {
59+
Foo[Foo["A"] = 1024] = "A";
60+
Foo[Foo["B"] = 2048] = "B";
61+
})(Ka.Foo || (Ka.Foo = {}));
62+
var Foo = Ka.Foo;
63+
})(Ka || (Ka = {}));
64+
var e1 = Z.Foo.A; // ok
65+
var e2 = Z.Foo.A; // ok, X.Foo is equivalent to X.Foo.A | X.Foo.B
66+
var e3 = Z.Foo.A; // not legal
67+
var e4 = Z.Foo.A; // not legal either because Z.Foo is computed and Z.Foo.A is not necessarily assignable to X.Foo.A
68+
var e5 = Z.Foo.A; // ok

tests/baselines/reference/enumLiteralAssignableToEnumInsideUnion.symbols

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@ module Y {
2121
>B : Symbol(Foo.B, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 7, 10))
2222
}
2323
}
24+
module Z {
25+
>Z : Symbol(Z, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 9, 1))
26+
27+
export enum Foo {
28+
>Foo : Symbol(Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 10, 10))
29+
30+
A = 1 << 1,
31+
>A : Symbol(Foo.A, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 11, 21))
32+
33+
B = 1 << 2,
34+
>B : Symbol(Foo.B, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 12, 19))
35+
}
36+
}
2437
const y: X.Foo | boolean = Y.Foo.A;
25-
>y : Symbol(y, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 10, 5))
38+
>y : Symbol(y, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 16, 5))
2639
>X : Symbol(X, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 0, 0))
2740
>Foo : Symbol(X.Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 0, 10))
2841
>Y.Foo.A : Symbol(Y.Foo.A, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 6, 21))
@@ -31,3 +44,23 @@ const y: X.Foo | boolean = Y.Foo.A;
3144
>Foo : Symbol(Y.Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 5, 10))
3245
>A : Symbol(Y.Foo.A, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 6, 21))
3346

47+
const z: X.Foo | boolean = Z.Foo.A;
48+
>z : Symbol(z, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 17, 5))
49+
>X : Symbol(X, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 0, 0))
50+
>Foo : Symbol(X.Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 0, 10))
51+
>Z.Foo.A : Symbol(Z.Foo.A, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 11, 21))
52+
>Z.Foo : Symbol(Z.Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 10, 10))
53+
>Z : Symbol(Z, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 9, 1))
54+
>Foo : Symbol(Z.Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 10, 10))
55+
>A : Symbol(Z.Foo.A, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 11, 21))
56+
57+
const x: Z.Foo | boolean = X.Foo.A;
58+
>x : Symbol(x, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 18, 5))
59+
>Z : Symbol(Z, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 9, 1))
60+
>Foo : Symbol(Z.Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 10, 10))
61+
>X.Foo.A : Symbol(X.Foo.A, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 1, 21))
62+
>X.Foo : Symbol(X.Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 0, 10))
63+
>X : Symbol(X, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 0, 0))
64+
>Foo : Symbol(X.Foo, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 0, 10))
65+
>A : Symbol(X.Foo.A, Decl(enumLiteralAssignableToEnumInsideUnion.ts, 1, 21))
66+

tests/baselines/reference/enumLiteralAssignableToEnumInsideUnion.types

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ module Y {
2121
>B : Foo
2222
}
2323
}
24+
module Z {
25+
>Z : typeof Z
26+
27+
export enum Foo {
28+
>Foo : Foo
29+
30+
A = 1 << 1,
31+
>A : Foo
32+
>1 << 1 : number
33+
>1 : number
34+
>1 : number
35+
36+
B = 1 << 2,
37+
>B : Foo
38+
>1 << 2 : number
39+
>1 : number
40+
>2 : number
41+
}
42+
}
2443
const y: X.Foo | boolean = Y.Foo.A;
2544
>y : boolean | X.Foo
2645
>X : any
@@ -31,3 +50,23 @@ const y: X.Foo | boolean = Y.Foo.A;
3150
>Foo : typeof Y.Foo
3251
>A : Y.Foo
3352

53+
const z: X.Foo | boolean = Z.Foo.A;
54+
>z : boolean | X.Foo
55+
>X : any
56+
>Foo : X.Foo
57+
>Z.Foo.A : Z.Foo
58+
>Z.Foo : typeof Z.Foo
59+
>Z : typeof Z
60+
>Foo : typeof Z.Foo
61+
>A : Z.Foo
62+
63+
const x: Z.Foo | boolean = X.Foo.A;
64+
>x : boolean | Z.Foo
65+
>Z : any
66+
>Foo : Z.Foo
67+
>X.Foo.A : X.Foo
68+
>X.Foo : typeof X.Foo
69+
>X : typeof X
70+
>Foo : typeof X.Foo
71+
>A : X.Foo
72+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
enum E {
2+
A, B, C
3+
}
4+
enum Computed {
5+
A = 1 << 1,
6+
B = 1 << 2,
7+
C = 1 << 3,
8+
}
9+
let n: number;
10+
let e: E = n; // ok because it's too inconvenient otherwise
11+
e = 0; // ok, in range
12+
e = 4; // ok, out of range, but allowed computed enums don't have all members
13+
let a: E.A = 0; // ok, A === 0
14+
a = 2; // error, 2 !== 0
15+
a = n; // ok
16+
17+
let c: Computed = n; // ok
18+
c = n; // ok
19+
c = 4; // ok
20+
let ca: Computed.A = 1; // error, Computed.A isn't a literal type because Computed has no enum literals
21+
22+
23+

tests/cases/compiler/enumLiteralAssignableToEnumInsideUnion.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,20 @@ module Y {
88
A, B
99
}
1010
}
11-
const y: X.Foo | boolean = Y.Foo.A;
11+
module Z {
12+
export enum Foo {
13+
A = 1 << 1,
14+
B = 1 << 2,
15+
}
16+
}
17+
module Ka {
18+
export enum Foo {
19+
A = 1 << 10,
20+
B = 1 << 11,
21+
}
22+
}
23+
const e1: X.Foo | boolean = Z.Foo.A; // ok
24+
const e2: X.Foo.A | X.Foo.B | boolean = Z.Foo.A; // ok, X.Foo is equivalent to X.Foo.A | X.Foo.B
25+
const e3: X.Foo.B | boolean = Z.Foo.A; // not legal
26+
const e4: X.Foo.A | boolean = Z.Foo.A; // not legal either because Z.Foo is computed and Z.Foo.A is not necessarily assignable to X.Foo.A
27+
const e5: Ka.Foo | boolean = Z.Foo.A; // ok

0 commit comments

Comments
 (0)