Let’s say I have a list of countries that are country coded and I want to display a proper word for each code that I receive as props for my component.
interface MyComponentProps {
countryCode: 'en' | 'de' | 'fr';
}
const MyComponent: FC<MyComponentProps> = ({countryCode}) => (
<p>{countryCode}</p>
)
So in the component above I’d like to display for example English when I get ‘en’ as props, German for de… you get the idea. What is the best way convert these values into other values according to some map like ‘en’ = ‘English’, ‘de’ = ‘German’ etc?
2
Answers
Just use an object literal:
Playground
Note that the
satisfies
clause will cause the compiler to warn you if you forget a key in the map. If you don’t want that completeness checking you can delete that bit.I personally call it "dictionary"