Conditional Types Deep Dive
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
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).
Codetype 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>; // stringExpected result
T1 is 'b' | 'c', T2 is number, T3 is string — all verified by hovering.
- 2
Build NonNullable
NonNullable<T> removes null and undefined from T. Implement it using your MyExclude.
Codetype MyNonNullable<T> = MyExclude<T, null | undefined>; type T4 = MyNonNullable<string | null>; // string type T5 = MyNonNullable<number | undefined>; // number type T6 = MyNonNullable<string | null | undefined>; // stringExpected result
T4, T5, T6 all exclude null and undefined.
- 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.
Codetype 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>; // numberExpected result
T7 is number, T8 is string (recursively unwrapped), T9 is number (no Promise).
- 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.
Codetype 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 oneExpected result
Only one of meow or bark is allowed; both or neither cause type errors.
- 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
type MyExclude<T, U> = /* TODO */;
type MyExtract<T, U> = /* TODO */;
type MyNonNullable<T>> = /* TODO */;type MyExclude<T, U> = T extends U ? never : T;
type MyExtract<T, U> = T extends U ? T : never;
type MyNonNullable<T> = MyExclude<T, null | undefined>;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
- Conditional Types
T extends U ? X : Y and distributive conditionals.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.