microsoft / microsoft/TypeScript

when preserveConstEnums = true should allow use enum[key]

Open
#31,353 3 comments 9 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: 3.4.0-dev.201xxxxx

Search Terms:

Code

preserveConstEnums = true

const enum EnumCacheName
{
	'toc_contents' = '.toc_contents.cache'
}

let { target } = yargs
	.option('target', {
		type: 'string',
	})
	.argv
;


let cache_file = path.join(ProjectConfig.cache_root, EnumCacheName[target as any]);

Expected behavior:

no error, because this code exists in .js

var EnumCacheName;
(function (EnumCacheName) {
    EnumCacheName["toc_contents"] = ".toc_contents.cache";
})(EnumCacheName || (EnumCacheName = {}));

Actual behavior:

Error: TS2476: A const enum member can only be accessed using a string literal.

Playground Link:

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 by reproducing the reported TS2476 error with the const enum and indexed access shown in the issue, using preserveConstEnums=true. Inspect the compiler path that checks const enum member access and compare it with the expected JavaScript output. Done means this code compiles without the error and preserves the expected runtime lookup behavior.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.