Unlocking TypeScript: 7 Essential Utility Types Explained
Written on
Introduction to TypeScript Utility Types
In the realm of modern web development, TypeScript has emerged as the go-to technology. While TypeScript provides various ways to define your code, its utility types serve as powerful tools that enhance clarity, cleanliness, and reduce the chances of hidden errors.
Utility Type 1: keyof
The keyof operator allows you to extract the keys from an object. For instance, if you have an object with properties such as name and age, using keyof on that object will yield a union of its keys, which would be string | number.
const person = {
name: "John",
age: 24,
};
const personKeys = keyof person; // personKeys will be "name" | "age"
console.log(personKeys); // Output: name | age
Utility Type 2: ReturnType
The ReturnType type enables you to determine the return type of a function. For example, if you have a function that returns a string, you can use ReturnType to ascertain that the return value is of type string.
function getString(): string {
return "Hello";}
const returnType = ReturnType;
// returnType will be "string"
console.log(returnType); // Output: string
Utility Type 3: Awaited
The Awaited type helps you derive the type of the result from a promise that you are awaiting. For instance, if you have an asynchronous function that returns a promise of a string, you can use Awaited<Type> to identify the awaited result type.
async function getStringAsync(): Promise<string> {
return "Hello";}
const awaitedReturnType = Awaited<string>;
// awaitedReturnType will be "string"
console.log(awaitedReturnType); // Output: string
Utility Type 4: Pretty
The Pretty type, created by Matt Poco, retrieves all properties of a nested type, regardless of how deeply nested it is. If you have a type with several levels of nesting, Pretty will give you a type that encompasses all properties from the nested structure.
type MainType = {
name: string;
age: number;
};
type NestedType = MainType & {
isDeveloper: boolean;};
const prettyNestedType = Pretty;
// prettyNestedType will include all properties of MainType and NestedType
Utility Type 5: Partial
The Partial type transforms all properties of an object into optional ones. If you have an interface with multiple properties, applying Partial results in a type where all properties can be absent.
interface Todo {
title: string;
description: string;
}
const partialTodo: Partial<Todo> = {};
// partialTodo can include any or none of the properties of Todo
Utility Type 6: Required
Conversely, the Required type mandates that all properties of an object must be present. If you have an interface with various attributes, using Required ensures that every property is included in the resulting type.
interface Todo {
title: string;
description: string;
}
const requiredTodo: Required<Todo> = { title: "Hello" };
// requiredTodo must have all properties of Todo
Utility Type 7: Omit
The Omit type is utilized to exclude specific properties from an object type. For instance, if you have an interface with several attributes, Omit can help you create a type that excludes certain properties.
interface Todo {
title: string;
description: string;
createdAt: Date;
}
const todoWithoutCreatedAt: Omit<Todo, 'createdAt'> = { title: "Hello", description: "World" };
// todoWithoutCreatedAt does not have the createdAt property
Learn more about the Omit operator in this article:
Enjoyed the content? Give it a thumbs up ๐ to encourage more insights!
Chapter 2: Video Insights on TypeScript Utility Types
In this video titled "TypeScript's Utility Types... Blazing fast," you can explore an in-depth discussion about various utility types in TypeScript, enhancing your coding skills and understanding.
The video "TypeScript Utility Types You Must Learn" provides valuable insights and examples that will deepen your comprehension of these essential tools in TypeScript.