Loading lesson path
Conditional types in TypeScript enable you to create types that depend on other types, similar to how if-else statements work in JavaScript.Formula
They're a powerful feature that allows for sophisticated type transformations and type - level programming.: Perform conditional checks on types
: Extract and manipulate types using infer
: Combine with other TypeScript features
: Build powerful type utilities
T extends U ? X : Y, which means: "if type T extends (or is assignable to) type U, use type X, otherwise use type Y ".
Example type IsString<T> = T extends string ? true : false;
// Usage examples type Result1 = IsString<string>; // true type Result2 = IsString<number>; // false type Result3 = IsString<"hello">; // true (literal types extend their base types)Formula
// We can use this with variables too let a: IsString < string >; // a has type 'true'let b: IsString<number>; // b has type 'false'Conditional types are particularly useful with union types, where they're automatically distributed over union members:
Example type ToArray<T> = T extends any ? T[] : never;
// When used with a union type, it applies to each member of the union type StringOrNumberArray = ToArray<string | number>;Formula
// This becomes ToArray < string > | ToArray < number >// Which becomes string[] | number[]
Formula
// We can also extract specific types from a union type ExtractString < T > = T extends string ? T : never;type StringsOnly = ExtractString<string | number | boolean | "hello">;
// Result: string | "hello"The infer keyword allows you to declare a type variable within the condition part of a conditional type and then use it in the true branch of the condition:
Formula
// Extract the return type of a function type type ReturnType < T > = T extends (...args: any[]) => infer R ? R : never;// Examples function greet() { return "Hello, world!"; }
function getNumber() { return 42; }
type GreetReturnType = ReturnType<typeof greet>; // string type NumberReturnType = ReturnType<typeof getNumber>; // numberFormula
// Extract element type from array type ElementType < T > = T extends (infer U)[] ? U : never;type NumberArrayElement = ElementType<number[]>; // number type StringArrayElement = ElementType<string[]>; // stringFormula
TypeScript includes several built - in conditional types in its standard library:Formula
// Extract < T, U > - Extracts types from T that are assignable to Utype OnlyStrings = Extract<string | number | boolean, string>; // stringFormula
// Exclude < T, U > - Excludes types from T that are assignable to Utype NoStrings = Exclude<string | number | boolean, string>; // number | booleanFormula
// NonNullable < T > - Removes null and undefined from Ttype NotNull = NonNullable<string | null | undefined>; // string
// Parameters<T> - Extracts parameter types from a function type type Params = Parameters<(a: string, b: number) => void>; // [string, number]
// ReturnType<T> - Extracts the return type from a function type type Return = ReturnType<() => string>; // stringConditional types can be used recursively to create complex type transformations: