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 VReadonly<T>- Make all properties readonlyOmit<T, K>- Remove properties K from TRequired<T>- Make all properties required
Union Utilities
Exclude<T, U>- Remove types assignable to U from TExtract<T, U>- Keep only types assignable to U in TNonNullable<T>- Remove null and undefined from T
π¨ Open
and:
- Use
Recordto create a config type - Use
Readonlyto make a user immutable - Use
Omitto create a user type without id - Use
Requiredto make optional properties required - Use
Exclude,Extract, andNonNullableon union types