Transforming Types, Not Values

Take a typical case, made up for illustration. A checkout form had a model plus three parallel types: field errors, touched flags and change handlers. A deliveryNotes field was added to the model and nowhere else. Its validation errors were silently dropped, and the change handler only compiled with a cast.

Mapped types let you write “one of these per field” once and derive it from the model. With key remapping and template literals they can rename keys too, and with recursion they can transform nested types (carefully). Below is the foundation, key remapping, combining with template literals, and deep transformations that do not break dates and arrays.

If parallel types keep falling out of step
  • You have a type and a "partial version" of it defined separately that you keep manually syncing
  • You write out { onClick?: () => void; onFocus?: () => void; onBlur?: () => void } by hand instead of deriving it
  • You've seen { [K in keyof T]: ... } in TypeScript source and moved on without understanding it
  • You want event handler types from a list of event names but don't know how to generate them

Mapped types with template literals eliminate whole categories of manual type maintenance — one source type, infinite derived shapes.