When you don’t want to repeat yourself, sometimes a type needs to be based on another type.

Mapped types build on the syntax for index signatures, which are used to declare the types of properties which has not been declared ahead of time:

  1. type OnlyBoolsAndHorses = {
    [key: string]: boolean | Horse;
    };
  2. const conforms: OnlyBoolsAndHorses = {
    del: true,
    rodney: false,
    };

A mapped type is a generic type which uses a union of PropertyKeys (frequently created via a keyof) to iterate through keys to create a type:

  1. type OptionsFlags<Type> = {
    [Property in keyof Type]: boolean;
    };

In this example, OptionsFlags will take all the properties from the type Type and change their values to be a boolean.

  1. type FeatureFlags = {
    darkMode: () => void;
    newUserProfile: () => void;
    };
  2. type FeatureOptions = OptionsFlags<FeatureFlags>;
    type FeatureOptions = {
  3. darkMode: boolean;
  4. newUserProfile: boolean;
  5. }

Mapping Modifiers

There are two additional modifiers which can be applied during mapping: readonly and ? which affect mutability and optionality respectively.

You can remove or add these modifiers by prefixing with - or +. If you don’t add a prefix, then + is assumed.

  1. // Removes 'readonly' attributes from a type's properties
    type CreateMutable<Type> = {
    -readonly [Property in keyof Type]: Type[Property];
    };
  2. type LockedAccount = {
    readonly id: string;
    readonly name: string;
    };
  3. type UnlockedAccount = CreateMutable<LockedAccount>;
    type UnlockedAccount = {
  4. id: string;
  5. name: string;
  6. }
  1. // Removes 'optional' attributes from a type's properties
    type Concrete<Type> = {
    [Property in keyof Type]-?: Type[Property];
    };
  2. type MaybeUser = {
    id: string;
    name?: string;
    age?: number;
    };
  3. type User = Concrete<MaybeUser>;
    type User = {
  4. id: string;
  5. name: string;
  6. age: number;
  7. }

Key Remapping via as

In TypeScript 4.1 and onwards, you can re-map keys in mapped types with an as clause in a mapped type:

  1. type MappedTypeWithNewProperties<Type> = {
    [Properties in keyof Type as NewKeyType]: Type[Properties]
    }

You can leverage features like template literal types to create new property names from prior ones:

  1. type Getters<Type> = {
    [Property in keyof Type as `get${Capitalize<string & Property>}`]: () => Type[Property]
    };
  2. interface Person {
    name: string;
    age: number;
    location: string;
    }
  3. type LazyPerson = Getters<Person>;
    type LazyPerson = {
  4. getName: () => string;
  5. getAge: () => number;
  6. getLocation: () => string;
  7. }

You can filter out keys by producing never via a conditional type:

  1. // Remove the 'kind' property
    type RemoveKindField<Type> = {
    [Property in keyof Type as Exclude<Property, "kind">]: Type[Property]
    };
  2. interface Circle {
    kind: "circle";
    radius: number;
    }
  3. type KindlessCircle = RemoveKindField<Circle>;
    type KindlessCircle = {
  4. radius: number;
  5. }

Further Exploration

Mapped types work well with other features in this type manipulation section, for example here is a mapped type using a conditional type which returns either a true or false depending on whether an object has the property pii set to the literal true:

  1. type ExtractPII<Type> = {
    [Property in keyof Type]: Type[Property] extends { pii: true } ? true : false;
    };
  2. type DBFields = {
    id: { format: "incrementing" };
    name: { type: string; pii: true };
    };
  3. type ObjectsNeedingGDPRDeletion = ExtractPII<DBFields>;
    type ObjectsNeedingGDPRDeletion = {
  4. id: false;
  5. name: true;
  6. }