Menu

1 / 2

Keeping TypeScript DRY with Pick and Omit

DEV Community·Rocky Chowdhury·6 months ago
#iF5hvas0
Reading 0:00
15s threshold

In a real-world codebase, interfaces grow. A User type might have 20+ properties covering everything from authentication to UI preferences. But not every part of the codebase needs all of them — a login form cares only about email and password , while an admin panel might need everything except password .

Without the right tools, you end up duplicating type definitions across files. That duplication is a quiet bug incubator. TypeScript's Pick and Omit utility types solve this by letting you derive focused, specialized types directly from a master interface — define each property exactly once and never repeat yourself.

The Problem: Manual Type Duplication

Start with a master User interface:

interface User { id : number ; name : string ; email : string ; password : string ; role : " admin " | " user " ; createdAt : Date ; }

Enter fullscreen mode

Exit fullscreen mode

Without utility types, a developer might write separate interfaces for different contexts:

// For the login form interface…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.