</>
Skip to content
TypeScript lessons (25/31)

TypeScript — Utility Types

Partial

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

function updateUser(user: Partial<User>) {
  // All properties optional
}

Required

interface Config {
  host?: string;
  port?: number;
}

function connect(config: Required<Config>) {
  // All properties required
}

Readonly

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

function process(user: Readonly<User>) {
  // user.name = 'Bob'; // Error
}

Pick

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

type UserBasic = Pick<User, 'name' | 'age'>;

Omit

type UserWithoutEmail = Omit<User, 'email'>;

Record

type UserRoles = Record<string, string[]>;
const roles: UserRoles = {
  admin: ['read', 'write'],
  user: ['read']
};

Mini Practice

  1. Use Partial
  2. Use Required
  3. Use Pick and Omit
  4. Create Record types

Up Next

Continue with Mapped Types - Type transformation.

Related Topics

Frequently Asked Questions about Utility Types

What is Utility Types in TypeScript?

Utility Types is a fundamental concept in TypeScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Utility Types?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Utility Types.

Why is Utility Types important in TypeScript?

Utility Types is essential for TypeScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.