Your second example is not how anyone here is recommending using an object to replace a switch. You've still got the function call, which is now redundant.
Here's how you'd actually do it:
type CountryCode =
| "CH"
| "US";
const countryNames: Record<CountryCode, string> = {
"CH": "Switzerland",
"US": "United States of America",
}
Your second example is way more complicated than your first, but this one is even easier to read (at least for me), and still provides all the same functionality and type safety (including exhaustiveness checking).
Here's how you'd actually do it:
Your second example is way more complicated than your first, but this one is even easier to read (at least for me), and still provides all the same functionality and type safety (including exhaustiveness checking).