Advanced Types
Discriminated unions, keyof/typeof at the type level, and utility types beyond Partial/Pick/Omit.
Discriminated unions
A very common pattern: a union of object types that share a common literal property (the "discriminant"), which TypeScript uses to narrow the type inside an if/switch:
Without the shared status field to narrow on, you'd need repeated "data" in state checks or type assertions discriminated unions let the compiler do that work for you.
keyof
Produces a union of a type's property names as string literal types:
typeof (type-level)
Unlike the runtime typeof operator (which returns a string like "string"), TypeScript's type-level typeof extracts the type of a variable or object, so you don't have to redeclare a type that already matches something in your code:
Combined, keyof typeof is a common idiom for turning an existing object (like a lookup table) into a union of its keys:
More utility types
Beyond Partial/Required/Pick/Omit/Record (see Type):
ReturnType<T> extracts a function's return type:
Parameters<T> extracts a function's parameter types as a tuple:
Awaited<T> unwraps the type a Promise resolves to (handles nested Promises too):
Exclude<T, U> / Extract<T, U> filter members out of, or in from, a union:
NonNullable<T> removes null and undefined from a type: