microsoft / microsoft/TypeScript

Enum declarations should consistently disallow computed keys

Open
#42,468 6 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Error Messages
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

🔎 Search Terms
  • Computed property names are not allowed in enums
  • enum declaration computed
🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about enums
⏯ Playground Link

Playground link with relevant code

💻 Code
let y = "y";
type z = "z";

enum Foo {
    ['x'],
    [{}],
    [y],
    [z]
}
🙁 Actual behavior

TS reports Computed property names are not allowed in enums. for [{}], [y] and [z], while it allows ['x'] (in contraddiction with the error message).

🙂 Expected behavior

All of them should be an error, for the reason reported by the error.

📚 Context

This came up at https://github.com/babel/babel/issues/12683: we can implement the current TS behavior in Babel, but I'd first like to check if TS is expected to report an error for ['x'].

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

Reproduce the behavior from the linked TypeScript Playground using the enum sample with ['x'], [{}], [y], and [z]. Trace the enum computed-property diagnostic from that example; done means all four declarations consistently produce the reported error, with coverage for the demonstrated cases.

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.