webroad.online
  1. 1Web
  2. 2HTML
  3. 3CSS
  4. 4JavaScript
  5. 5TypeScript
  6. 6Git
  7. 7Tooling
  8. 8React
  9. 9State management
  10. 10Next.js
  11. 11Forms
  12. 12Data and backend
  13. 13SEO
  14. 14Tailwind CSS
  15. 15Animations
  16. 16Testing
  17. 17Architecture
TypeScript · Lesson 5 of 6

Utility types, as const, satisfies

Partial, Pick, Omit, Record, ReturnType and types derived from values.

Updated

What utility types are

TS comes with ready-made types that transform other types. Instead of rewriting User in 5 variants (for creating, updating, displaying), you derive them from a single User. The benefit: change User once, and every variant updates.

type User = { id: number; name: string; email: string; password: string }

type UserUpdate = Partial<User>              // everything optional
type PublicUser = Omit<User, 'password'>     // no password
type UserPreview = Pick<User, 'id' | 'name'> // only a few fields

The most used ones — what they do and when

Utility What it does When
Partial<T> every field optional a patch / partial update, default values
Required<T> every field required after filling in the defaults
Readonly<T> every field readonly config, state you don't change
Pick<T, K> only the keys K props for a small component
Omit<T, K> everything except K removing sensitive or generated fields (id)
Record<K, V> an object with keys K and values V dictionaries, mappings
ReturnType<F> a function's return type typing from existing code
Parameters<F> a tuple of the parameter types wrappers around functions
Awaited<T> the type inside a Promise Awaited<ReturnType<typeof getUser>>
NonNullable<T> removes null and undefined after a check

typeof and keyof — types from values

const config = { theme: 'dark', lang: 'en' }
type Config = typeof config                 // { theme: string; lang: string }
type ConfigKey = keyof Config               // 'theme' | 'lang'

async function getUser(id: number) { ... }
type UserData = Awaited<ReturnType<typeof getUser>>   // the real type, without rewriting it

as const — values as literals

const ROLES = ['admin', 'editor', 'viewer'] as const
type Role = (typeof ROLES)[number]          // 'admin' | 'editor' | 'viewer'

A single list is both a value at runtime (for a <select>) and a type at compile time. No duplication.

satisfies — checking without losing the exact type

const routes = {
  home: '/',
  blog: '/blog',
} satisfies Record<string, string>

routes.home            // the type stays exactly '/', not string
routes.shop            // ✗ doesn't exist
const x: T = ... const x = ... satisfies T
Checks the shape yes yes
The type of x T (wider) the exact one from the value

Summary

  • Derive types from a single one: Partial, Pick, Omit, Record.
  • typeof / ReturnType / Awaited — types from existing code.
  • as const for lists of literals; satisfies for checking without widening.

Official sources

Exercises

Was this page helpful?

One tap — no account needed.