'Efficiently initialzing a Typescript Record without forcing a cast?

Given a type representing a series of strings

enum Weekday {
  MONDAY = 'monday',
  ...
  SUNDAY = 'sunday',
}

type Day = Weekday | 'today' | 'tomorrow';

and a type representing day attributes (eg)

type Weather = {
  temperature: number;
  humidity: number;
}

And now a Record combining the two:

type WeatherReport = Record<Day, Weather>;

I'd like to create an instance of WeatherReport with initial values (e.g.)

const report: WeatherReport = {
  monday: {temperature: 0, humidity: 0},
  tuesday: {temperature: 0, humidity: 0},
  wednesday: {temperature: 0, humidity: 0},
  thursday: {temperature: 0, humidity: 0},
  friday: {temperature: 0, humidity: 0},
  saturday: {temperature: 0, humidity: 0},
  sunday: {temperature: 0, humidity: 0},
  today: {temperature: 0, humidity: 0},
  tomorrow: {temperature: 0, humidity: 0},
}

but without all the boilerplate.

If necessary, I can have an Array<Day> with all possible Day values in it already.

For example, I wrote this but it obviously doesn't compile:

const report: WeatherReport = {};
for (const day of ARRAY_OF_EVERY_DAY_ELEMENT) {
  report[day] = {temperature: 0, humidity: 0};
}

but of course Typescript complains:

Type '{}' is missing the following properties from type 'Record<Day, Weather>': monday, tuesday, wednesday, ...`

Any thoughts on how to do this efficiently without forcing a cast from Partial<WeatherReport> to WeatherReport?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source