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:
- Create
Configas string keys β number values, then aconfigvalue{ timeout: 5000, retries: 3 } - Create
ReadonlyUserand areadonlyUserfixture{ id: '1', name: 'Alice', email: 'a@b.com' } - Create
UserWithoutIdand anewUserfixture{ name: 'Bob', email: 'b@b.com' } - Create
RequiredUserand afullUserfixture that includes requiredbio: 'Hello!'andwebsite: 'https://alice.dev' - For
Status = 'pending' | 'active' | 'inactive' | 'deleted' | null | undefined:ActiveStatusβ Status values that remain after removing deleted and nullish entriesValidStatusβ removesnullandundefinedStringStatusβ keeps only the string variants
- Create fixtures
status = 'active'(typed asValidStatus) andactiveStatus = 'pending'(typed asActiveStatus) - Export
config,readonlyUser,newUser,fullUser,status, andactiveStatus