What is the Record utility type?
Why Interviewers Ask This
Mid-level TypeScript roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.
Answer
Record<K, V> creates an object type where the keys are of type K and the values are of type V. Implementation: type Record<K extends keyof any, V> = { [P in K]: V }. Simple usage: type StringToNumber = Record<string, number>; — an object with any string keys and number values. With literal key types: type UserRoles = Record<"admin" | "editor" | "viewer", boolean>; — exactly these three keys, all boolean values. Common use case — mapping an enum to values: type PageMeta = Record<Routes, { title: string; description: string }>. Record is essentially a cleaner alternative to an index signature ({ [key: string]: V }) when the keys are known. Unlike index signatures, Record with literal keys enforces that all specified keys are present. Combine with Partial for optional keys: Partial<Record<K, V>>.
Pro Tip
This topic has TypeScript-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.