The Record type helps you create an object with specific keys and values of a certain type. It keeps your data organized and safe.
Record type in Typescript
Introduction
Syntax
Typescript
Record<Keys, Type>
Keys can be a union of string literals or string or number.
Type is the type of the values for each key.
Examples
Typescript
type Scores = Record<'math' | 'english' | 'science', number>; const studentScores: Scores = { math: 90, english: 85, science: 92 };
Typescript
const phoneBook: Record<string, string> = { 'Alice': '123-4567', 'Bob': '987-6543' };
Typescript
type Flags = Record<string, boolean>; const featureFlags: Flags = { darkMode: true, betaUser: false };
Sample Program
This program defines fruit colors using Record and prints each fruit with its color.
Typescript
type FruitColors = Record<'apple' | 'banana' | 'cherry', string>; const colors: FruitColors = { apple: 'red', banana: 'yellow', cherry: 'dark red' }; for (const fruit in colors) { console.log(`${fruit} is ${colors[fruit]}`); }
Important Notes
Record keys must be unique and known if you use a union of string literals.
You can use string or number as keys for more flexible objects.
Record helps catch errors if you forget a key or use the wrong value type.
Summary
Record creates an object type with specific keys and value types.
It is useful for mapping fixed keys to values of the same type.
It helps keep your code safe and clear by enforcing types.
Practice
1. What does the TypeScript
Record<K, T> type do?easy
Solution
Step 1: Understand the Record type parameters
Record<K, T>takes two type parameters:Kfor keys andTfor values.Step 2: Identify the purpose of Record
It creates an object type where keys are of typeKand values are of typeT.Final Answer:
Creates an object type with keys of type K and values of type T. -> Option AQuick Check:
Record<K, T> = object with keys K and values T [OK]
Hint: Record maps keys to values of one type [OK]
Common Mistakes:
- Thinking Record creates arrays
- Confusing Record with functions
- Assuming Record allows any key type
2. Which of the following is the correct syntax to define a Record type with string keys and number values?
easy
Solution
Step 1: Recall Record syntax
Record requires two type parameters: key type and value type.Step 2: Match key and value types
We want string keys and number values, soRecord<string, number>is correct.Final Answer:
type Scores = Record<string, number>; -> Option DQuick Check:
Record<string, number> = string keys, number values [OK]
Hint: Record needs both key and value types [OK]
Common Mistakes:
- Using only one type parameter
- Swapping key and value types
- Using invalid syntax without two parameters
3. What is the output of this TypeScript code?
type UserRoles = Record<'admin' | 'user', boolean>;
const roles: UserRoles = { admin: true, user: false };
console.log(roles['admin']);medium
Solution
Step 1: Understand the Record type usage
UserRoles maps keys 'admin' and 'user' to boolean values.Step 2: Check the object values and access
roles has admin: true, user: false. Accessing roles['admin'] returns true.Final Answer:
true -> Option AQuick Check:
roles['admin'] = true [OK]
Hint: Access Record keys like normal object keys [OK]
Common Mistakes:
- Expecting false instead of true
- Thinking roles['admin'] is undefined
- Confusing type error with runtime output
4. Identify the error in this TypeScript code using Record:
type Config = Record<'host' | 'port', string>;
const serverConfig: Config = { host: 'localhost', port: 8080 };medium
Solution
Step 1: Check Record type definition
Config expects keys 'host' and 'port' with string values.Step 2: Check assigned object values
port is assigned 8080 (number), but type expects string, causing a type error.Final Answer:
Type error: port value should be a string, not a number. -> Option CQuick Check:
Record<'host'|'port', string> requires string values [OK]
Hint: Match value types exactly in Record [OK]
Common Mistakes:
- Ignoring type mismatch for port
- Assuming numbers allowed for string values
- Thinking keys must be numbers
5. You want to create a Record type to map product IDs (numbers) to product names (strings). Which is the best way to define it in TypeScript?
hard
Solution
Step 1: Understand Record key types in TypeScript
Record keys can bestringornumbertypes. TypeScript handles number keys by converting them to strings internally for JavaScript objects while enforcing numeric access.Step 2: Choose correct key type for product IDs
Since product IDs are numbers, useRecord<number, string>for type-safe numeric keys.Step 3: Confirm value type
Product names are strings, so value type isstring.Final Answer:
type Products = Record<number, string>; -> Option BQuick Check:
Record<number, string> = numeric keys, string values [OK]
Hint: Use number keys for numeric IDs in Record [OK]
Common Mistakes:
- Using string keys which allows non-numeric keys
- Confusing value types
- Using fixed string keys instead of dynamic keys
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
