Skip to main content

What does Record<K, T> do?

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

FeaturePlain objectRecord<K, T>
Key typingnot strictstrict (via union)
Checking all fieldsnoyes
Convenience for dictionariesmoderateideal
Autocomplete supportlimitedfull

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

QuestionAnswer
What does Record<K, T> doCreates an object type where keys have type K and values have type T
How to choose keysVia a union or an enumeration (`'key1'
Does it check all fieldsYes, TypeScript requires all keys to be present
Can it be combinedYes, with Partial, Readonly, Pick, and others

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.