ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

TypeScript Tutorial• 8 min read

Advanced Built-in Utility Types

Mastering Partial, Required, Pick, Omit, Record, and ReturnType

TypeScript includes a rich suite of built-in utility types that facilitate common type transformations, keeping your codebase DRY (Don't Repeat Yourself).

Essential Utility Types in Daily Development

Partial<T>: Constructs a type with all properties of T set to optional.

Required<T>: Constructs a type with all properties of T set to required.

Readonly<T>: Makes all properties immutable (cannot be reassigned).

Pick<T, K>: Constructs a type by picking a set of properties K from T.

Omit<T, K>: Constructs a type by picking all properties from T and then removing keys K.

Record<K, T>: Constructs an object type whose property keys are K and whose values are T.

Transforming Types with Utilities
interface Article {
  id: string;
  title: string;
  content: string;
  author: string;
  published: boolean;
  viewCount: number;
}

// 1. DTO for updating an article: All fields optional
type UpdateArticleDto = Partial<Article>;

// 2. Summary preview card: Only pick headline info
type ArticlePreview = Pick<Article, 'id' | 'title' | 'author'>;

// 3. New article creation: Omit server-generated id and viewCount
type CreateArticleDto = Omit<Article, 'id' | 'viewCount'>;

// 4. Category dictionary lookup table
type ArticleCatalog = Record<'tech' | 'design' | 'business', Article[]>;

const updateDraft: UpdateArticleDto = {
  title: "Mastering TypeScript Utility Types"
}; // Valid without providing other fields!
Utility types eliminate the need to manually declare duplicate partial interfaces.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which utility type constructs a new shape by removing specific keys from an existing interface?