Typescript Utility Types
Introduction to TypeScript Utility Types
TypeScript utility types are a set of predefined types that can be used to manipulate and transform other types. They are a powerful feature of the TypeScript type system, allowing developers to create more expressive and flexible types. In this tutorial, we will explore some of the most commonly used TypeScript utility types, including Partial, Readonly, Pick, Omit, Record, and Exclude.
Partial Type
The Partial type is used to create a new type that is a subset of an existing type, where all properties are optional. This is useful when you want to create a type that has some, but not all, of the properties of an existing type.
interface User {
name: string;
age: number;
address: string;
}
type PartialUser = Partial;
const user: PartialUser = {
name: 'John Doe',
// age and address are not required
};
Readonly Type
The Readonly type is used to create a new type that is a subset of an existing type, where all properties are readonly. This is useful when you want to ensure that certain properties cannot be modified.
interface User {
name: string;
age: number;
}
type ReadonlyUser = Readonly;
const user: ReadonlyUser = {
name: 'John Doe',
age: 30,
};
// user.name = 'Jane Doe'; // Error: Cannot assign to 'name' because it is a read-only property.
Pick Type
The Pick type is used to create a new type that is a subset of an existing type, where only certain properties are included. This is useful when you want to create a type that has only a few properties of an existing type.
interface User {
name: string;
age: number;
address: string;
}
type NameAndAge = Pick;
const user: NameAndAge = {
name: 'John Doe',
age: 30,
};
Omit Type
The Omit type is used to create a new type that is a subset of an existing type, where certain properties are excluded. This is useful when you want to create a type that has all properties of an existing type, except for a few.
interface User {
name: string;
age: number;
address: string;
}
type UserWithoutAddress = Omit;
const user: UserWithoutAddress = {
name: 'John Doe',
age: 30,
};
Record Type
The Record type is used to create a new type that is an object with a certain set of properties, where each property has a specific type. This is useful when you want to create a type that has a dynamic set of properties.
type Color = 'red' | 'green' | 'blue';
type Colors = Record;
const colors: Colors = {
red: '#ff0000',
green: '#00ff00',
blue: '#0000ff',
};
Exclude Type
The Exclude type is used to create a new type that is a subset of an existing type, where certain types are excluded. This is useful when you want to create a type that has all types of an existing type, except for a few.
type NumberOrString = number | string;
type ExcludeNumber = Exclude;
const value: ExcludeNumber = 'hello';
Conclusion
In this tutorial, we have explored some of the most commonly used TypeScript utility types, including Partial, Readonly, Pick, Omit, Record, and Exclude. These types can be used to manipulate and transform other types, creating more expressive and flexible types. By using these utility types, you can write more robust and maintainable code, and take advantage of the full power of the TypeScript type system.