This is the code I already have:
const Locales = {
en_gb: 'en-gb',
en_us: 'en-us',
} as const
type ApiLocales = typeof Locales[keyof typeof Locales]
type DatabaseLocales = keyof typeof Locales
function databaseLanguageCodeFromString(languageString: ApiLocales): DatabaseLocales[ApiLocales] {
return {
'en-gb': 'en_gb',
'en-us': 'en_us',
}[languageString]
}
However, I'm not quite sure how it works, whether it works, will it work at runtime? My database won't accept values that don't conform to an Enum, so perhaps this isn't critical. Although, it'd be good for my code to catch problems before they reach the database.