Skip to content

Commit 278dcc6

Browse files
authored
Merge pull request #18029 from Microsoft/allow-string-enum-in-element-access
Allow string enum in element access
2 parents bab287d + 8087206 commit 278dcc6

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7685,8 +7685,10 @@ namespace ts {
76857685
return type;
76867686
}
76877687
// In the following we resolve T[K] to the type of the property in T selected by K.
7688+
// We treat boolean as different from other unions to improve errors;
7689+
// skipping straight to getPropertyTypeForIndexType gives errors with 'boolean' instead of 'true'.
76887690
const apparentObjectType = getApparentType(objectType);
7689-
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Primitive)) {
7691+
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Boolean)) {
76907692
const propTypes: Type[] = [];
76917693
for (const t of (<UnionType>indexType).types) {
76927694
const propType = getPropertyTypeForIndexType(apparentObjectType, t, accessNode, /*cacheSymbol*/ false);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [stringEnumInElementAccess01.ts]
2+
enum E {
3+
A = "a",
4+
B = "b",
5+
C = "c",
6+
}
7+
8+
interface Item {
9+
a: string;
10+
b: number;
11+
c: boolean;
12+
}
13+
14+
declare const item: Item;
15+
declare const e: E;
16+
const snb: string | number | boolean = item[e];
17+
18+
19+
//// [stringEnumInElementAccess01.js]
20+
var E;
21+
(function (E) {
22+
E["A"] = "a";
23+
E["B"] = "b";
24+
E["C"] = "c";
25+
})(E || (E = {}));
26+
var snb = item[e];
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/conformance/expressions/elementAccess/stringEnumInElementAccess01.ts ===
2+
enum E {
3+
>E : Symbol(E, Decl(stringEnumInElementAccess01.ts, 0, 0))
4+
5+
A = "a",
6+
>A : Symbol(E.A, Decl(stringEnumInElementAccess01.ts, 0, 8))
7+
8+
B = "b",
9+
>B : Symbol(E.B, Decl(stringEnumInElementAccess01.ts, 1, 12))
10+
11+
C = "c",
12+
>C : Symbol(E.C, Decl(stringEnumInElementAccess01.ts, 2, 12))
13+
}
14+
15+
interface Item {
16+
>Item : Symbol(Item, Decl(stringEnumInElementAccess01.ts, 4, 1))
17+
18+
a: string;
19+
>a : Symbol(Item.a, Decl(stringEnumInElementAccess01.ts, 6, 16))
20+
21+
b: number;
22+
>b : Symbol(Item.b, Decl(stringEnumInElementAccess01.ts, 7, 14))
23+
24+
c: boolean;
25+
>c : Symbol(Item.c, Decl(stringEnumInElementAccess01.ts, 8, 14))
26+
}
27+
28+
declare const item: Item;
29+
>item : Symbol(item, Decl(stringEnumInElementAccess01.ts, 12, 13))
30+
>Item : Symbol(Item, Decl(stringEnumInElementAccess01.ts, 4, 1))
31+
32+
declare const e: E;
33+
>e : Symbol(e, Decl(stringEnumInElementAccess01.ts, 13, 13))
34+
>E : Symbol(E, Decl(stringEnumInElementAccess01.ts, 0, 0))
35+
36+
const snb: string | number | boolean = item[e];
37+
>snb : Symbol(snb, Decl(stringEnumInElementAccess01.ts, 14, 5))
38+
>item : Symbol(item, Decl(stringEnumInElementAccess01.ts, 12, 13))
39+
>e : Symbol(e, Decl(stringEnumInElementAccess01.ts, 13, 13))
40+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
=== tests/cases/conformance/expressions/elementAccess/stringEnumInElementAccess01.ts ===
2+
enum E {
3+
>E : E
4+
5+
A = "a",
6+
>A : E.A
7+
>"a" : "a"
8+
9+
B = "b",
10+
>B : E.B
11+
>"b" : "b"
12+
13+
C = "c",
14+
>C : E.C
15+
>"c" : "c"
16+
}
17+
18+
interface Item {
19+
>Item : Item
20+
21+
a: string;
22+
>a : string
23+
24+
b: number;
25+
>b : number
26+
27+
c: boolean;
28+
>c : boolean
29+
}
30+
31+
declare const item: Item;
32+
>item : Item
33+
>Item : Item
34+
35+
declare const e: E;
36+
>e : E
37+
>E : E
38+
39+
const snb: string | number | boolean = item[e];
40+
>snb : string | number | boolean
41+
>item[e] : string | number | boolean
42+
>item : Item
43+
>e : E
44+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @noImplicitAny: true
2+
enum E {
3+
A = "a",
4+
B = "b",
5+
C = "c",
6+
}
7+
8+
interface Item {
9+
a: string;
10+
b: number;
11+
c: boolean;
12+
}
13+
14+
declare const item: Item;
15+
declare const e: E;
16+
const snb: string | number | boolean = item[e];

0 commit comments

Comments
 (0)