Bird
Raised Fist0
Typescriptprogramming~5 mins

Record type in Typescript

Choose your learning style10 modes available
Introduction

The Record type helps you create an object with specific keys and values of a certain type. It keeps your data organized and safe.

When you want to map a set of known keys to values of the same type.
When you need a simple dictionary-like structure with fixed keys.
When you want to ensure all keys have values of a certain type.
When you want to replace an object type with many similar properties.
When you want to avoid repeating the same type for each property.
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
This creates a type with keys 'math', 'english', and 'science', each holding a number.
Typescript
type Scores = Record<'math' | 'english' | 'science', number>;

const studentScores: Scores = {
  math: 90,
  english: 85,
  science: 92
};
This allows any string as a key, with string values, like a phone book.
Typescript
const phoneBook: Record<string, string> = {
  'Alice': '123-4567',
  'Bob': '987-6543'
};
Use Record to store flags with true/false values for any string keys.
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]}`);
}
OutputSuccess
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/5)
1. What does the TypeScript Record<K, T> type do?
easy
A. Creates an object type with keys of type K and values of type T.
B. Defines a function that returns a record from a database.
C. Creates an array of type T with length K.
D. Declares a variable that can hold any type of value.

Solution

  1. Step 1: Understand the Record type parameters

    Record<K, T> takes two type parameters: K for keys and T for values.
  2. Step 2: Identify the purpose of Record

    It creates an object type where keys are of type K and values are of type T.
  3. Final Answer:

    Creates an object type with keys of type K and values of type T. -> Option A
  4. Quick 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
A. type Scores = Record;
B. type Scores = Record<number>;
C. type Scores = Record<string>;
D. type Scores = Record;

Solution

  1. Step 1: Recall Record syntax

    Record requires two type parameters: key type and value type.
  2. Step 2: Match key and value types

    We want string keys and number values, so Record<string, number> is correct.
  3. Final Answer:

    type Scores = Record<string, number>; -> Option D
  4. Quick 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
A. true
B. false
C. undefined
D. Type error

Solution

  1. Step 1: Understand the Record type usage

    UserRoles maps keys 'admin' and 'user' to boolean values.
  2. Step 2: Check the object values and access

    roles has admin: true, user: false. Accessing roles['admin'] returns true.
  3. Final Answer:

    true -> Option A
  4. Quick 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
A. Syntax error: Record keys must be numbers.
B. No error, code is correct.
C. Type error: port value should be a string, not a number.
D. Runtime error: port key missing.

Solution

  1. Step 1: Check Record type definition

    Config expects keys 'host' and 'port' with string values.
  2. Step 2: Check assigned object values

    port is assigned 8080 (number), but type expects string, causing a type error.
  3. Final Answer:

    Type error: port value should be a string, not a number. -> Option C
  4. Quick 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
A. type Products = Record;
B. type Products = Record;
C. type Products = Record<'id', string>;
D. type Products = Record;

Solution

  1. Step 1: Understand Record key types in TypeScript

    Record keys can be string or number types. TypeScript handles number keys by converting them to strings internally for JavaScript objects while enforcing numeric access.
  2. Step 2: Choose correct key type for product IDs

    Since product IDs are numbers, use Record<number, string> for type-safe numeric keys.
  3. Step 3: Confirm value type

    Product names are strings, so value type is string.
  4. Final Answer:

    type Products = Record<number, string>; -> Option B
  5. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard