Creates an object type with a specific set of keys, all having the same value type.
type RecordType = Record<KeyType, ValueType>;
type UserMap = Record<string, string>;
const users: UserMap = {
"user_1": "Alice",
"user_2": "Bob",
"user_3": "Charlie"
};
*// Status code meanings*
type HttpStatusMessages = Record<number, string>;
const statusMessages: HttpStatusMessages = {
200: "OK",
404: "Not Found",
500: "Internal Server Error"
};
*// Role permissions using union type for keys*
type Role = 'admin' | 'editor' | 'viewer';
type RolePermissions = Record<Role, boolean>;
const permissions: RolePermissions = {
admin: true,
editor: true,
viewer: false
*// All three keys MUST be present*
};
typescript*// Mapping product SKUs to product info*
type ProductInfo = {
name: string;
price: number;
};
type ProductCatalog = Record<string, ProductInfo>;
const catalog: ProductCatalog = {
"SKU001": { name: "Laptop", price: 999 },
"SKU002": { name: "Mouse", price: 25 }
};
{ [key: string]: ValueType