I've been working on this code snippet:
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]
}
But I'm unsure about its functionality and whether it will actually work at runtime. It's crucial for my database to only accept Enum-compliant values, so this could be a potential issue. I would like to catch any errors in my code before they impact the database.