Sub topic 1
Conditional types: choosing a type with `extends`
Prerequisites
This chapter assumes you know TypeScript generics, union and intersection types, tuple types, function signatures, mapped types, and basic template literal types. You should also understand that a type describes which values are allowed.
A conditional type chooses one type or another by testing a relationship between types:
type Result<T> = T extends string ? "text" : "other";Read this as: “If T can be assigned to string, use "text"; otherwise, use "other".”
The extends keyword in this position tests assignability. It does not create a class inheritance relationship.
type A = Result<string>; // "text"
type B = Result<42>; // "other"
type C = Result<"hello">; // "text"The string literal type "hello" is assignable to string, so it selects the true branch.
The three parts of a conditional type
A conditional type has this general form:
type Name<T> = T extends Constraint ? WhenTrue : WhenFalse;It contains:
T, the type being tested.Constraint, the type thatTmust be assignable to.WhenTrue, the result if the test succeeds.WhenFalse, the result if the test fails.
The branches can contain any valid type expression, including another conditional type.
type IsArray<T> = T extends unknown[] ? true : false;
type A = IsArray<number[]>; // true
type B = IsArray<number>; // falseThis lets a utility type describe a decision without creating runtime code. The test happens during type checking, and the resulting type is available to the rest of the program.
Checking object shape
Conditional types can test whether a type has a particular structure. For example, this utility selects the type of an id property when that property exists:
type IdType<T> = T extends { id: infer I } ? I : never;
type UserId = IdType<{ id: number; name: string }>; // number
type MissingId = IdType<{ name: string }>; // neverThe infer keyword captures a type during a successful match. Here, I becomes the type of id. This chapter focuses on the conditional decision; the next chapters develop infer in more detail.
A conditional type can also select an entirely different result:
type ToArray<T> = T extends string ? string[] : T[];
type TextValues = ToArray<string>; // string[]
type NumberValues = ToArray<number>; // number[]The false branch uses the original type parameter. That makes the utility reusable for many inputs.
Nested decisions
Use nested conditional types when the result depends on more than one test. Keep each test small so the result remains readable.
type Category<T> =
T extends string
? "text"
: T extends number
? "number"
: T extends boolean
? "boolean"
: "other";
type A = Category<"ready">; // "text"
type B = Category<10>; // "number"
type C = Category<false>; // "boolean"
type D = Category<Date>; // "other"The compiler evaluates the conditions from top to bottom. A type that matches an earlier branch never reaches later branches.
Common errors
The most common mistake is confusing assignability with exact equality. string and "ready" are not equal, but "ready" extends string, so the test succeeds.
Another mistake is placing a broad type first:
type BadCategory<T> =
T extends string | number ? "primitive" :
T extends string ? "text" :
"other";The "text" branch is unreachable because every string already matches string | number. Put more specific tests before broader ones.
Exercise
Exercise
What is the result of type R = ToArray<"ok"> using the ToArray utility above, and why?
Show answer
R is string[]. The literal type "ok" extends string, so the true branch is selected.