Property value transformation
Concept and syntax
Indexed access T[K] retrieves each corresponding property type during mapping. Wrapping that type preserves the relationship between the key and its value. A uniform replacement would lose that relationship.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type Boxes<T>={[K in keyof T]:{value:T[K]}}; type B=Boxes<{count:number;label:string}>;const b:B={count:{value:2},label:{value:"tea"}}; console.log(b.count.value,b.label.value);Expected output:
2 teaCommon mistake
Intentionally invalid; compile separately and do not execute.
type Boxes<T>={[K in keyof T]:{value:T[K]}}; type B=Boxes<{count:number;label:string}>;const b:B={count:{value:"two"},label:{value:"tea"}};Expected diagnostic: TS2322. T["count"] is number, so the count box needs a number.
Practice and recap
Wrap every property in a one-element tuple instead.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Property value transformation — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Property value transformation is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Boxes<T>={[K in keyof T]:{value:T[K]}}; type B=Boxes<{count:number;label:string}>;const b:B={count:{value:2},label:{value:"tea"}}; console.log(b.count.value,b.label.value);Which diagnostic and explanation match the rejected example under the lesson settings?
type Boxes<T>={[K in keyof T]:{value:T[K]}}; type B=Boxes<{count:number;label:string}>;const b:B={count:{value:"two"},label:{value:"tea"}};What is Boxes<{x:boolean}>["x"]["value"]?