As I am working a lot with TypeScript these days I ran into some interesting TypeScript Utility types that I would like to explore a bit more in a series of small blog posts.

Record<Keys, Type>

The Record utility type can be used to create a merged type where the keys are of Keys and the values of Type.

example:

type PizzaInfo = {
  spiciness: number,
  price: number,
}
type PizzaName = "Quatro Stagioni" | "Quatro Formaggi" | "Capricciosa"

We will use the PizzaName type for the keys and the PizzaInfo type for the values to create the merged record type:

const pizzas: Record<PizzaName, PizzaInfo> = {
  "Quatro Stagioni": {spiciness: 2, price: 14.99},
  "Quatro Formaggi": {spiciness: 1, price: 19.99},
  "Capricciosa":     {spiciness: 2, price: 19.99}
}

For a complete overview of TypeScript Utility types please check:

TypeScript handbook utility types