microsoft / microsoft/TypeScript
when preserveConstEnums = true should allow use enum[key]
Nobody has claimed this yet.
- 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
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 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