Sub topic 4
Homomorphic mapped types and how they preserve structure
This chapter assumes you know object types, generics with type parameters, keyof, indexed access types such as T[K], and the basic mapped type syntax { [K in keyof T]: ... }. The examples assume strict is enabled in tsconfig.json.
What makes a mapped type homomorphic
A mapped type is homomorphic when its key constraint has the form keyof T, where T is a type. TypeScript then copies the readonly and ? modifiers from each property of T onto the matching property of the result. You do not write these modifiers yourself. The mapped type keeps them.
type Config = { readonly host: string; port?: number };
type Tagged<T> = { [K in keyof T]: T[K] };
type Copy = Tagged<Config>;
// { readonly host: string; port?: number }Tagged changes nothing about the value types, yet the result still has host as read-only and port as optional. That preservation is the reason the category has a name. The mapped type keeps the structure of the original type.
Losing the modifiers with an explicit key list
A mapped type that names its keys directly is not homomorphic. TypeScript then has no source type to copy modifiers from.
type Config = { readonly host: string; port?: number };
type Explicit = { [K in "host" | "port"]: Config[K] };
// { host: string; port: number | undefined }Both readonly and ? are gone. The port property is now required, and its type includes undefined because Config["port"] includes undefined under strict. If you want the modifiers kept, write the constraint as keyof Config or keyof T instead of a literal union.
Arrays and tuples
When a homomorphic mapped type is instantiated with an array or tuple, TypeScript maps the element type and returns an array or tuple. This has been true since TypeScript 3.1.
type Box<T> = { value: T };
type Boxed<T> = { [K in keyof T]: Box<T[K]> };
type BoxedNames = Boxed<string[]>; // Box<string>[]
type BoxedPair = Boxed<[number, string]>; // [Box<number>, Box<string>]If you write a non-homomorphic mapped type, the array shape is lost and you get an object with numeric and method keys. Keep the keyof T form when you want the result to stay an array or tuple.
flowchart TD
A[Mapped type with constraint keyof T] --> B{Is T a type parameter or type?}
B -->|Yes| C[Homomorphic: readonly and optional copied]
B -->|No, explicit key union| D[Not homomorphic: modifiers dropped]
C --> E{Is T an array or tuple?}
E -->|Yes| F[Result is array or tuple]
E -->|No| G[Result is object type]caption: Decision path for whether a mapped type keeps modifiers and array shape. alt: A flowchart. A mapped type with a keyof T constraint is homomorphic and copies modifiers. A mapped type with an explicit key union drops them. Homomorphic types that receive an array or tuple return an array or tuple.
Common errors
- Expecting modifiers after using a literal union. The result loses
readonlyand?. Switch tokeyof T. - Expecting arrays from a non-homomorphic type. Only the
keyof Tform maps arrays and tuples element by element. - Forgetting that optional properties include
undefined. Understrict,T[K]for an optional property includesundefined, so the mapped result may too.
> callout note
Exercise
Exercise
Given type Settings = { readonly theme: string; retries?: number };, what does type Clone<T> = { [K in keyof T]: T[K] } produce for Clone<Settings>, and what does type Flat = { [K in "theme" | "retries"]: Settings[K] } produce?
Show answer
Clone<Settings> keeps readonly theme: string and retries?: number, because the constraint is keyof T. Flat drops both modifiers and gives theme: string; retries: number | undefined.