Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript
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:
Reacted by Chunlei Duan, Yusei Fujimatsu, Harpush, David Alan Hjelle, SPGoding, Misode, Ivan Goncharov, Kiryl Dziamura and Eduardo Sanchez
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript
Activity
bluelovers commentedon May 26, 2019
bluelovers commentedon Jun 8, 2019
Harpush commentedon Oct 23, 2020
I have the same issue... I thought the flag is meant to take the "best" from both worlds... which means regular const enum accessing will be replaced with literal by the compiler while "runtime" access will stay as is in runtime and won't error.
Given this playground we can see the javascript emitted will run but typescript still complains.