10LOC

#conditional-types

TypeScriptintermediate

Extract and Exclude in a tiny parser helper

type Assignment = { kind: "assign"; key: string; value: string };
type Flag = { kind: "flag"; key: string };
type Token = Assignment | Flag | { kind: "comment"; text: string };

const tokenize = (line: string): Token => {

Use utility conditional types to parse and narrow a small input grammar.

TypeScriptintermediate

Awaited<T> and unwrapping nested promise types

type Nested = Promise<Promise<Promise<string>>>;
type Flat = Awaited<Nested>;

export async function loadUser(): Promise<{ id: string; name: string }> {
  return { id: "1", name: "Ada" };

Unwrap Promise<Promise<Promise<T>>> down to T in one step, the way await does at runtime, using TypeScript's built-in recursive helper.

TypeScriptadvanced

Building DeepPartial<T> from scratch

export type DeepPartial<T> = T extends readonly unknown[]
  ? T
  : T extends object
    ? { [K in keyof T]?: DeepPartial<T[K]> }
    : T;

Make every property optional at every nesting level, for patch-style updates, without touching arrays element by element.

TypeScriptadvanced

Conditional types + infer to extract a function's return type

type AsyncReturnType<Fn extends (...args: never[]) => unknown> = Fn extends (
  ...args: never[]
) => Promise<infer R>
  ? R
  : Fn extends (...args: never[]) => infer R

Build a return-type extractor that also unwraps the Promise, so async and sync functions land on the same resolved type.

TypeScriptadvanced

Template literal types for typed route strings

type ParamNames<Path extends string> = Path extends `${string}:${infer Param}/${infer Rest}`
  ? Param | ParamNames<Rest>
  : Path extends `${string}:${infer Param}`
    ? Param
    : never;

Extract :param names out of a route string at the type level, so a route's caller must supply exactly the params that route needs.