Skip to content

Allow string enum in element access #18029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7688,7 +7688,7 @@ namespace ts {
}
// In the following we resolve T[K] to the type of the property in T selected by K.
const apparentObjectType = getApparentType(objectType);
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Primitive)) {
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Boolean)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment explaining that we are avoiding TypeFlags.Boolean here for stylistic reasons to ensure that we show boolean and not true in error messages? Might also want to link to keyofAndIndexedAccessErrors.errors.txt, which is the only test that fails (i.e., has slightly different error messages) if this check isn't here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const propTypes: Type[] = [];
for (const t of (<UnionType>indexType).types) {
const propType = getPropertyTypeForIndexType(apparentObjectType, t, accessNode, /*cacheSymbol*/ false);
Expand Down
26 changes: 26 additions & 0 deletions tests/baselines/reference/stringEnumInElementAccess01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [stringEnumInElementAccess01.ts]
enum E {
A = "a",
B = "b",
C = "c",
}

interface Item {
a: string;
b: number;
c: boolean;
}

declare const item: Item;
declare const e: E;
const snb: string | number | boolean = item[e];


//// [stringEnumInElementAccess01.js]
var E;
(function (E) {
E["A"] = "a";
E["B"] = "b";
E["C"] = "c";
})(E || (E = {}));
var snb = item[e];
40 changes: 40 additions & 0 deletions tests/baselines/reference/stringEnumInElementAccess01.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/conformance/expressions/elementAccess/stringEnumInElementAccess01.ts ===
enum E {
>E : Symbol(E, Decl(stringEnumInElementAccess01.ts, 0, 0))

A = "a",
>A : Symbol(E.A, Decl(stringEnumInElementAccess01.ts, 0, 8))

B = "b",
>B : Symbol(E.B, Decl(stringEnumInElementAccess01.ts, 1, 12))

C = "c",
>C : Symbol(E.C, Decl(stringEnumInElementAccess01.ts, 2, 12))
}

interface Item {
>Item : Symbol(Item, Decl(stringEnumInElementAccess01.ts, 4, 1))

a: string;
>a : Symbol(Item.a, Decl(stringEnumInElementAccess01.ts, 6, 16))

b: number;
>b : Symbol(Item.b, Decl(stringEnumInElementAccess01.ts, 7, 14))

c: boolean;
>c : Symbol(Item.c, Decl(stringEnumInElementAccess01.ts, 8, 14))
}

declare const item: Item;
>item : Symbol(item, Decl(stringEnumInElementAccess01.ts, 12, 13))
>Item : Symbol(Item, Decl(stringEnumInElementAccess01.ts, 4, 1))

declare const e: E;
>e : Symbol(e, Decl(stringEnumInElementAccess01.ts, 13, 13))
>E : Symbol(E, Decl(stringEnumInElementAccess01.ts, 0, 0))

const snb: string | number | boolean = item[e];
>snb : Symbol(snb, Decl(stringEnumInElementAccess01.ts, 14, 5))
>item : Symbol(item, Decl(stringEnumInElementAccess01.ts, 12, 13))
>e : Symbol(e, Decl(stringEnumInElementAccess01.ts, 13, 13))

44 changes: 44 additions & 0 deletions tests/baselines/reference/stringEnumInElementAccess01.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
=== tests/cases/conformance/expressions/elementAccess/stringEnumInElementAccess01.ts ===
enum E {
>E : E

A = "a",
>A : E.A
>"a" : "a"

B = "b",
>B : E.B
>"b" : "b"

C = "c",
>C : E.C
>"c" : "c"
}

interface Item {
>Item : Item

a: string;
>a : string

b: number;
>b : number

c: boolean;
>c : boolean
}

declare const item: Item;
>item : Item
>Item : Item

declare const e: E;
>e : E
>E : E

const snb: string | number | boolean = item[e];
>snb : string | number | boolean
>item[e] : string | number | boolean
>item : Item
>e : E

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @noImplicitAny: true
enum E {
A = "a",
B = "b",
C = "c",
}

interface Item {
a: string;
b: number;
c: boolean;
}

declare const item: Item;
declare const e: E;
const snb: string | number | boolean = item[e];