microsoft / microsoft/TypeScript
Type of reverse mappings of numeric enums is a `string` instead of `keyof typeof Enum`
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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