Skip to content

Mapped Types Toolkit

Medium ~35 min

What you'll build

A set of utility types reimplementing TypeScript's built-in helpers, plus a custom DeepPartial that makes all nested properties optional.

Goals

  • Use [K in keyof T] syntax to map over keys
  • Add or remove modifiers with ? and -?
  • Build Pick and Omit with key filtering
  • Implement recursive DeepPartial

Prerequisites

  • Generic types
  • keyof and indexed access types
  • Conditional types (from project-11)

Steps

  1. 1

    Build Partial and Readonly

    Partial<T> makes all properties optional; Readonly<T> makes them readonly. Both use mapped types with modifiers.

    Code
    type MyPartial<T> = { [K in keyof T]?: T[K]; };
    type MyReadonly<T> = { readonly [K in keyof T]: T[K]; };
    
    interface User { id: number; name: string; email: string; }
    
    type UserPartial = MyPartial<User>;   // { id?: number; name?: string; email?: string; }
    type UserReadonly = MyReadonly<User>; // readonly id, name, email

    Expected result

    UserPartial allows partial objects; UserReadonly disallows mutation.

  2. 2

    Build Required

    Required<T> removes the optional modifier. Use -? to strip the ? from each property.

    Code
    type MyRequired<T> = { [K in keyof T]-?: T[K]; };
    
    type MaybeUser = { id?: number; name?: string; };
    type RequiredUser = MyRequired<MaybeUser>; // { id: number; name: string; }

    Expected result

    RequiredUser has all properties required.

  3. 3

    Build Pick and Omit

    Pick<T, K> keeps only keys in K; Omit<T, K> removes them. Pick uses a mapped type over K; Omit is Pick + Exclude.

    Code
    type MyPick<T, K extends keyof T> = { [P in K]: T[P]; };
    type MyOmit<T, K extends keyof T> = MyPick<T, MyExclude<keyof T, K>>;
    
    type UserPreview = MyPick<User, 'id' | 'name'>;     // { id: number; name: string; }
    type UserWithoutId = MyOmit<User, 'id'>;             // { name: string; email: string; }

    Expected result

    UserPreview has only id and name; UserWithoutId excludes id.

  4. 4

    Build Record

    Record<K, V> creates an object type with keys K and values V. Use a mapped type over K.

    Code
    type MyRecord<K extends keyof any, V> = { [P in K]: V; };
    
    type StringMap = MyRecord<string, number>;  // { [key: string]: number }
    type ThreeFlags = MyRecord<'a' | 'b' | 'c', boolean>; // { a: boolean; b: boolean; c: boolean; }

    Expected result

    StringMap is an index signature; ThreeFlags has exactly three boolean keys.

  5. 5

    Build DeepPartial

    DeepPartial recursively makes all properties optional, including nested objects. Use a conditional to check if the value is an object.

    Code
    type DeepPartial<T> = {
      [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
    };
    
    interface Config {
      server: { port: number; host: string; };
      db: { url: string; pool: number; };
    }
    
    type PartialConfig = DeepPartial<Config>;
    // { server?: { port?: number; host?: string; }; db?: { url?: string; pool?: number; }; }
    
    const cfg: PartialConfig = { server: { port: 3000 } }; // OK — deeply partial

    Expected result

    PartialConfig allows any level of partial nesting.

Try it yourself

What you learned

You rebuilt TypeScript's built-in utility types from scratch — understanding them lets you create custom transformations for any object shape.

Related Resources

Related Lessons

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic