Suggest an editImprove this articleRefine the answer for “What does Record<K, T> do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Record<K, T>`** creates an object type whose **keys** have type `K` and whose **values** have type `T`. **Key point:** TypeScript strictly checks that all keys from `K` are present and that their values match type `T`.Shown above the full answer for quick recall.Answer (EN)Image## 1. What `Record<K, T>` does > `Record<K, T>` creates an object type > whose **keys** have type `K` and whose **values** have type `T`. --- ### Syntax ```javascript Record<KeyType, ValueType> ``` --- ### Example ```javascript type Roles = Record<string, number>; const userRoles: Roles = { admin: 1, user: 2, guest: 3, }; ``` > Here: > > - all keys are `string`; > - all values are `number`. --- ## 2. Example with specific keys ```javascript type Status = 'pending' | 'success' | 'error'; type StatusMessages = Record<Status, string>; const messages: StatusMessages = { pending: 'Loading...', success: 'Done', error: 'Error', }; ``` > TypeScript strictly checks that all keys are present (`pending`, `success`, `error`) > and the type of their values (`string`). --- ## 3. How `Record<K, T>` is built internally ```javascript type Record<K extends keyof any, T> = { [P in K]: T; }; ``` Breakdown: - `K` is the set of allowed keys (`string | number | symbol`); - `T` is the value type; - `[P in K]` creates an object with properties `P` from the set `K`; - `: T` specifies that every property has type `T`. --- ## 4. Usage with `union` types `Record` automatically expands every variant of `K` into a key. ```javascript type Page = 'home' | 'about' | 'contact'; type PageInfo = Record<Page, { title: string }>; const pages: PageInfo = { home: { title: 'Home' }, about: { title: 'About' }, contact: { title: 'Contact' }, }; ``` > TypeScript checks that you did not skip any page and did not add an extra one. --- ## 5. Comparing `Record` with a plain object | Feature | Plain object | `Record<K, T>` | |---|---|---| | Key typing | not strict | strict (via union) | | Checking all fields | no | yes | | Convenience for dictionaries | moderate | ideal | | Autocomplete support | limited | full | --- ## 6. Practical examples ### Example 1. A settings map ```javascript type SettingKey = 'theme' | 'language' | 'timezone'; type Settings = Record<SettingKey, string>; const settings: Settings = { theme: 'dark', language: 'uk', timezone: 'Europe/Kyiv', }; ``` --- ### Example 2. A counter by id ```javascript const scores: Record<number, number> = { 1: 10, 2: 25, 3: 40, }; ``` --- ### Example 3. A translation dictionary ```javascript type Locale = 'en' | 'de'; type Translations = Record<Locale, string>; const greeting: Translations = { en: 'Hello', de: 'Hallo', }; ``` --- ### Example 4. A typed object by enum ```javascript enum Role { Admin = 'admin', User = 'user', } type RolePermissions = Record<Role, string[]>; const permissions: RolePermissions = { [Role.Admin]: ['create', 'edit', 'delete'], [Role.User]: ['view'], }; ``` --- ## 7. Combining with other Utility Types You can combine `Record` with `Partial`, `Readonly`, and so on. ```javascript type PartialConfig = Partial<Record<'host' | 'port' | 'ssl', string>>; const config: PartialConfig = { host: 'localhost', ssl: 'true', }; ``` > Now not all properties have to be specified. --- ## 8. Errors with invalid keys TypeScript will not allow adding a key that does not exist: ```javascript type Roles = 'admin' | 'user'; type RoleLabels = Record<Roles, string>; const labels: RoleLabels = { admin: 'Administrator', user: 'User', guest: 'Guest', // Error - "guest" is not part of Roles }; ``` --- ## Summary | Question | Answer | |---|---| | What does `Record<K, T>` do | Creates an object type where keys have type `K` and values have type `T` | | How to choose keys | Via a `union` or an enumeration (`'key1' | | Does it check all fields | Yes, TypeScript requires all keys to be present | | Can it be combined | Yes, with `Partial`, `Readonly`, `Pick`, and others |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.