Comment

Comment by jasonkester

This got me wondering what I actually do in practice. I think it's this:

  const KnownFlavors {
    Vanilla: "Vanilla",
    Chocolate: "Chocolate",
    Strawberry: "Strawberry"
  }
Then, use a string to hold the actual value.

  doug.favoriteFlavor = KnownFlavors.Chocolate;
  cindy.favoriteFlavor = "Mint"

  case: KnownFlavors.Chocolate:
Expand your list of known flavors whenever you like, your system will still always hold valid data. You get all the benefits of typo-proofing your code, switching on an enum, etc., without having to pile on any wackiness to fool your compiler or keep the data normalized.

It acknowledges the reality that a non-exhaustive enum isn’t really an enum. It’s just a list of things that people might type into that field.

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton