Homomorphic mapping
Concept and syntax
A homomorphic mapped type of the form [K in keyof T] can preserve source modifiers and tuple structure. Mapping a tuple of elements does not simply produce a generic dictionary. Explicit modifier changes can override preserved modifiers.
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 Box<T>={[K in keyof T]:{value:T[K]}}; type Pair=Box<readonly [number,string]>;const x:Pair=[{value:2},{value:"tea"}]; console.log(x[0].value,x[1].value);Expected output:
2 teaCommon mistake
Intentionally invalid; compile separately and do not execute.
type Box<T>={[K in keyof T]:{value:T[K]}}; type Pair=Box<readonly [number,string]>;const x:Pair=[{value:2},{value:"tea"}]; x[0]={value:3};Expected diagnostic: TS2540. The homomorphic tuple mapping preserved readonly on the tuple.
Practice and recap
Compare a mutable tuple with a readonly tuple.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Homomorphic mapping — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Homomorphic mapping is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Box<T>={[K in keyof T]:{value:T[K]}}; type Pair=Box<readonly [number,string]>;const x:Pair=[{value:2},{value:"tea"}]; console.log(x[0].value,x[1].value);Which diagnostic and explanation match the rejected example under the lesson settings?
type Box<T>={[K in keyof T]:{value:T[K]}}; type Pair=Box<readonly [number,string]>;const x:Pair=[{value:2},{value:"tea"}]; x[0]={value:3};Which operation is rejected on the readonly mapped Pair?