I don’t understand all these comments. I use TS enums like I use Java enums and I literally never have issues. What are y’all doing with these?
Maybe people should become familiar with Grug: https://grugbrain.dev/
I'll take the t-rex.
> apex predator of grug is complexity. complexity bad. say again: complexity very bad. you say now: complexity very, very bad. given choice between complexity or one on one against t-rex, grug take t-rex: at least grug see t-rex
// in some util.ts or helpers.ts
export function keysOf<T extends string>(obj: { [k in T]: unknown }): readonly T[] {
return Object.keys(obj) as unknown[] as T[];
}
//---
const kOperationsFunctions = {
add(a: number, b: number) { return a + b; },
subtract(a: number, b: number) { return a - b; },
multiply(a: number, b: number) { return a * b; },
divide(a: number, b: number) { return a / b; },
};
const kOperations = keysOf(kOperationsFunctions);
type Operation = (typeof kOperations)[number];
I don't want to declare an enum here. That would just be a waste of typing and an opportunity for things to get out of sync.