TypeScript Generics and Advanced Type Operators

Conditional types — assignability-based type branches

A conditional type selects a branch according to whether the checked type is assignable to another type. A naked type parameter on the left of extends distributes across union members; wrapping both sides in tuple types prevents that distribution. A conditional involving an unresolved type parameter can remain deferred. This is a type computation, not an emitted if statement; special inputs such as never and any require care. Example output: "text other". Local strict TypeScript 5.8.3 / ES2022 baseline. The type construct itself is erased; the logged value illustrates a checked use, not runtime type validation.

Formal Syntax & Structure

typescript syntaxtypescript
type Result<T> = T extends string ? "text" : "other";

Working Example

Example Snippettypescript
type Kind<T> = T extends string ? "text" : "other";const label: Kind<string | number> = "text";const fallback: Kind<number> = "other";console.log(label, fallback);
TypeScript Reference