Skip to content

when preserveConstEnums = true should allow use enum[key] #31353

Open
@bluelovers

Description

@bluelovers

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions