Record<K, V> is a utility type that creates an object where K is the key and all values are of type V. In the example below, Permission is of type Record with UserType key and values of type boolean. With Record, you can define values that should comply with the defined types. One use case is to use as a configuration object.
type UserType = "admin" | "editor" | "viewer";
type Permission = Record<UserType, boolean>;
const permissions: Permission = {
admin: true,
editor: true,
viewer: false,
};
function canAccessDashboard(role: UserType, perms: Permission) {
return perms[role];
}
console.log(canAccessDashboard("admin", permissions)); // true
console.log(canAccessDashboard("editor", permissions)); // true
console.log(canAccessDashboard("viewer", permissions)); // false