Hacker News new | ask | show | jobs
by sirlone 519 days ago
I can't remember the last time I needed enums. Usually they fall out of the code in one way or another

    // 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.