Let's say we have enum defined as such:
export enum WeekDayEnum { sunday = 0, monday, tuseday, wednesday, thursday, friday, saturday, } export type DayOfWeek = | "monday" | "tuseday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday";
Usage 1: pass number value as lookup for string key
export const getToday = (_date?: Date) => { const date: Date = _date || new Date(); const year = date.getFullYear(); const month = date.getMonth(); const day = date.getDate(); return new Date(year, month, day); }; export const isSameWeekDay = (day: DayOfWeek, _date?: Date) => { const today = getToday(_date).getDay(); return WeekDayEnum[today] === day; };
'today' is a number type, so :
WeekDayEnum[today] // 'monday'
usage 2: pass string key to lookup number value
const getTodayOpeningTime = ( timeTable: WeeklyTimeTableVM[] ): WeeklyTimeTableVM => { const todayNum = getToday().getDay() as weeksNumber; const todayStr = WeekDayEnum[todayNum]; return timeTable.find((day) => day[0] === todayStr) as WeeklyTimeTableVM; };