Skip to content

Implicit index signatures for enum object types #31687

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 6 commits into from
Jun 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7316,7 +7316,8 @@ namespace ts {
stringIndexInfo = createIndexInfo(anyType, /*isReadonly*/ false);
}
}
const numberIndexInfo = symbol.flags & SymbolFlags.Enum ? enumNumberIndexInfo : undefined;
const numberIndexInfo = symbol.flags & SymbolFlags.Enum && (getDeclaredTypeOfSymbol(symbol).flags & TypeFlags.Enum ||
some(type.properties, prop => !!(getTypeOfSymbol(prop).flags & TypeFlags.NumberLike))) ? enumNumberIndexInfo : undefined;
setStructuredTypeMembers(type, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
// We resolve the members before computing the signatures because a signature may use
// typeof with a qualified name expression that circularly references the type we are
Expand Down Expand Up @@ -8191,6 +8192,9 @@ namespace ts {
propTypes.push(getTypeOfSymbol(prop));
}
}
if (kind === IndexKind.String) {
append(propTypes, getIndexTypeOfType(type, IndexKind.Number));
}
if (propTypes.length) {
return getUnionType(propTypes, UnionReduction.Subtype);
}
Expand Down Expand Up @@ -14596,7 +14600,7 @@ namespace ts {
* with no call or construct signatures.
*/
function isObjectTypeWithInferableIndex(type: Type) {
return type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.ValueModule)) !== 0 &&
return type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.Enum | SymbolFlags.ValueModule)) !== 0 &&
!typeHasCallOrConstructSignatures(type);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/baselines/reference/implicitIndexSignatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ function f4() {
const v3 = getNumberIndexValue(o1);
const v4 = getNumberIndexValue(o2);
}

function f5() {
enum E1 { A, B }
enum E2 { A = "A", B = "B" }
enum E3 { A = 0, B = "B" }
const v1 = getStringIndexValue(E1);
const v2 = getStringIndexValue(E2);
const v3 = getStringIndexValue(E3);
const v4 = getNumberIndexValue(E1);
const v5 = getNumberIndexValue(E2);
const v6 = getNumberIndexValue(E3);
}


//// [implicitIndexSignatures.js]
Expand Down Expand Up @@ -83,3 +95,26 @@ function f4() {
var v3 = getNumberIndexValue(o1);
var v4 = getNumberIndexValue(o2);
}
function f5() {
var E1;
(function (E1) {
E1[E1["A"] = 0] = "A";
E1[E1["B"] = 1] = "B";
})(E1 || (E1 = {}));
var E2;
(function (E2) {
E2["A"] = "A";
E2["B"] = "B";
})(E2 || (E2 = {}));
var E3;
(function (E3) {
E3[E3["A"] = 0] = "A";
E3["B"] = "B";
})(E3 || (E3 = {}));
var v1 = getStringIndexValue(E1);
var v2 = getStringIndexValue(E2);
var v3 = getStringIndexValue(E3);
var v4 = getNumberIndexValue(E1);
var v5 = getNumberIndexValue(E2);
var v6 = getNumberIndexValue(E3);
}
49 changes: 49 additions & 0 deletions tests/baselines/reference/implicitIndexSignatures.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,52 @@ function f4() {
>o2 : Symbol(o2, Decl(implicitIndexSignatures.ts, 39, 7))
}

function f5() {
>f5 : Symbol(f5, Decl(implicitIndexSignatures.ts, 44, 1))

enum E1 { A, B }
>E1 : Symbol(E1, Decl(implicitIndexSignatures.ts, 46, 15))
>A : Symbol(E1.A, Decl(implicitIndexSignatures.ts, 47, 13))
>B : Symbol(E1.B, Decl(implicitIndexSignatures.ts, 47, 16))

enum E2 { A = "A", B = "B" }
>E2 : Symbol(E2, Decl(implicitIndexSignatures.ts, 47, 20))
>A : Symbol(E2.A, Decl(implicitIndexSignatures.ts, 48, 13))
>B : Symbol(E2.B, Decl(implicitIndexSignatures.ts, 48, 22))

enum E3 { A = 0, B = "B" }
>E3 : Symbol(E3, Decl(implicitIndexSignatures.ts, 48, 32))
>A : Symbol(E3.A, Decl(implicitIndexSignatures.ts, 49, 13))
>B : Symbol(E3.B, Decl(implicitIndexSignatures.ts, 49, 20))

const v1 = getStringIndexValue(E1);
>v1 : Symbol(v1, Decl(implicitIndexSignatures.ts, 50, 9))
>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13))
>E1 : Symbol(E1, Decl(implicitIndexSignatures.ts, 46, 15))

const v2 = getStringIndexValue(E2);
>v2 : Symbol(v2, Decl(implicitIndexSignatures.ts, 51, 9))
>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13))
>E2 : Symbol(E2, Decl(implicitIndexSignatures.ts, 47, 20))

const v3 = getStringIndexValue(E3);
>v3 : Symbol(v3, Decl(implicitIndexSignatures.ts, 52, 9))
>getStringIndexValue : Symbol(getStringIndexValue, Decl(implicitIndexSignatures.ts, 11, 13))
>E3 : Symbol(E3, Decl(implicitIndexSignatures.ts, 48, 32))

const v4 = getNumberIndexValue(E1);
>v4 : Symbol(v4, Decl(implicitIndexSignatures.ts, 53, 9))
>getNumberIndexValue : Symbol(getNumberIndexValue, Decl(implicitIndexSignatures.ts, 13, 68))
>E1 : Symbol(E1, Decl(implicitIndexSignatures.ts, 46, 15))

const v5 = getNumberIndexValue(E2);
>v5 : Symbol(v5, Decl(implicitIndexSignatures.ts, 54, 9))
>getNumberIndexValue : Symbol(getNumberIndexValue, Decl(implicitIndexSignatures.ts, 13, 68))
>E2 : Symbol(E2, Decl(implicitIndexSignatures.ts, 47, 20))

const v6 = getNumberIndexValue(E3);
>v6 : Symbol(v6, Decl(implicitIndexSignatures.ts, 55, 9))
>getNumberIndexValue : Symbol(getNumberIndexValue, Decl(implicitIndexSignatures.ts, 13, 68))
>E3 : Symbol(E3, Decl(implicitIndexSignatures.ts, 48, 32))
}

59 changes: 59 additions & 0 deletions tests/baselines/reference/implicitIndexSignatures.types
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,62 @@ function f4() {
>o2 : { 0: string; 1: string; count: number; }
}

function f5() {
>f5 : () => void

enum E1 { A, B }
>E1 : E1
>A : E1.A
>B : E1.B

enum E2 { A = "A", B = "B" }
>E2 : E2
>A : E2.A
>"A" : "A"
>B : E2.B
>"B" : "B"

enum E3 { A = 0, B = "B" }
>E3 : E3
>A : E3.A
>0 : 0
>B : E3.B
>"B" : "B"

const v1 = getStringIndexValue(E1);
>v1 : string | E1
>getStringIndexValue(E1) : string | E1
>getStringIndexValue : <T>(map: { [x: string]: T; }) => T
>E1 : typeof E1

const v2 = getStringIndexValue(E2);
>v2 : E2
>getStringIndexValue(E2) : E2
>getStringIndexValue : <T>(map: { [x: string]: T; }) => T
>E2 : typeof E2

const v3 = getStringIndexValue(E3);
>v3 : string | E3.A
>getStringIndexValue(E3) : string | E3.A
>getStringIndexValue : <T>(map: { [x: string]: T; }) => T
>E3 : typeof E3

const v4 = getNumberIndexValue(E1);
>v4 : string
>getNumberIndexValue(E1) : string
>getNumberIndexValue : <T>(map: { [x: number]: T; }) => T
>E1 : typeof E1

const v5 = getNumberIndexValue(E2);
>v5 : unknown
>getNumberIndexValue(E2) : unknown
>getNumberIndexValue : <T>(map: { [x: number]: T; }) => T
>E2 : typeof E2

const v6 = getNumberIndexValue(E3);
>v6 : string
>getNumberIndexValue(E3) : string
>getNumberIndexValue : <T>(map: { [x: number]: T; }) => T
>E3 : typeof E3
}

8 changes: 4 additions & 4 deletions tests/baselines/reference/useObjectValuesAndEntries1.types
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ enum E { A, B }
>B : E.B

var entries5 = Object.entries(E); // [string, any][]
>entries5 : [string, any][]
>Object.entries(E) : [string, any][]
>entries5 : [string, string | E][]
>Object.entries(E) : [string, string | E][]
>Object.entries : { <T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][]; (o: {}): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][]; (o: {}): [string, any][]; }
>E : typeof E

var values5 = Object.values(E); // any[]
>values5 : any[]
>Object.values(E) : any[]
>values5 : (string | E)[]
>Object.values(E) : (string | E)[]
>Object.values : { <T>(o: { [s: string]: T; } | ArrayLike<T>): T[]; (o: {}): any[]; }
>Object : ObjectConstructor
>values : { <T>(o: { [s: string]: T; } | ArrayLike<T>): T[]; (o: {}): any[]; }
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/compiler/implicitIndexSignatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ function f4() {
const v3 = getNumberIndexValue(o1);
const v4 = getNumberIndexValue(o2);
}

function f5() {
enum E1 { A, B }
enum E2 { A = "A", B = "B" }
enum E3 { A = 0, B = "B" }
const v1 = getStringIndexValue(E1);
const v2 = getStringIndexValue(E2);
const v3 = getStringIndexValue(E3);
const v4 = getNumberIndexValue(E1);
const v5 = getNumberIndexValue(E2);
Copy link
Member

Choose a reason for hiding this comment

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

Why is this unknown and not an error? (Is it just because inference fails and "a thing with an inferrable index" is assignable to a {[x: number]: unknown}?) I'd think that yeah, E2 has an inferrable index type, sure, but we know that it has no numbers in it at all. But, from the stance that the number index signature (were it present in the type) would need to be assignable to the string index, this'd need to be E2.

Copy link
Member Author

Choose a reason for hiding this comment

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

Because we infer nothing for T in the call to getNumberIndexValue. Keep in mind we're not doing an assignment here, we're just doing inference.

Copy link
Member

Choose a reason for hiding this comment

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

Well, we are doing an assignment - we're saying that a {[x: string]: E2} is assignable to a {[x: number]: unknown}. I guess the output type here just irks me a tad - it could only possibly be an E2 or keyof typeof E2, rather than an unknown.

Like, if I write enum StrNum { Zero = "0", One = "1" }, indexing by 0 or 1 is gunna get me Zero or One, likewise if I write enum NumStr { "0" = "Zero", "1" = "One" }, indexing by Zero or One is gunna get me "0" or "1".

Copy link
Member Author

Choose a reason for hiding this comment

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

No, indexing your StrNum by 0 or 1 is going to get you undefined (there's no reverse mapping for string valued members), and NumStr is an error because members can't have numeric names.

Copy link
Member Author

Choose a reason for hiding this comment

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

Anyway, the unknown type is consistent with what we infer for object literal types that have no numerically named members. In some sense it would be more consistent to return undefined since that's what you're going to get, but that really is an orthogonal issue.

Copy link
Member

Choose a reason for hiding this comment

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

there's no reverse mapping for string valued member

I always forget about that.

Anyway, the unknown type is consistent with what we infer for object literal types that have no numerically named members

Oh, ick. Ok.

const v6 = getNumberIndexValue(E3);
}