enum PAGES {
HOME = 'HOME',
CONTACT = 'CONTACT',
}
export const links: { [key: string]: string } = {
[PAGES.HOME]: '/home',
[PAGES.CONTACT]: '/contact',
};
export function getLink(page: string) {
return BASE_URL + links[page];
}
Is there a more efficient way to define the enum constants without repeating the string values?
I noticed that my code has redundant strings in defining the enums.
Exploring the use of mapped types:
type PAGE_KEY = 'HOME' | 'CONTACT';
export const urls: { [key: string]: string } = {
HOME: '/home-page',
CONTACT: '/contact-us/?version=v2',
};
export function getLink(page: PAGE_KEY) {
return BASE_URL + urls[page];
}