Patterns
One page per question. Every recipe compiles, every result is the type the compiler printed, and every page says where the recipe goes wrong.
Every recipe and every result is compiled at build time
- How do I stop a `UserId` being passed where an `OrderId` was expected, when both are just strings?TypeScript is structural, so two aliases of `string` are the same type. A phantom property makes them different without changing the value.Brand a primitive
- How do I get a compile error when someone adds a case to a union and forgets to handle it?Narrow every case and assign what is left to `never`. Once the union grows, what is left is no longer nothing, and the assignment fails.Make a switch exhaustive
- How do I stop `Omit<User, "nmae">` silently succeeding and removing nothing?The built-in constrains its key parameter to `keyof any`, which is every key there could ever be. Constrain it to `keyof T` instead.Make Omit catch typos
- How do I turn `{ a: string } & { b: number }` into `{ a: string; b: number }` so tooltips are readable and equality checks pass?A homomorphic mapped type walks every key once and rebuilds a single object. Modifiers survive; the intersection does not.Flatten an intersection
- Why does `Object.keys(config)` give me `string[]` instead of the keys I can see right there?Because a value can have more keys than its type declares, and `Object.keys` returns all of them. Narrowing the return is a deliberate, local decision — not a fix for a bug.Get typed keys from an object
- How do I accept an options object where every field is optional, but the empty object is not allowed?Build one variant per key where that key is required and the rest are optional, then union them. Any single supplied key satisfies at least one variant.Require at least one property
- How do I read `user.address.city` through a reusable path without losing the result type?Generate the valid key tuples recursively, then consume one key at a time to compute the value at the end.Get a value by a typed deep path
- How do I keep each event name paired with the right payload and listener?Use an event map and one key parameter so the event name selects the payload type in both methods.Type an event emitter
- How do I make success data available only in the success branch and errors only in the failure branch?Give every variant one shared literal field. Checking it narrows the whole object, not just the field.Model states that narrow
- How do I let callers choose an input and transform without writing type arguments at the call site?Place type parameters where values reveal them: one in the array element and one in the callback return.Infer a generic result from a callback
- How do I accept either `href` or `onClick`, but reject objects that provide both?Union the two shapes and add the other side’s unique keys as optional `never` properties.Make two prop shapes mutually exclusive
- How do I accept one search key while rejecting both an empty query and a query with several keys?Build one variant per key: require that key and forbid every other key with optional `never`.Require exactly one property
- How do I get only the keys whose properties are strings, functions, or another chosen type?Map every key to itself or `never`, then index the mapped object to collect the survivors.Select keys by their value type
- How do I prevent mutation below the first property level instead of only freezing the outer object type?Preserve functions, recurse through arrays, and map every object property with a readonly modifier.Make an object deeply readonly
- How do I keep a literal union and the array or object used at runtime from drifting apart?Keep one value as the source of truth, preserve its literals with `as const`, then query its type.Derive a union from runtime data
- How do I preserve the relationship between each key and its value while iterating `Object.entries`?Build a union of key/value tuples with a mapped type and keep the unavoidable assertion inside one helper.Get typed entries from an object
- How do I make a helper infer a literal tuple without requiring callers to write `as const`?Mark the type parameter `const`; inference then prefers readonly literal shapes over widened arrays and primitives.Preserve literals with a const generic
- How do I accept callbacks that return either `T` or `Promise<T>` and always expose one predictable result?Accept a maybe-promise, await it, and describe the flattened result with `Awaited`.Normalize a sync or async callback
- How do I select one property from every object and have the returned array keep that property’s type?Bind the key to `keyof T` and use indexed access for the return element.Pluck a typed property from objects
- How do I index a discriminated union by its `kind` instead of repeatedly extracting members?Map over the union and remap each member to the literal value of its discriminant.Turn a union into a lookup map

