microsoft / microsoft/TypeScript

map through enum

Open
#28,565 6 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

TypeScript Version: 3.1.6

Search Terms: enum

Code

enum ABC {
  A = 'A',
  B = 'B',
  C = 'C',
}
// No Error
for (const key in ABC) {
  console.log(ABC[key]);
}
// Error: Element implicitly has an 'any' type because index expression is not of type 'number'
Object.keys(ABC).map(key => console.log(ABC[key]));

Expected behavior:
Object.keys, values, entries on enum ABC infer type of ABC's key and value.
Actual behavior:
Cannot get inferred type.

Shouldn't type of key from enum be 'A' | 'B' | 'C'?

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 by reproducing the enum and Object.keys example from the issue with TypeScript 3.1.6, then compare the inferred types for the for-in and Object.keys cases. The work is done when Object.keys, values, and entries provide the expected enum key and value types without an implicit-any error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.