I like the type-safety bit of using a Record in Typescript but seem to be in a bind with respect to looping through keys enums and populating the record
export enum Key {
A = 'A',
B = 'B',
C = 'C'
}
export interface Value {
isAvailable: boolean;
reasons: string[];
}
export type Access = Record<Key, Value>;
export function access() {
// I would like to avoid this initialization but TS does not allow it because it defeats the
// purpose and makes me initialize a value for each key in the enum upfront.
const featureAccess: Access = {
[Key.A]: null,
[Key.B]: null,
[Key.C]: null,
};
Object.keys(Key).forEach((eachKey: string) => {
const feature = Access[eachKey];
featureAccess[feature] = {
isAvailable: ..., // Populate from API
reasons: ...// Populate from API
};
});
return featureAccess;
}
Is this a wrong candidate for using the Typescript Record?