Skip to content

Use in keyword for enums #14084

Closed
Closed
@pocesar

Description

@pocesar

Although enums are both numbers and strings, it's not possible to use a type like this:

enum Values {
  A, B, C
}

type ValuesConstraint = {
  [index: Values]: string; // An index signature parameter type must be 'string' or 'number'
}

const Mapped: ValuesConstraint = {
   [Values.A]: 'Alpha',
   [Values.B]: 'Beta',
   [Values.C]: 'Gamma'
}

const f = Mapped[Values.C] // f: any

Tried to use in as in [index in Values] but it says Type 'Values' is not assignable to type 'string'. Trying to use a type ValuesType = Values.A | Values.B | Values.C also doesn't work, is there any workaround?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions