More Utility Types

More Utilities
πŸ‘¨β€πŸ’Ό Let's explore more utility types for objects and union types.
type Shelf = Record<string, number>
type LockedBook = Readonly<{ title: string; pages: number }>
type BookPreview = Omit<{ id: string; title: string; summary: string }, 'id'>
type FullBook = Required<{ title?: string; author?: string }>

type Status = 'draft' | 'published' | null
type PublishedOnly = Exclude<Status, 'draft' | null>

Object Utilities

  • Record<K, V> - Create object with keys K and values V
  • Readonly<T> - Make all properties readonly
  • Omit<T, K> - Remove properties K from T
  • Required<T> - Make all properties required

Union Utilities

  • Exclude<T, U> - Remove types assignable to U from T
  • Extract<T, U> - Keep only types assignable to U in T
  • NonNullable<T> - Remove null and undefined from T
🐨 Open
index.ts
and:
  1. Use Record to create a config type
  2. Use Readonly to make a user immutable
  3. Use Omit to create a user type without id
  4. Use Required to make optional properties required
  5. Use Exclude, Extract, and NonNullable on union types

Please set the playground first

Loading "More Utility Types"
Loading "More Utility Types"
Login to get access to the exclusive discord channel.
Loading Discord Posts