TypeScript Utility Types: Useful types

1. Partial: Makes All Properties of an Object Optional Partial is one of the most useful types when working with objects. It allows you to easily make all properties of a type optional. Example: interface User { name: string; age: number; } const updateUser = (user: Partial) => { // You only need to pass in some properties of the user. }; updateUser({ name: "John" }); In the example above, you can call updateUser with partial information of User, rather than having to pass all properties. 2. Required: Makes All Properties of an Object Required Opposite to Partial, Required makes all properties of an object required. Example: interface User { name: string; age?: number; } const user: Required = { name: "John", age: 30, // The age cannot be omitted anymore }; In this example, you cannot create a User object without age. 3. Pick: Select Some Properties from an Object Pick allows you to create a new type from an existing object with only the specified properties. Example: interface User { name: string; age: number; email: string; } const contactInfo: Pick = { name: "John", email: "john@example.com", }; Here, Pick helps you extract only the name and email properties of User, ignoring age. 4. Omit: Remove Some Properties from an Object Omit is the reverse of Pick. It allows you to create a new type from an object but without certain properties. Example: interface User { name: string; age: number; email: string; } const userWithoutEmail: Omit = { name: "John", age: 30, }; In this example, Omit helps you create a User object without the email property. 5. Record: Create an Object with a Specific Key and Value Type Record allows you to define an object with keys of type K and values of type T. This type is very useful when you need an object with dynamic keys. Example: type Role = "admin" | "user" | "guest"; const permissions: Record = { admin: true, user: false, guest: false, }; In this example, Record helps create a permissions object with keys from Role and boolean values. 6. Exclude: Remove Values from a Type Exclude helps you create a new type by removing values of type U from T. This type is useful when you need to remove specific values from a union type. Example: type Fruit = "apple" | "banana" | "orange"; type Citrus = Exclude; // Citrus only includes "apple" and "orange" In this example, Exclude helps you remove "banana" from the Fruit type. 7. NonNullable: Remove null and undefined NonNullable helps you remove null and undefined from a type. This is a great way to ensure that values always have valid values. Example: type MyType = string | null | undefined; type MyNonNullableType = NonNullable; // MyNonNullableType only includes string NonNullable removes null and undefined from the MyType type. Summary TypeScript Utility Types help you write cleaner, more maintainable code and avoid unnecessary errors when working with data types. Partial: Makes all properties optional. Required: Makes all properties required. Pick: Selects some properties from an object. Omit: Removes some properties from an object. Record: Creates an object with specific key and value types. Exclude: Removes values from a type. NonNullable: Removes null and undefined. Using the correct utility types will help you manage your data types more efficiently and optimize your code!

Apr 21, 2025 - 11:09
 0
TypeScript Utility Types: Useful types

1. Partial: Makes All Properties of an Object Optional

Partial is one of the most useful types when working with objects. It allows you to easily make all properties of a type optional.

Example:

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

const updateUser = (user: Partial<User>) => {
  // You only need to pass in some properties of the user.
};

updateUser({ name: "John" });

In the example above, you can call updateUser with partial information of User, rather than having to pass all properties.

2. Required: Makes All Properties of an Object Required

Opposite to Partial, Required makes all properties of an object required.

Example:

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

const user: Required<User> = {
  name: "John",
  age: 30,  // The age cannot be omitted anymore
};

In this example, you cannot create a User object without age.

3. Pick: Select Some Properties from an Object

Pick allows you to create a new type from an existing object with only the specified properties.

Example:

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

const contactInfo: Pick<User, "name" | "email"> = {
  name: "John",
  email: "john@example.com",
};

Here, Pick helps you extract only the name and email properties of User, ignoring age.

4. Omit: Remove Some Properties from an Object

Omit is the reverse of Pick. It allows you to create a new type from an object but without certain properties.

Example:

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

const userWithoutEmail: Omit<User, "email"> = {
  name: "John",
  age: 30,
};

In this example, Omit helps you create a User object without the email property.

5. Record: Create an Object with a Specific Key and Value Type

Record allows you to define an object with keys of type K and values of type T. This type is very useful when you need an object with dynamic keys.

Example:

type Role = "admin" | "user" | "guest";
const permissions: Record<Role, boolean> = {
  admin: true,
  user: false,
  guest: false,
};

In this example, Record helps create a permissions object with keys from Role and boolean values.

6. Exclude: Remove Values from a Type

Exclude helps you create a new type by removing values of type U from T. This type is useful when you need to remove specific values from a union type.

Example:

type Fruit = "apple" | "banana" | "orange";
type Citrus = Exclude<Fruit, "banana">;
// Citrus only includes "apple" and "orange"

In this example, Exclude helps you remove "banana" from the Fruit type.

7. NonNullable: Remove null and undefined

NonNullable helps you remove null and undefined from a type. This is a great way to ensure that values always have valid values.

Example:

type MyType = string | null | undefined;
type MyNonNullableType = NonNullable<MyType>;
// MyNonNullableType only includes string

NonNullable removes null and undefined from the MyType type.

Summary

TypeScript Utility Types help you write cleaner, more maintainable code and avoid unnecessary errors when working with data types.

  • Partial: Makes all properties optional.
  • Required: Makes all properties required.
  • Pick: Selects some properties from an object.
  • Omit: Removes some properties from an object.
  • Record: Creates an object with specific key and value types.
  • Exclude: Removes values from a type.
  • NonNullable: Removes null and undefined.

Using the correct utility types will help you manage your data types more efficiently and optimize your code!