Conditional types and infer
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Write a conditional type with extends.
- Extract types with infer.
- Explain distributive conditional types over unions.
Overview
#Conditional types
A conditional type chooses between two types based on assignability.
type IsString<T> = T extends string ? true : false;
This is a compile-time decision. No runtime branch is created.
infer
infer introduces a type variable inside a conditional and lets the checker solve for it.
type ElementType<T> = T extends Array<infer U> ? U : never;
type Unwrap<T> = T extends Promise<infer U> ? U : T;
Distributive behavior
When the checked type is a naked type parameter, the conditional distributes over unions.
type ToArray<T> = T extends unknown ? T[] : never;
type R = ToArray<string | number>; // string[] | number[]
Wrapping the parameter in a tuple stops distribution: [T] extends [unknown] ? T[] : never gives (string | number)[].
Built-ins built this way
Exclude, Extract, NonNullable and Awaited are all conditional types. The next exercise rebuilds one from scratch.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Implement `MyExclude<T, U>` that removes from T every member assignable to U, without using the built-in Exclude.
starter.typescriptexport type MyExclude<T, U> = T;Reveal the reference answer
reference.typescriptexport type MyExclude<T, U> = T extends U ? never : T;Authored expected output: Compile-time: MyExclude<"a" | "b" | "c", "a"> is "b" | "c". No runtime output.
The naked T distributes over the union and drops members assignable to U.
Exercise 2Implement
Implement `UnwrapPromise<T>` that resolves a promise type to its value type and leaves non-promises unchanged, using infer.
starter.typescriptexport type UnwrapPromise<T> = T;Reveal the reference answer
reference.typescriptexport type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;Authored expected output: Compile-time: UnwrapPromise<Promise<number>> is number; UnwrapPromise<string> is string. No runtime output.
infer names the resolved type inside the conditional.
Exercise 3Predict
What is `R`? ```ts type ToArray<T> = T extends unknown ? T[] : never; type R = ToArray<string | number>; ```
Exercise 4Debug
This conditional distributes when it should not. Fix it so `R` is `(string | number)[]`. ```ts type ToArray<T> = T extends unknown ? T[] : never; type R = ToArray<string | number>; ```
starter.typescripttype ToArray<T> = T extends unknown ? T[] : never; type R = ToArray<string | number>;Reveal the reference answer
reference.typescripttype ToArray<T> = [T] extends [unknown] ? T[] : never; type R = ToArray<string | number>;Authored expected output: Compile-time: R is (string | number)[]. No runtime output.
Wrapping both sides in tuples makes the check non-distributive.
Common misconceptions
#- Conditional types run at runtime.
- infer must be named to match an existing type.
- Conditionals never distribute.
Follow-up practice
#- typescript-mapped-template
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.