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…
Section overview
5 resourcesCovers 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
-
Create a type that represents a subset of a
Carinterface with properties:make,model,year,color. The new type should only havemakeandmodelproperties. -
Create a type that represents a
Userinterface with properties:name,email,phoneNumber,address. The new type should excludephoneNumber. -
Use the Partial utility type to create a function that updates a
Productinterface 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article