Unveiling the Most Used TypeScript Utilities

  • Sat Jun 17 2023
  • Jazim Abbas

Welcome to the world of TypeScript utility types! TypeScript provides a set of powerful utility types that can greatly enhance your development experience and improve code maintainability. In this article, we will explore some of the most commonly used utility types, namely Pick, Omit, Readonly, and Mapped types. We'll delve into their syntax, understand their applications, and uncover how they can streamline your code and boost productivity.

Whether you're a seasoned TypeScript developer or just getting started, understanding and leveraging these utility types will elevate your TypeScript skills and enable you to write more robust and expressive code.

Let's explore a few of the powerful most used utility types that TypeScript offers:

Indexed Access Types

Indexed Access Types in TypeScript provide a flexible and concise way to access and manipulate specific properties of a type using index notation. With indexed access types, you can dynamically retrieve the type of a particular property based on its key or index. This feature allows you to create more generic and reusable code, enabling you to access and manipulate properties in a type-safe manner

type Post = {
  title: string;
  content: string;
  status: "draft" | "published";
  tags: Array<string>;
  authors: Array<"jazim" | "abbas">;
};

type Status = Post["status"];
// type Status = "draft" | "published"

Another approach to extract specific properties from a type is by using the Pick utility type. However, it's important to note that Pick creates a new object and copies only the specified fields into the new type

type Status = Pick<Post, "status">;
// type Status = {
//   status: "draft" | "published";
// };

Often, when working with external libraries, you may encounter situations where you need to extract a specific type from an array-based type provided by the library. While the library might provide a type like the following code snippet demonstrates:

type Posts = Array<{ title: string; content: string }>;

There are cases where you only require the type of a single post, rather than an array of posts. Many developers resort to manually creating a new type by duplicating all the fields from the array type. However, there is a more efficient solution that leverages TypeScript's indexing capabilities.

By utilizing TypeScript's indexing feature, you can easily extract the desired type without the need for manual copying. The following code snippet demonstrates how:

type Post = Posts[number];
// Post = { title: string; content: string }

In this example, Posts[number] is used to access the type of an individual post within the Posts array type. This approach dynamically retrieves the type of a single post from the array, eliminating the need for duplicating field definitions.

In this example, Posts[number] is used to access the type of an individual post within the Posts array type. This approach dynamically retrieves the type of a single post from the array, eliminating the need for duplicating field definitions.

Pick Property

I have observed developers creating new types solely to extract specific properties from an existing type. However, TypeScript offers built-in utility types like Pick that allow you to achieve the same result without the need to define additional types.

The Pick utility type in TypeScript is designed to selectively pick properties from an existing type and construct a new type with only those selected properties.

type Post = {
  title: string;
  content: string;
  status: "draft" | "published";
  tags: Array<string>;
  authors: Array<"jazim" | "abbas">;
};

type Status = Pick<Post, "status">;
// type Status = {
//   status: "draft" | "published";
// };

You can also pick multiple properties e.g.

type Post = {
  title: string;
  content: string;
  status: "draft" | "published";
  tags: Array<string>;
  authors: Array<"jazim" | "abbas">;
};

type Status = Pick<Post, "status" | "tags">;
// type Status = {
//   status: "draft" | "published";
//   tags: Array<string>
// };

Omit Property

The Omit utility type in TypeScript enables you to exclude selected properties from an existing type, resulting in a new type that contains all properties except the ones you specify.

type Post = {
  title: string;
  content: string;
  status: "draft" | "published";
  tags: Array<string>;
  authors: Array<"jazim" | "abbas">;
};

type NewPost = Omit<Post, "tags" | "authors">;
// type NewPost = {
//   title: string;
//   content: string;
//   status: "draft" | "published";
// };

Partial Property

The Partial utility type in TypeScript allows you to create a new type with all properties of an existing type marked as optional. This means that you can choose to provide values for some or none of the properties when working with the resulting type

type Post = {
  title: string;
  content: string;
  status: "draft" | "published";
  tags: Array<string>;
  authors: Array<"jazim" | "abbas">;
};

type OptionalPost = Partial<Post>;
// type OptionalPost = {
//   title?: string;
//   content?: string;
//   status?: "draft" | "published";
//   tags?: Array<string>;
//   authors?: Array<"jazim" | "abbas">;
// };

Readonly Property

The Readonly utility type in TypeScript allows you to create a new type where all properties of an existing type are marked as read-only. This ensures that once an object is instantiated with the resulting type, its properties cannot be modified

type Post = {
  title: string;
  content: string;
  status: "draft" | "published";
  tags: Array<string>;
  authors: Array<"jazim" | "abbas">;
};

type ReadonlyPost = Readonly<Post>;
// type ReadonlyPost = {
//   readonly title: string;
//   readonly content: string;
//   readonly status: "draft" | "published";
//   readonly tags: Array<string>;
//   readonly authors: Array<"jazim" | "abbas">;
// };

Conclusion

While there are many more utility types that TypeScript offers, the ones discussed in this article are some of the most commonly used and impactful ones in everyday coding. These utility types serve as a foundation for understanding and leveraging TypeScript's powerful type system.

TypeScript utility types offer a wide range of possibilities for manipulating and transforming types. By harnessing the power of these utility types, you can enhance type safety, improve code readability, and reduce manual effort.

Understanding their syntax, applications, and potential limitations empowers you to write cleaner, more maintainable code and navigate complex type transformations with ease.

As you continue to explore TypeScript utility types, don't forget to experiment, combine them, and discover new ways to leverage their capabilities. With practice and a deep understanding of these utility types, you'll become a more proficient TypeScript developer, capable of tackling complex type scenarios and building robust applications