Loading lessons...
Conditional Utility Types (Exclude, Extract, ReturnType)
Conditional & Union Utility Types
TypeScript provides utilities to operate on unions, functions, and instance types.
Union & Function Utilities Overview
| Utility Type | Usage | Description |
|---|---|---|
Exclude<Union, ExcludedMembers> | Union filtering | Removes specified types from a union. |
Extract<Union, ExtractedMembers> | Union selection | Keeps only types assignable to specified members. |
NonNullable<T> | Null check | Strips null and undefined from T. |
ReturnType<Fn> | Function inspect | Extracts the return type of a function signature. |
Parameters<Fn> | Function inspect | Obtains parameter types of a function as a tuple. |
Code Examples
type T0 = Exclude<"a" | "b" | "c", "a">; // "b" | "c"
type T1 = Extract<string | number | boolean, string | number>; // string | number
type T2 = NonNullable<string | number | null | undefined>; // string | number
function getUser() {
return { id: 101, username: "dev_user" };
}
type UserResult = ReturnType<typeof getUser>; // { id: number; username: string; }
TL;DR
Excludestrips union types;Extractkeeps matching union types.NonNullableremovesnullandundefined.ReturnType<typeof fn>captures function return types automatically.