Learn Programming, Tech & Coding · Free Online Tools

IT Question Answer
Back to TypeScript
TypeScript Utility Types Guide

TypeScript Utility Types Guide

TypeScript916 viewsBy Admin
typescriptutilitytypes

What are Utility Types?

TypeScript ships built-in utility types that transform existing types so you do not have to rewrite them. They save huge amounts of boilerplate.

Partial<T> — Make All Fields Optional

interface User { id: number; name: string; email: string; }

function update(id: number, fields: Partial<User>) { /* ... */ }
update(1, { name: "New Name" }); // ✅ only some fields

Pick<T, K> & Omit<T, K>

type PublicUser = Omit<User, "email">;       // { id, name }
type Credentials = Pick<User, "email">;       // { email }

Required<T> & Readonly<T>

type StrictUser = Required<User>;   // all fields mandatory
type FrozenUser = Readonly<User>;   // cannot reassign fields

Record<K, V> — Build a Map Type

type Roles = Record<string, boolean>;
const perms: Roles = { admin: true, guest: false };

ReturnType & Parameters

function makeUser() { return { id: 1, name: "x" }; }
type U = ReturnType<typeof makeUser>; // { id: number; name: string }

Cheat Sheet

UtilityDoes
Partial<T>All fields optional
Required<T>All fields required
Pick<T,K>Keep only K fields
Omit<T,K>Remove K fields
Record<K,V>Object with K keys, V values

FAQs

Can I combine utility types?

Yes — Partial<Omit<User, "id">> is perfectly valid and common.

Where are these defined?

In TypeScript's built-in lib.es5.d.ts — no import needed. More in our TypeScript section.