Skip to content

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

Open
bluelovers opened this issue May 11, 2019 · 3 comments
Open

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

bluelovers opened this issue May 11, 2019 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@bluelovers
Copy link
Contributor

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:

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels May 13, 2019
@bluelovers
Copy link
Contributor Author

bluelovers commented May 26, 2019

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)

@bluelovers
Copy link
Contributor Author

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.

@Harpush
Copy link

Harpush commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants