Skip to content

Conditional Type

In one line

A conditional type (T extends U ? X : Y) selects type X if T is assignable to U, otherwise type Y. It enables type-level programming.

In simple words

A conditional type is like an if-else at the type level. T extends U ? X : Y means: if T is assignable to U, the type is X; otherwise it is Y.

Conditional types are powerful for type-level computation. They can distribute over unions, infer type parameters with infer, and create complex type transformations.

The built-in Exclude<T, U>, Extract<T, U>, and NonNullable<T> are all conditional types. They are used in advanced TypeScript libraries for deriving types from other types automatically.

Example

typescript
type IsString<T> = T extends string ? "yes" : "no";
type A = IsString<string>;  // "yes"
type B = IsString<number>;  // "no"

type NonNullable<T> = T extends null | undefined ? never : T;

IsString checks if T is a string. NonNullable removes null and undefined from T.

Related terms

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary