Skip to content
intermediatePhase 35 · TypeScript

Utility Types

Use Partial, Required, Pick, Omit, Record, and other built-in utility types.

45m
0 problems
Topic Progress0%

Partial, Required, Pick, Omit

Partial, Required, Pick, Omit

Partial

Makes all properties optional:

interface User {
  name: string;
  age: number;
  email: string;
}

// All properties are now optional
type PartialUser = Partial<User>;
// { name?: string; age?: number; email?: string }

// Useful for update functions
function updateUser(id: number, updates: Partial<User>) {
  // Can update any subset of properties
}

updateUser(1, { name: "Bob" }); // OK
updateUser(1, { age: 30, email: "bob@example.com" }); // OK

Required

Makes all properties required:

type OptionalUser = {
  name?: string;
  age?: number;
  email?: string;
};

// All properties are now required
type RequiredUser = Required<OptionalUser>;
// { name: string; age: number; email: string }

Pick

Selects specific properties:

type User = {
  id: number;
  name: string;
  email: string;
  password: string;
};

// Only name and email
type UserPreview = Pick<User, "name" | "email">;
// { name: string; email: string }

// For API responses
type UserProfile = Pick<User, "id" | "name" | "email">;

Omit

Removes specific properties:

type User = {
  id: number;
  name: string;
  email: string;
  password: string;
};

// Everything except password
type SafeUser = Omit<User, "password">;
// { id: number; name: string; email: string }

// For form inputs (no id)
type CreateUserInput = Omit<User, "id">;

Record and Readonly

Record and Readonly

Record

Constructs an object type with specific keys and values:

// Record<Keys, Values>
type Scores = Record<string, number>;

const scores: Scores = {
  math: 95,
  english: 88,
  science: 92
};

// With union keys
type Page = "home" | "about" | "contact";
type PageContent = Record<Page, string>;

const content: PageContent = {
  home: "Welcome",
  about: "About us",
  contact: "Contact info"
};

// Record with complex values
type UserMap = Record<number, User>;
const users: UserMap = {
  1: { id: 1, name: "Alice" },
  2: { id: 2, name: "Bob" }
};

Readonly

Makes all properties immutable:

type User = {
  name: string;
  age: number;
};

const user: Readonly<User> = {
  name: "Alice",
  age: 30
};

// user.name = "Bob"; // Error!

// ReadonlyArray
const numbers: ReadonlyArray<number> = [1, 2, 3];
// numbers.push(4); // Error!

// ReadonlyMap
const map = new ReadonlyMap<string, number>();
// map.set("key", 1); // Error!

Readonly vs Const

// const prevents reassignment
const x = 5;
// x = 6; // Error!

// const doesn't prevent mutation
const arr = [1, 2, 3];
arr.push(4); // OK!

// Readonly prevents mutation
const readonlyArr: readonly number[] = [1, 2, 3];
// readonlyArr.push(4); // Error!

Mapped Types

Mapped Types

Mapped types transform existing types by iterating over their properties.

Basic Mapped Type

type Optional<T> = {
  [K in keyof T]?: T[K];
};

type User = {
  name: string;
  age: number;
};

type OptionalUser = Optional<User>;
// { name?: string; age?: number }

Readonly Mapped Type

type MyReadonly<T> = {
  readonly [K in keyof T]: T[K];
};

type ReadonlyUser = MyReadonly<User>;
// { readonly name: string; readonly age: number }

Removing Modifiers

// Remove readonly
type Mutable<T> = {
  -readonly [K in keyof T]: T[K];
};

// Remove optional
type Concrete<T> = {
  [K in keyof T]-?: T[K];
};

Built-in Mapped Types

// Partial<T>: { [K in keyof T]?: T[K] }
// Required<T>: { [K in keyof T]-?: T[K] }
// Readonly<T>: { readonly [K in keyof T]: T[K] }
// Record<K, T>: { [P in K]: T }

Custom Utility Types

// Make specific properties optional
type PartialBy<T, K extends keyof T> = Omit<T, K> &
  Partial<Pick<T, K>>;

// Make specific properties required
type RequiredBy<T, K extends keyof T> = Omit<T, K> &
  Required<Pick<T, K>>;

Practice Problems

0/3solved
Build Utility Types Component

Create a reusable React component implementing Utility Types. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Utility Types Testing

Write unit and integration tests for Utility Types using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Utility Types Performance

Optimize Utility Types for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What does Partial<T> do?

Question 1 options

2. What is the difference between Pick and Omit?

Question 2 options

3. What does Record<K, V> create?

Question 3 options

4. What is a mapped type?

Question 4 options

Flashcards

Question

What does Partial<T> do?

Answer

Makes all properties optional

Question

What does Pick<T, K> do?

Answer

Selects specific properties from a type

Question

What does Omit<T, K> do?

Answer

Removes specific properties from a type

Question

What does Record<K, V> create?

Answer

An object type with keys K and values V

Question

What is Utility Types?

Answer

Utility Types is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.Partial makes all properties optional
  • 2.Required makes all properties required
  • 3.Pick selects specific properties
  • 4.Omit removes specific properties
  • 5.Mapped types transform existing types

Interview Tips

  • Explain when to use Partial vs making properties optional
  • Know the difference between Pick and Omit
  • Understand how mapped types work with keyof

Cheat Sheet

Cheat Sheet

Partial & Required

type P = Partial<User>;  // all optional
 type R = Required<User>; // all required

Pick & Omit

type P = Pick<User, "name" | "email">;
type O = Omit<User, "password">;

Record

type R = Record<string, number>;
type P = Record<"a" | "b", string>;

Readonly

type R = Readonly<User>;
const arr: readonly number[] = [1, 2];

Mapped Types

type Optional<T> = { [K in keyof T]?: T[K] };
type Readonly<T> = { readonly [K in keyof T]: T[K] };