cat logo

Mapped Types in Typescript

3 min read
0
typescript
Mapped Types in Typescript

A mapped type in TypeScript is a new type created from a base type using a custom type mapping utility.

Creating new types using a custom type mapper is a common practice that promotes DRY (Don’t Repeat Yourself) principles in a TypeScript codebase. There are various methods for deriving new types in TypeScript, and custom mapping is one of them.
A TypeScript type mapper leverages the index signature syntax to transform a union type and generate a new type.

Example of a Mapped Type

Let’s say you have an interface representing user profile:

interface User {
    id: number;
    name: string;
    email: string;
}

Now, if you want to create a new type where all the properties of the User interface are optional, you can use a mapped type like this:

type Partial<T> = {
    [K in keyof T]?: T[K];
};

type OptionalUser = Partial<User>;

Partial Type was added to the utility types since 2.1 typescript version

Explanation:

  1. User Interface: Defines the structure of a user with id, name, and email.
  2. Mapped Type: Partial<T> takes a type T and creates a new type where all properties of T are optional. Here:
    • K in keyof T iterates over each key of T.
    • T[K] gets the type of each property, and the ? makes them optional.
  3. OptionalUser Type: OptionalUser is now a new type based on User where all the properties (id, name, email) are optional.

Usage:

You can use the OptionalUser type like this:

const user1: OptionalUser = {}; // Valid, all properties are optional
const user2: OptionalUser = { name: "Alice" }; // Also valid
const user3: OptionalUser = { id: 1, email: "alice@example.com" }; // Valid too

Resources

  1. Typescript 2.1 Release
  2. Mapped types in Typescript docs
  3. Great Article about Mapped types by Matt Pocock
  4. Refine Blog Post with cool examples