Last active
January 26, 2025 19:41
-
-
Save rauschma/c29e128eeb3989b179ce10b4bfd2e561 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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