-
Notifications
You must be signed in to change notification settings - Fork 12.8k
when preserveConstEnums = true should allow use enum[key] #31353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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)
|
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. |
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
Code
preserveConstEnums = true
Expected behavior:
no error, because this code exists in .js
Actual behavior:
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: