Skip to main content

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:

javascript
interface User { id: number; name: string; age?: number; } type ReadonlyUser = Readonly<User>;

ReadonlyUser is the same User, but with all properties readonly.


UtilityWhat it doesExample
Partial<T>Makes all fields of type T optionalPartial<User>
Required<T>Makes all fields requiredRequired<User>
Readonly<T>Makes all fields read-onlyReadonly<User>
Pick<T, K>Selects only the specified fields from a type`Pick<User, 'id'
Omit<T, K>Excludes the specified fields from a typeOmit<User, 'age'>
Record<K, V>Creates an object with keys K and values VRecord<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 functionReturnType<() => string>string
Parameters<F>Extracts the parameter types of a functionParameters<(a:number,b:string)=>void>[number,string]
InstanceType<C>The instance type of a classInstanceType<typeof User>

3. Usage examples


Partial<T>

Makes all properties optional.

javascript
interface User { id: number; name: string; age: number; } const updateUser = (user: Partial<User>) => { // only the needed fields can be passed }; updateUser({ name: "Tim" }); // ok

Required<T>

The opposite of Partial - makes all fields required.

javascript
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.

javascript
interface Config { port: number; host: string; } const cfg: Readonly<Config> = { port: 3000, host: "localhost" }; cfg.port = 8080; // Error: readonly

Pick<T, K>

Creates a type with only the specified fields.

javascript
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.

javascript
type UserWithoutEmail = Omit<User, "email">; const u: UserWithoutEmail = { id: 1, name: "Tim" };

Record<K, V>

Creates an object with keys K and values V.

javascript
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.

javascript
type Letters = "a" | "b" | "c"; type NoA = Exclude<Letters, "a">; // "b" | "c"

Extract<T, U>

Keeps only the common types.

javascript
type Letters = "a" | "b" | "c"; type OnlyA = Extract<Letters, "a" | "x">; // "a"

NonNullable<T>

Removes null and undefined.

javascript
type MaybeString = string | null | undefined; type DefinitelyString = NonNullable<MaybeString>; // string

ReturnType<F>

Gets the type of a function's return value.

javascript
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.

javascript
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.

javascript
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:

javascript
interface User { id: number; name: string; email?: string; } type SafeUser = Readonly<Partial<User>>; // all fields are optional and readonly

5. Your own Utility Type (custom)

You can create your own utilities using mapped types and keyof:

javascript
type Nullable<T> = { [K in keyof T]: T[K] | null; }; type UserWithNulls = Nullable<User>;

Now every field of User can be null.


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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.