TypeScript Tutorial

Utility Types

Partial, Pick, Omit, Record, and other built-in helpers

What are Utility Types?

TypeScript provides several global utility types to help with common type transformations. These allow you to take an existing type and create a new, modified version of it without rewriting the entire definition.

1. Partial<T> and Required<T>

  • Partial<T>: Makes all properties of a type optional.
  • Required<T>: Makes all properties mandatory (opposite of Partial).
typescript
interface User { id: number; name: string; age?: number; }

type UpdateUser = Partial<User>; // { id?, name?, age? }

2. Pick<T, K> and Omit<T, K>

  • Pick<T, K>: Creates a type by selecting a set of properties K from T.
  • Omit<T, K>: Creates a type by removing a set of properties K from T.
typescript
type UserPreview = Pick<User, "id" | "name">;
type UserWithoutId = Omit<User, "id">;

3. Record<K, T>

Creates an object type whose property keys are K and whose property values are T.
typescript
const users: Record<number, User> = {
    1: { id: 1, name: "Alpha" }
};

4. Readonly<T>

Makes all properties of a type immutable.
typescript
const config: Readonly<{ theme: string }> = { theme: "dark" };
// config.theme = "light"; // ERROR!

Summary

TypeTransformationUsage
PartialAll properties ?PATCH requests, configs
RequiredRemove all ?Validation logic
PickChoose specific keysUI components, narrow models
OmitRemove specific keysSecurity (hide passwords)
RecordMap of Keys to ValuesCaches, lookup tables
ReadonlyAll properties readonlyConstants, shared state
ExcludeRemove from Union`Exclude<"a"
ExtractKeep from Union`Extract<"a"
NonNullableRemove null / undefinedSanitization
ReturnTypeGet function output typeAPI response handling
ParametersGet function input typesHigher-order functions
Logic"Don't repeat yourself" - transform existing types!
StandardBuilt into TypeScript (No imports needed)