Skip to content

Conditional Types Deep Dive

Medium ~40 min

What you'll build

A utility type library implementing Exclude, Extract, NonNullable, and a custom Awaited type that unwraps Promise<T> recursively.

Goals

  • Understand conditional type syntax T extends U ? X : Y
  • Build Exclude and Extract from first principles
  • Use distributive conditional types over unions
  • Implement recursive type unwrapping (Awaited)

Prerequisites

  • Generic types
  • Union and intersection types
  • Type aliases

Steps

  1. 1

    Build Exclude and Extract

    Exclude<T, U> removes types from T that are assignable to U. Extract<T, U> keeps them. Both use conditional types over unions (distributive).

    Code
    type MyExclude<T, U> = T extends U ? never : T;
    type MyExtract<T, U> = T extends U ? T : never;
    
    type T1 = MyExclude<'a' | 'b' | 'c', 'a'>;      // 'b' | 'c'
    type T2 = MyExtract<string | number | boolean, number>; // number
    
    type T3 = MyExclude<string | null | undefined, null | undefined>; // string

    Expected result

    T1 is 'b' | 'c', T2 is number, T3 is string — all verified by hovering.

  2. 2

    Build NonNullable

    NonNullable<T> removes null and undefined from T. Implement it using your MyExclude.

    Code
    type MyNonNullable<T> = MyExclude<T, null | undefined>;
    
    type T4 = MyNonNullable<string | null>;        // string
    type T5 = MyNonNullable<number | undefined>;   // number
    type T6 = MyNonNullable<string | null | undefined>; // string

    Expected result

    T4, T5, T6 all exclude null and undefined.

  3. 3

    Build a recursive Awaited type

    Awaited<T> recursively unwraps nested Promises: Awaited<Promise<Promise<number>>> is number. Use a conditional that checks for a .then method.

    Code
    type MyAwaited<T> = T extends null | undefined ? T :
      T extends object & { then(onfulfilled: infer F, ...args: infer _): any; } ?
        F extends (value: infer V, ...args: infer _) => any ?
          MyAwaited<V> : never
      : T;
    
    type T7 = MyAwaited<Promise<number>>;           // number
    type T8 = MyAwaited<Promise<Promise<string>>>;  // string
    type T9 = MyAwaited<number>;                    // number

    Expected result

    T7 is number, T8 is string (recursively unwrapped), T9 is number (no Promise).

  4. 4

    Build a type-level XOR

    Create a type that requires exactly one of two properties — an XOR (exclusive or) at the type level. This is useful for 'either A or B but not both' APIs.

    Code
    type Without<T, U> = { [P in MyExclude<keyof T, keyof U>]?: never };
    type XOR<T, U> = (Without<T, U> & U) | (Without<U, T> & T);
    
    interface CatOptions { meow: boolean; }
    interface DogOptions { bark: boolean; }
    
    type PetOptions = XOR<CatOptions, DogOptions>;
    
    const a: PetOptions = { meow: true };     // OK
    const b: PetOptions = { bark: true };     // OK
    const c: PetOptions = { meow: true, bark: true }; // Error: can't have both
    const d: PetOptions = {};                  // Error: must have one

    Expected result

    Only one of meow or bark is allowed; both or neither cause type errors.

  5. 5

    Test with type assertions

    Use type-level assertions (via const variables with explicit types) to verify your utility types produce the expected results. The compiler is your test runner.

    Code
    // Type-level test: if these compile, the types are correct
    const _t1: 'b' | 'c' = {} as MyExclude<'a' | 'b' | 'c', 'a'>;
    const _t2: number = {} as MyNonNullable<number | null>;
    const _t3: string = {} as MyAwaited<Promise<Promise<string>>>;
    
    console.log('All type tests passed');

    Expected result

    The file compiles with no type errors, confirming the utilities work.

Try it yourself

What you learned

You mastered conditional types — the engine behind Exclude, Extract, NonNullable, and Awaited. They let you compute types from other types at compile time.

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