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:
Error: TS2476: A const enum member can only be accessed using a string literal.
export const enum Enum01
{
'01' = '01',
'02' = '01',
}
export enum Enum02
{
'01' = '01',
'02' = '01',
}
export let a1: keyof typeof Enum01
export let a2: keyof typeof Enum02
a1 === Enum01['01'];
a1 === '01';
a1 === '02';
a2 === Enum02['01'];
a2 === '01';
a2 === '02';
export let c1 = [
Enum01[a1],
// Error: TS2476: A const enum member can only be accessed using a string literal.
Enum01[a2],
// Error: TS2476: A const enum member can only be accessed using a string literal.
]
export let c2 = [
Enum02[a1],
Enum02[a2],
]
export declare const enum Enum01 {
'01' = "01",
'02' = "01"
}
export declare enum Enum02 {
'01' = "01",
'02' = "01"
}
export declare let a1: keyof typeof Enum01;
export declare let a2: keyof typeof Enum02;
export declare let c1: any[];
export declare let c2: Enum02[];
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Enum01;
(function (Enum01) {
Enum01["01"] = "01";
Enum01["02"] = "01";
})(Enum01 = exports.Enum01 || (exports.Enum01 = {}));
var Enum02;
(function (Enum02) {
Enum02["01"] = "01";
Enum02["02"] = "01";
})(Enum02 = exports.Enum02 || (exports.Enum02 = {}));
exports.a1 === "01" /* '01' */;
exports.a1 === '01';
exports.a1 === '02';
exports.a1 === '03';
exports.a2 === Enum02['01'];
exports.a2 === '01';
exports.a2 === '02';
exports.a2 === '03';
exports.c1 = [
Enum01[exports.a1],
Enum01[exports.a2],
];
exports.c2 = [
Enum02[exports.a1],
Enum02[exports.a2],
];
Object.keys(Enum01)
// Error: TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
Object.keys(Enum02)
import { $enum } from "ts-enum-util";
export const enum EnumMethod
{
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
PATCH = 'PATCH',
DELETE = 'DELETE',
HEAD = 'HEAD',
}
const methods = ([
EnumMethod.GET,
EnumMethod.POST,
EnumMethod.PUT,
EnumMethod.PATCH,
EnumMethod.DELETE,
EnumMethod.HEAD,
] as const);
$enum(EnumMethod)
Error:(53, 7) TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
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.
Most helpful comment