I have an object set up with my enum-like times of day and I am attempting to create the correct type for a record based on these entries.
export const TIMEOFDAY = {
FirstLight: 'First Light',
Morning: 'Morning',
Antemeridiem: 'Antemeridiem',
Zenith: 'Zenith',
Postmeridiem: 'Postmeridiem',
Evening: 'Evening',
LastLight: 'Last Light',
Night: 'Night',
}
When trying to define the record, it is indicating that all keys are missing in the record entries. TypeScript seems unable to recognize that I am referencing them using TIMEOFDAY.FirstLight
as shown below.
type TimeOfDayData = {
phase: number
hours: number
lux: number
temperature: number
}
type TimeOfDayDataRecord = Record<keyof typeof TIMEOFDAY, TimeOfDayData>
const TIMEOFDAYDATA: TimeOfDayDataRecord = {
[TIMEOFDAY.FirstLight]: /* */ { phase: 1, temperature: 10, hours: 5, lux: 1 },
[TIMEOFDAY.Morning]: /* */ { phase: 2, temperature: 23, hours: 8, lux: 100 },
[TIMEOFDAY.Antemeridiem]: /* */ { phase: 3, temperature: 42, hours: 13, lux: 300 },
[TIMEOFDAY.Zenith]: /* */ { phase: 4, temperature: 55, hours: 16, lux: 500 },
[TIMEOFDAY.Postmeridiem]: /* */ { phase: 3, temperature: 48, hours: 22, lux: 300 },
[TIMEOFDAY.Evening]: /* */ { phase: 2, temperature: 32, hours: 25, lux: 100 },
[TIMEOFDAY.LastLight]: /* */ { phase: 1, temperature: 15, hours: 30, lux: 1 },
[TIMEOFDAY.Night]: /* */ { phase: 0, temperature: -10, hours: 33, lux: 0 },
}
The error message displayed is:
Type '{ [x: string]: { phase: number; temperature: number; hours: number; lux: number; }; }' is missing the following properties from type 'TimeOfDayDataRecord': Morning, Antemeridiem, Zenith, Postmeridiem, and 4 more.ts(2740)
Any suggestions on how to resolve this issue or where the mistake may lie?