TypeScript built-in utility types transform existing types without duplication. Partial<T> makes all properties optional. Required<T> makes all required. Pick<T,K> extracts a subset of keys. Omit<T,K> removes keys. Readonly<T> prevents mutation. Record<K,V> constructs a mapped type. Awaited<T> unwraps Promise. These compose: Readonly<Partial<User>> makes a fully optional immutable User type.
Combining utility types for DTO patterns.
Don't write a PublicUser type manually that copies all User props minus password. Use Omit<User, 'password'>. When User gains a new property, PublicUser automatically gains it too.
Record<'admin'|'editor'|'viewer', Permission[]> is safer than {[key: string]: Permission[]} because it ensures all three roles are present. Index signatures allow any string key.
Utility types compose. Readonly<Partial<T>> is a perfectly valid immutable partial type. Required<Omit<T,"secret">> removes a key and ensures the rest are required. Build from primitives.
Sign in to share your feedback and join the discussion.