Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active January 26, 2025 19:41
Show Gist options
  • Save rauschma/c29e128eeb3989b179ce10b4bfd2e561 to your computer and use it in GitHub Desktop.
Save rauschma/c29e128eeb3989b179ce10b4bfd2e561 to your computer and use it in GitHub Desktop.
// Q: Why not an object literal?
// A: Then you have to create separate constants for the symbols:
// https://2ality.com/2025/01/typescript-enum-patterns.html#using-symbols-as-property-values
type PropertyValues<Obj> = Obj[keyof Obj];
function createEnum<C extends new (...args: unknown[]) => unknown>(enumClass: C): Omit<C, 'prototype'> {
return enumClass;
}
const Color = createEnum(class {
static readonly Red = Symbol('Red');
static readonly Green = Symbol('Green');
});
type TColor = PropertyValues<typeof Color>;
// unique symbol | unique symbol
type X = keyof typeof Color;
// "Red" | "Green"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment