TypeScript / TypeScript Advanced Types

Exploring Utility Types for Type Transformation

This tutorial dives into Utility Types provided by TypeScript. You will learn about Partial, Pick and Omit types and how they can be used to transform types, making your code more…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers advanced type concepts in TypeScript, including mapped types, conditional types, and type manipulation.

Introduction

In this tutorial, we'll explore three important utility types provided by TypeScript: Partial, Pick, and Omit. These utility types are powerful tools that can help you transform types in your code, leading to more concise and maintainable codebases.

By the end of this tutorial, you will understand:
- What utility types are and why they're useful
- How to use Partial, Pick, and Omit utility types
- Practical examples of these utility types in action

Before we begin, you should have a basic understanding of TypeScript and its type system. Familiarity with generic types will also be beneficial.

Step-by-Step Guide

Utility Types

In TypeScript, utility types are a set of generic types that provide common type transformations. They can be very useful in many situations, especially when dealing with complex type manipulation.

Partial Type

The Partial type takes an existing type and makes all of its properties optional. This is useful when you want to make a type that can have any subset of the properties of the original type.

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

// PartialPerson type will have all the properties of Person but they are all optional
type PartialPerson = Partial<Person>;

Pick Type

The Pick type allows you to construct a type by picking specific properties from an existing type.

interface Person {
  name: string;
  age: number;
  address: string;
}

// PickPerson type will only have the name and age properties of the Person type
type PickPerson = Pick<Person, 'name' | 'age'>;

Omit Type

The Omit type is the opposite of the Pick type. It constructs a type by omitting specific properties from an existing type.

interface Person {
  name: string;
  age: number;
  address: string;
}

// OmitPerson type will have all the properties of the Person type except for address
type OmitPerson = Omit<Person, 'address'>;

Code Examples

Let's look at some practical examples of how to use these utility types.

Using Partial Type

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

function updatePerson(person: Partial<Person>) {
  // update person
}

// You can call updatePerson with any subset of the Person properties
updatePerson({ name: 'John' });
updatePerson({ age: 30 });
updatePerson({ name: 'John', age: 30 });

Using Pick Type

interface Person {
  name: string;
  age: number;
  address: string;
}

function printPerson(person: Pick<Person, 'name' | 'age'>) {
  console.log(person);
}

// You can only call printPerson with an object that has name and age properties
printPerson({ name: 'John', age: 30 }); // { name: 'John', age: 30 }

Using Omit Type

interface Person {
  name: string;
  age: number;
  address: string;
}

function printPerson(person: Omit<Person, 'address'>) {
  console.log(person);
}

// You can call printPerson with an object that doesn't have an address property
printPerson({ name: 'John', age: 30 }); // { name: 'John', age: 30 }

Summary

In this tutorial, we've learned about three useful utility types in TypeScript: Partial, Pick, and Omit. We've seen how they can be used to transform types, making our code more concise and maintainable.

For further learning, I suggest you explore the other utility types available in TypeScript.

Practice Exercises

  1. Create a type that represents a subset of a Car interface with properties: make, model, year, color. The new type should only have make and model properties.

  2. Create a type that represents a User interface with properties: name, email, phoneNumber, address. The new type should exclude phoneNumber.

  3. Use the Partial utility type to create a function that updates a Product interface with properties: id, name, price.

Make sure to test your solutions and compare them with the examples provided in the tutorial.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

JWT Decoder

Decode and validate JSON Web Tokens (JWT).

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help