-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
proposal:symbol enum #18408
Comments
We first need a way to treat |
I see #15473 merged, does it mean we can use Symbols in Enum somehow? |
Not to bikeshed, but wouldn't a more consistent syntax be: enum Example: Symbol {
X,Y,Z
}
// or
enum Example<Symbol> {
X,Y,Z
} Rather than introducing a new keyword of |
This would be very useful with Redux actions... |
Is there something that would help move this issue? |
I've found an alternative way to do it, and I hope it helps: /**
* Action types for StoreState.todo
*/
export namespace TodoActionTypes {
// Adding todo item
export const ADD_ITEM = Symbol('@todo/ADD_ITEM')
export type ADD_ITEM = typeof ADD_ITEM
// Removing todo item
export const DEL_ITEM = Symbol('@todo/DEL_ITEM')
export type DEL_ITEM = typeof DEL_ITEM
// Performing undo on StoreState.todo
export const UNDO = Symbol('@todo/UNDO')
export type UNDO = typeof UNDO
// Performing redo on StoreState.todo
export const REDO = Symbol('@todo/REDO')
export type REDO = typeof REDO
}
/**
* Action types for StoreState.todo
*/
export type TodoActionTypes =
| TodoActionTypes.ADD_ITEM
| TodoActionTypes.DEL_ITEM
| TodoActionTypes.UNDO
| TodoActionTypes.REDO |
i guess this will do?
|
I have found another solution at https://fettblog.eu/symbols-in-javascript-and-typescript/ and I slightly improved it. Sadly, there is lot of code around it, but it gives pretty protection (compile time and runtime) const COLOR_RED = Symbol('COLOR_RED');
const COLOR_ORANGE = Symbol('COLOR_ORANGE');
// intentionally not part of the enum for demonstration
const COLOR_GREEN = Symbol('COLOR_GREEN');
// freeze needed for runtime protection
export const colors = Object.freeze({
COLOR_RED,
COLOR_ORANGE,
} as const);
type ValuesWithKeys<T, K extends keyof T> = T[K];
type Values<T> = ValuesWithKeys<T, keyof T>
export type COLOR = Values<typeof colors>;
function foo(val: COLOR) {
}
foo(COLOR_RED);
foo(colors.COLOR_ORANGE);
foo(COLOR_GREEN); // refused by typescript |
To continue on the same vein as this thread, I think enum values should be symbolic by default rather than integers. That's the whole point of enums to begin with. It would be best if developers don't even know the underlying value but rather treat enums as a union of interrelated Symbols. It would promote developers to have much better coding standards. So something like enum TrafficColors = {
Red,
Yellow,
Green
} would be best if translated into something like var TrafficColors = {
Red: Symbol('Red'),
Yellow: Symbol('Yellow'),
Green: Symbol('Green')
} with keys used as the arguments in Symbol constructors to have google error reporting at runtime. |
I want symbol enum.
syntax example:
↓
The text was updated successfully, but these errors were encountered: