Generate boolean flags
Create a configuration type whose entries indicate whether corresponding features are enabled.
TypeScript · Function
Mapped types transform an existing type by iterating over a union of property keys, commonly keyof another type. They can change property values, modifiers, and—using key remapping—property names.
A mapped type creates a new type by iterating over a key union, commonly obtained with keyof, and assigning a transformed property type to each key. The mapped declaration can also preserve, add, remove, or conditionally rename properties while retaining their relationship to the source type.
type Mapped<Type> = {
[Property in keyof Type]: NewType;
};| Name | Type | Description |
|---|---|---|
Type | generic type parameter | The source type whose keys and property types are transformed. |
A new object type whose properties are produced by iterating over the selected keys.
type Features = {
darkMode: () => void;
newUserProfile: () => void;
};
type OptionsFlags<Type> = {
[Property in keyof Type]: boolean;
};
type FeatureOptions = OptionsFlags<Features>;
const enabled: FeatureOptions = {
darkMode: true,
newUserProfile: false,
};The mapped type transforms every property in Features into a boolean property.
Create a configuration type whose entries indicate whether corresponding features are enabled.
Derive an editable or required variant without duplicating every property declaration.
Generate renamed properties or omit selected members while deriving an API surface.
A mapped type iterates over a union of PropertyKey values, often keyof Type, and creates one output property per key. Its value expression can transform each source property, such as changing every value to boolean.
Follow-up: How can a mapped type remove selected properties? Use key remapping with an as clause and produce never for keys that should be excluded.
The modifiers are readonly and ?. A modifier without a prefix means +, while - removes the corresponding modifier, for example -? makes mapped properties required.
Follow-up: How would you make every property mutable and required? Apply -readonly and -? in the mapped declaration.
An as clause remaps each source key to a new key type. Conditional logic can return never to filter a key, and template literal types can derive names from existing keys.
Follow-up: What constraint is useful when remapping only string keys? A conditional such as Key extends string ? ... : never excludes number and symbol keys.
keyofindex signaturesconditional typestemplate literal typesPartialReadonly