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.
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.
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>;
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'>;
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'>;
Let's look at some practical examples of how to use these utility types.
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 });
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 }
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 }
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.
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.
Create a type that represents a User
interface with properties: name
, email
, phoneNumber
, address
. The new type should exclude phoneNumber
.
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.