microsoft / microsoft/TypeScript

Type of reverse mappings of numeric enums is a `string` instead of `keyof typeof Enum`

Open
#38,806 6 comments 23 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.0.0-dev.20200526

Search Terms:
enum
numeric enum
reverse enum

Code

enum Enum {
  one = 1,
  two = 2,
}

const a: keyof typeof Enum = Enum[Enum.one]; // (1)
const b = Enum[0]; // (2)
console.log(a, b);

Expected behavior:
When a numeric enum is indexed by itself the value should be of type keyof typeof Enum:
No error on line (1).
Error Property '0' does not exist on type 'typeof Enum' on line (2).

Actual behavior:
When a numeric enum is indexed by a number (including itself) the value is of type string:
Error Type 'string' is not assignable to type '"one" | "two"' on line (1).
No error on line (2).

Playground Link: Provided

Related Issues:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Playground link and the minimal numeric-enum reproduction in the issue. Verify the indexed access types against the expected behavior: Enum[Enum.one] should be keyof typeof Enum, while Enum[0] should report that property 0 does not exist on typeof Enum.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.