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.

Partial<Type>

The Partial utility type is used to create a new type with the same keys as the originating type as optional keys. For this new type any key of the original type is accepted but it is no problem if one or more keys are missing.

example:

type Pizza = {
  name: string,
  spiciness: number,
  price: number
}

We can create a new type of a Pizza with the Partial utility type. The result is a new type in which all keys of Pizza are set to optional.

type PartialPizza = Partial<Pizza>;

Creating an object of this type could look something like:

const pizza: PartialPizza = {
  name: 'Quatro Stagioni',
  price: 14.99
}

The spiciness key is missing in this object but that is no problem as it is now defined as optional.

For a complete overview of TypeScript Utility types please check:

TypeScript handbook utility types