Use `NonNullable<T>`
Remove `null` and `undefined` from a type.
type MaybeUser = { id: string } | null | undefined;
type User = NonNullable<MaybeUser>;Useful after validation or guard logic has removed nullable cases.
Built-in utility types and practical application patterns.
Reusable helpers for app code and API modeling.
Remove `null` and `undefined` from a type.
type MaybeUser = { id: string } | null | undefined;
type User = NonNullable<MaybeUser>;Useful after validation or guard logic has removed nullable cases.
Keep only members assignable to another type.
type Shape = "circle" | "square" | 42;
type StringShape = Extract<Shape, string>;Helpful when refining broad unions into a specific subset.
Remove members assignable to another type.
type Shape = "circle" | "square" | 42;
type NumericFree = Exclude<Shape, number>;`Exclude` is the inverse of `Extract` and useful for filtering unions.
Extract a function’s parameter tuple.
type Fn = (id: string, active: boolean) => void;
type FnArgs = Parameters<Fn>; // [id: string, active: boolean]Great for wrappers, event helpers, and higher-order functions.
Get the instance type from a class constructor.
class UserService {}
type UserServiceInstance = InstanceType<typeof UserService>;Useful in dependency injection helpers or factory utilities.
Patterns for strongly typed forms, APIs, and state machines.
Model paginated collections with generics.
type Paginated<T> = {
items: T[];
total: number;
page: number;
pageSize: number;
};A generic wrapper keeps API contracts DRY across entities.
Type validation errors by field name.
type FieldErrors<T> = Partial<Record<keyof T, string>>;This pattern works well for forms, settings pages, and API validation results.
type AsyncState<T> =
| { status: "idle" }
| { status: "loading" }
| { status: "success"; data: T }
| { status: "error"; error: string };Discriminated unions make UI rendering branches easier to reason about.
type Events = {
"user.created": { id: string };
"user.deleted": { id: string };
};A typed event map pairs well with `keyof` and indexed access helpers.