News for hackers. Stuff that matters.
Re:TypeScript enums: use cases and alternatives by Klaster_1 on Monday January 20, 2025 @05:50AM (#42767312)
Often, I find myself in need to find all references of "Active" from your example, which doesn't work with union values. This looks like a LSP limitation. Of course, you can move assign values into consts and union these instead. But that means you are half way there to custom run-time enums, and all the way after you wrap the consts with an object in order to enumerate over values at run-time.
Re:TypeScript enums: use cases and alternatives by mistercow on Monday January 20, 2025 @06:44AM (#42767664)
> Often, I find myself in need to find all references of "Active" from your example, which doesn't work with union values.

I'm able to do that just fine in VS Code / Cursor.

I set up a union like this:

    export type TestUnion = 'foo' | 'bar' | 'baz';
Then use it in another file like this:

    const bar: TestUnion = 'bar';
    const barString: string = 'bar';
If I select 'bar' from the type and choose "Go to references", it shows me the `const bar` line, but not the `const barString` line, which is what I would expect.
Re:TypeScript enums: use cases and alternatives by homebrewer on Monday January 20, 2025 @08:37AM (#42768584)
Use `const enum Foo`, they leave no traces in the transpiled JS and provide good IDE experience.