Skip to content

Enums can not be used for index signature types #13042

Closed

Description

TypeScript Version: 2.1.4

export interface UserInterfaceColors {
    [index in UserInterfaceElement]: ColorInfo;
}
export interface ColorInfo {
    r: number;
    g: number;
    b: number;
    a: number;
}
export enum UserInterfaceElement {
    ActiveTitleBar = 0,
    InactiveTitleBar = 1,
}

Expected behavior:
No errors will be thrown.

Actual behavior:
index in UserInterfaceElement throws the error Type 'UserInterfaceElement' is not assignable to type 'string'

This is a regression caused by #12425. The change makes sense, but there are very valid use cases for supporting enums in index signatures.

For another use case, in a project I'm working on, we use an enum to list all types of items. We have a function that takes an object with the enum values as keys, and data as values.

function loadItemData (data: { [index in ItemType]: string }): void;

A workaround is to use index: number, but then it's not enforced that you have to use a valid ItemType as a key.

Original Issue: #2491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions