4 TypeScript Utility Types That Delete Half Your Types
Do you have a User type, plus UserUpdate, PublicUser and UserPreview, all copying the same fields? Every time you add a field, you have to update all of them, and one day you'll forget.
TypeScript's built-in utility types build those types from one source of truth. Here are the four you'll use every day, plus two bonuses. Every error message below is the real output of tsc --strict (TypeScript 7.0).
Start with one type:
interface User { id : number ; name : string ; email : string ; password : string ; role : "admin" | "editor" | "viewer" ; }
Every other user type will be derived from this one.
Partial<T> : every field optional
Perfect for update functions, where you only send what changed:
function updateUser ( id : number , changes : Partial < User > ) { /* … */ }
updateUser ( 1 , { name : "Ada" } ) ; // ✅ only what changed
updateUser ( 1 , { nmae : "Ada" } ) ; // ❌ error TS2353: Object literal may only specify known properties, and 'nmae' does not exist in type 'Partial<User>'.
Optional doesn't mean unchecked: typos are still caught.
Pick and Omit : choose fields
Pick keeps only the fields you list. Omit removes them.
type UserPreview = Pick < User , "id" | "name" > ; // { id: number; name: string }
type PublicUser = Omit < User , "password" > ;
const pub : PublicUser = { id : 1 , name : "Ada" , email : "a@x.dev" , role : "admin" , password : "hunter2" , // ❌ } ;
error TS2353 : Object literal may only specify known properties , and 'password' does not exist in type 'PublicUser' .
When you add a field to User , both types update automatically.
Record<K, V> : an object with known keys
type Role = User [ "role" ] ; // "admin" | "editor" | "viewer"
const permissions : Record < Role , string [ ] > = { admin : [ "read" , "write" , "delete" ] , editor : [ "read" , "write" ] , } ;
error TS2741 : Property 'viewer' is missing in type '{ admin: string[]; editor: string[]; }' but required in type 'Record<"admin" | "editor" | "viewer", string[]>' .
I forgot viewer , and TypeScript caught it. Add a new role later and every Record<Role, …> tells you exactly what's missing. That's much safer than { [key: string]: string[] } .
Bonus: ReturnType<typeof fn>
function createUser ( data : Omit < User , "id" > ) { return { . . . data , id : 42 , createdAt : new Date ( ) } ; }
type CreatedUser = ReturnType < typeof createUser > ; // User fields + createdAt: Date, without writing it by hand
The type stays in sync with the function. Change what it returns and the type follows.
Combine them (+ Readonly )
The real power comes from nesting them:
type UserPatch = Partial < Omit < User , "id" | "password" >> ;
const patch : UserPatch = { email : "n**@x.dev" } ; // ✅
const bad : UserPatch = { id : 7 } ; // ❌ error TS2353 : Object literal may only specify known properties , and 'id' does not exist in type 'Partial<Omit<User, "id" | "password">>' .
Every field is optional, but id and password can't be patched at all.
Readonly<T> blocks changes, which is great for config or state:
const user : Readonly < User > = loadUser ( ) ;
user . role = "viewer" ; // error TS2540: Cannot assign to 'role' because it is a read-only property.
( Readonly is shallow, and it's a compile-time check only, so nested objects can still be mutated.)
Cheat sheet
Partial<T>- All fields optional - Updates, patchesPick<T, K>- Keep only these fields - Previews, list itemsOmit<T, K>- Remove these fields - Hide secretsRecord<K, V>- Object with exactly these keys - Lookup tablesReadonly<T>- No assignments allowed - Config, stateReturnType<typeof fn>- Type of what a function returns - Keep types in sync
Write the type once and derive the rest. Which utility type do you use the most? 👇
I post one tested coding tip every day on YouTube at Refactorly . The 30-second version of this one is here .
Comments
No comments yet. Start the discussion.