What are Utility Types in TypeScript?
Utility Types are built-in (ready-made) TypeScript utility types that help you quickly create, modify, or combine types without writing out the whole structure by hand.
They let you "transform" existing types -
making properties optional, readonly, excluding fields, picking subsets, and so on.
1. What Utility Types are
Utility Types are special generic types built into TypeScript that perform common operations on other types.
An example of the idea:
interface User {
id: number;
name: string;
age?: number;
}
type ReadonlyUser = Readonly<User>;
ReadonlyUseris the sameUser, but with all propertiesreadonly.
2. The most popular Utility Types
| Utility | What it does | Example |
|---|---|---|
Partial<T> | Makes all fields of type T optional | Partial<User> |
Required<T> | Makes all fields required | Required<User> |
Readonly<T> | Makes all fields read-only | Readonly<User> |
Pick<T, K> | Selects only the specified fields from a type | `Pick<User, 'id' |
Omit<T, K> | Excludes the specified fields from a type | Omit<User, 'age'> |
Record<K, V> | Creates an object with keys K and values V | Record<string, number> |
Exclude<T, U> | Excludes from T all types assignable to U | `Exclude<'a' |
Extract<T, U> | Keeps only the types from T assignable to U | `Extract<'a' |
NonNullable<T> | Removes null and undefined | `NonNullable<string |
ReturnType<F> | Extracts the return type of a function | ReturnType<() => string> → string |
Parameters<F> | Extracts the parameter types of a function | Parameters<(a:number,b:string)=>void> → [number,string] |
InstanceType<C> | The instance type of a class | InstanceType<typeof User> |
3. Usage examples
Partial<T>
Makes all properties optional.
interface User {
id: number;
name: string;
age: number;
}
const updateUser = (user: Partial<User>) => {
// only the needed fields can be passed
};
updateUser({ name: "Tim" }); // okRequired<T>
The opposite of Partial - makes all fields required.
interface Options {
debug?: boolean;
cache?: boolean;
}
type StrictOptions = Required<Options>;
const opts: StrictOptions = {
debug: true,
cache: false, // now required
};Readonly<T>
Prevents properties from being changed.
interface Config {
port: number;
host: string;
}
const cfg: Readonly<Config> = { port: 3000, host: "localhost" };
cfg.port = 8080; // Error: readonlyPick<T, K>
Creates a type with only the specified fields.
interface User {
id: number;
name: string;
email: string;
}
type UserPreview = Pick<User, "id" | "name">;
const u: UserPreview = { id: 1, name: "Tim" };Omit<T, K>
Creates a type without the specified fields.
type UserWithoutEmail = Omit<User, "email">;
const u: UserWithoutEmail = { id: 1, name: "Tim" };Record<K, V>
Creates an object with keys K and values V.
type Roles = Record<string, number>;
const userRoles: Roles = {
admin: 1,
user: 2,
};Very convenient for typing dictionaries and enum-like structures.
Exclude<T, U>
Removes subtypes from a union.
type Letters = "a" | "b" | "c";
type NoA = Exclude<Letters, "a">; // "b" | "c"Extract<T, U>
Keeps only the common types.
type Letters = "a" | "b" | "c";
type OnlyA = Extract<Letters, "a" | "x">; // "a"NonNullable<T>
Removes null and undefined.
type MaybeString = string | null | undefined;
type DefinitelyString = NonNullable<MaybeString>; // stringReturnType<F>
Gets the type of a function's return value.
function getUser() {
return { id: 1, name: "Tim" };
}
type UserType = ReturnType<typeof getUser>;
// { id: number; name: string }Parameters<F>
Gets a function's parameter types as a tuple.
function multiply(a: number, b: number): number {
return a * b;
}
type Args = Parameters<typeof multiply>; // [number, number]InstanceType<C>
Gets the type of a class instance.
class Person {
name = "Tim";
}
type PersonInstance = InstanceType<typeof Person>;
const user: PersonInstance = new Person();4. Combining Utility Types
Utilities can be easily combined with each other:
interface User {
id: number;
name: string;
email?: string;
}
type SafeUser = Readonly<Partial<User>>;
// all fields are optional and readonly5. Your own Utility Type (custom)
You can create your own utilities using mapped types and keyof:
type Nullable<T> = {
[K in keyof T]: T[K] | null;
};
type UserWithNulls = Nullable<User>;Now every field of
Usercan benull.
Summary
Utility Types are built-in TypeScript tools for "transforming" and "modifying" types without duplicating code.
The main groups:
- modifying properties (
Partial,Required,Readonly); - selecting and excluding fields (
Pick,Omit); - creating collections (
Record); - operations on unions (
Exclude,Extract,NonNullable); - working with functions (
ReturnType,Parameters); - classes (
InstanceType).
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.