Imagine the scenario where an object type needs to be created and linked to a specific key specified in Record<Keys, Type>
.
Let's say there is a type called User
, which can have one of three values - user, admin, or moderator.
A new type called Stats needs to be defined with distinct properties for each of these values.
Unfortunately, using a union type does not produce the desired outcome:
type UserStats = {
timeOnline: string;
};
type AdminStats = {
timeAdmining: string;
};
type ModeratorStats = {
timeModerating: string;
};
type User = 'USER' | 'ADMIN' | 'MODERATOR';
type Stats = Record<User, UserStats | AdminStats | ModeratorStats>;
What would be the approach to achieve this goal?