Sub topic 11
Conditional Types
Assume you know basic TypeScript types, unions, interfaces, type aliases, and generic type parameters. Conditional types let one type choose another type based on a relationship between types. They are the type-level equivalent of an if statement.
The Conditional Type Syntax
A conditional type has three parts:
type Result<T> = T extends SomeType ? TrueType : FalseType;Read this as: “If T extends SomeType, use TrueType; otherwise, use FalseType.”
Here, extends asks whether one type is assignable to another. It does not mean that a class must inherit from another class.
type IdKind<T> = T extends number ? "numeric" : "text";
type A = IdKind<42>; // "numeric"
type B = IdKind<"user-1">; // "text"The conditional type runs when TypeScript substitutes a concrete type for T.
Choosing Between Object Shapes
Conditional types are useful when an API has different results for different input types.
interface Success<T> {
ok: true;
value: T;
}
interface Failure {
ok: false;
error: string;
}
type ResultFor<T> = T extends string ? Success<T> : Failure;
const textResult: ResultFor<string> = {
ok: true,
value: "ready",
};
const numberResult: ResultFor<number> = {
ok: false,
error: "Numbers are not supported",
};ResultFor<string> becomes Success<string>. ResultFor<number> becomes Failure.
Conditional types can also inspect whether a property exists. This example extracts a message type only from types that contain a message property:
type MessageOf<T> =
T extends { message: unknown } ? T["message"] : never;
type ApiError = { message: string; status: number };
type NoMessage = { status: number };
type ErrorMessage = MessageOf<ApiError>; // string
type MissingMessage = MessageOf<NoMessage>; // nevernever represents a type with no possible values. It is useful when a type does not meet the required shape.
Conditional Types with Unions
When the checked type is a generic type parameter, TypeScript can evaluate each member of a union separately. This behavior is called distribution and is useful for filtering types.
type OnlyStrings<T> = T extends string ? T : never;
type Names = OnlyStrings<string | number | boolean>;
// stringThe number and boolean members become never, leaving only string. A conditional type can therefore act as a type-level filter.
You can combine conditional types with ordinary unions:
type Primitive = string | number | boolean;
type ArrayIfPrimitive<T> =
T extends Primitive ? T[] : never;
type Values = ArrayIfPrimitive<string | number>;
// string[] | number[]The result is a union of arrays, not one array containing both types. If you need to treat the entire union as one value, use a tuple wrapper such as [T] extends [Primitive]. This disables distribution.
Practical Design Guidance
| Use conditional types when | Prefer another approach when |
|---|---|
| A result type depends on a generic input | A plain alias expresses the type clearly |
| You need to filter members of a union | The logic is easier to understand in runtime code |
| Several related APIs share a type-level rule | A conditional becomes deeply nested |
Keep conditional types readable. A named helper such as MessageOf<T> is easier to inspect than repeating a long conditional expression throughout an API.
Exercise
What is the result of type R<T> = T extends string ? 1 : 2; type X = R<string | number>?
Show answer
X is 1 | 2, because the conditional distributes over the union and evaluates string and number separately.