What is Record Type in TypeScript?

Hemanta Sundaray

Published December 21, 2023


In TypeScript, Record is a utility type that you can use to create a custom object type with a set of keys (of one type) and associated values (of another type) .

It's a generic type that takes two types as its parameters:

Record<Keys, Type>

The first parameter (Keys) is used to specify the type of the keys of the object, and the second parameter (Type) specifies the type of the values for these keys.

Here's an example:

Let's say you are developing an e-commerce app. You have a list of products, each with a unique identifier. You can use a Record to create a type that maps product IDs to product details.

type ProductID = string; // Assume product IDs are strings
type ProductDetails = {
  name: string;
  price: number;
  inStock: boolean;
};
const products: Record<ProductID, ProductDetails> = {
  prod123: { name: "Gaming Laptop", price: 1200, inStock: true },
  prod456: { name: "Headphones", price: 100, inStock: false },
  // ...
};