When you don’t want to repeat yourself, sometimes a type needs to be based on another type.
Mapped types build on the syntax for index signatures, which are used to declare the types of properties which has not been declared ahead of time:
typeOnlyBoolsAndHorses = {[key : string]: boolean |Horse ;};
constconforms :OnlyBoolsAndHorses = {del : true,rodney : false,};
A mapped type is a generic type which uses a union of PropertyKey
s (frequently created via a keyof
) to iterate through keys to create a type:
typeOptionsFlags <Type > = {[Property in keyofType ]: boolean;};
In this example, OptionsFlags
will take all the properties from the type Type
and change their values to be a boolean.
typeFeatureFlags = {darkMode : () => void;newUserProfile : () => void;};
typetype FeatureOptions = {FeatureOptions =OptionsFlags <FeatureFlags >;darkMode: boolean;
newUserProfile: boolean;
}
Mapping Modifiers
There are two additional modifiers which can be applied during mapping: readonly
and ?
which affect mutability and optionality respectively.
You can remove or add these modifiers by prefixing with -
or +
. If you don’t add a prefix, then +
is assumed.
// Removes 'readonly' attributes from a type's propertiestypeCreateMutable <Type > = {-readonly [Property in keyofType ]:Type [Property ];};
typeLockedAccount = {readonlyid : string;readonlyname : string;};
typetype UnlockedAccount = {UnlockedAccount =CreateMutable <LockedAccount >;id: string;
name: string;
}
// Removes 'optional' attributes from a type's propertiestypeConcrete <Type > = {[Property in keyofType ]-?:Type [Property ];};
typeMaybeUser = {id : string;name ?: string;age ?: number;};
typetype User = {User =Concrete <MaybeUser >;id: string;
name: string;
age: number;
}
Key Remapping via as
In TypeScript 4.1 and onwards, you can re-map keys in mapped types with an as
clause in a mapped type:
type MappedTypeWithNewProperties<Type> = {[Properties in keyof Type as NewKeyType]: Type[Properties]}
You can leverage features like template literal types to create new property names from prior ones:
typeGetters <Type > = {[Property in keyofType as `get${Capitalize <string &Property >}`]: () =>Type [Property ]};
interfacePerson {name : string;age : number;location : string;}
typetype LazyPerson = {LazyPerson =Getters <Person >;getName: () => string;
getAge: () => number;
getLocation: () => string;
}
You can filter out keys by producing never
via a conditional type:
// Remove the 'kind' propertytypeRemoveKindField <Type > = {[Property in keyofType asExclude <Property , "kind">]:Type [Property ]};
interfaceCircle {kind : "circle";radius : number;}
typetype KindlessCircle = {KindlessCircle =RemoveKindField <Circle >;radius: number;
}
Further Exploration
Mapped types work well with other features in this type manipulation section, for example here is a mapped type using a conditional type which returns either a true
or false
depending on whether an object has the property pii
set to the literal true
:
typeExtractPII <Type > = {[Property in keyofType ]:Type [Property ] extends {pii : true } ? true : false;};
typeDBFields = {id : {format : "incrementing" };name : {type : string;pii : true };};
typetype ObjectsNeedingGDPRDeletion = {ObjectsNeedingGDPRDeletion =ExtractPII <DBFields >;id: false;
name: true;
}