In TypeScript, there are several built-in utility types that make it easier to manipulate types in a flexible, dynamic way. Here are some of the most commonly used utility types:
Utility Type | Description | Example |
| Makes all properties in |
|
| Makes all properties in |
|
| Makes all properties in |
|
| Selects a subset of properties from |
|
| Excludes a subset of properties from |
|
| Constructs an object type with keys of type | `type RecordExample = Record<'name' |
| Excludes types from | `type NonString = Exclude<string |
| Extracts types from | `type OnlyString = Extract<string |
| Excludes | `type Defined = NonNullable<string |
| Extracts the instance type of a constructor function type |
|
| Extracts the return type of a function type |
|
| Extracts the parameter types of a function type |
|
| Extracts the parameter types of a constructor function type |
|
| Extracts the type of |
|
| Removes the |
|
| Unwraps the type of a promise, useful for |
|
Example Usage of Utility Types
Using
Partial
for Optional ParameterstypescriptCopy codeinterface User { name: string; age: number; } type PartialUser = Partial<User>; // { name?: string; age?: number; }
Using
Pick
to Select Specific PropertiestypescriptCopy codeinterface User { name: string; age: number; email: string; } type UserWithAge = Pick<User, 'name' | 'age'>; // { name: string; age: number; }
Using
Record
to Define a MappingtypescriptCopy codetype Status = 'success' | 'error' | 'loading'; type StatusMessages = Record<Status, string>; const messages: StatusMessages = { success: 'Operation was successful', error: 'There was an error', loading: 'Loading...' };
Using
Exclude
to Remove Specific TypestypescriptCopy codetype AllowedTypes = Exclude<string | number | boolean, boolean>; // Result: string | nu
Using
Extract
to extract types from a Union
Imagine you have a interface and want to create union type for properties that are of a specific type, like string.typescriptCopy codeinterface User { id: number; name: string; email: string; isAdmin: boolean; } type StringProperties = Extract<keyof User, "name" | "email">; // Result: "name" | "email"
Using NonNullable
to exclude null and undefined from a given type.
You might have an object type where some properties are optional, and you want to create a type where those properties are guaranteed to be defined.
typescriptCopy codeinterface User {
id: number;
name: string | null;
email?: string;
}
type DefinedUser = {
[K in keyof User]: NonNullable<User[K]>
};
// Result:
// DefinedUser is now:
// {
// id: number;
// name: string;
// email: string;
// }
Using InstanceType
to extracts the instance type from a given constructor function type.
Suppose you have a User
class, you want to get the type of an instance of User
using InstanceType.
typescriptCopy codeclass User {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
getInfo() {
return `${this.name} (ID: ${this.id})`;
}
}
type UserInstance = InstanceType<typeof User>;
// Now, `UserInstance` is equivalent to:
// {
// id: number;
// name: string;
// getInfo: () => string;
// }
const user: UserInstance = new User(1, "Alice");
console.log(user.getInfo()); // Output: "Alice (ID: 1)"