TypeScript Generics and Advanced Type Operators
Mapped types — transform property contracts
A mapped type constructs a type by iterating over a union of property keys. The value expression may use the current key and the corresponding source property. Mapping over keyof T can preserve existing modifiers; + and - modifiers can add or remove optionality and readonly. An as clause may rename keys or map a key to never to omit it. No object properties are created or modified at runtime by the type itself. Example output: "true false". Local strict TypeScript 5.8.3 / ES2022 baseline. The type construct itself is erased; the logged value illustrates a checked use, not runtime type validation.
Formal Syntax & Structure
typescript syntaxtypescript
type Flags<T> = { [K in keyof T]: boolean };Working Example
Example Snippettypescript
type Flags<T> = { [K in keyof T]: boolean };type Fields = { title: string; count: number };const flags: Flags<Fields> = { title: true, count: false };console.log(flags.title, flags.count);