Mapped types

A mapped type iterates through a set of keys to create a new type.

You can apply a ? modifier during mapping to make the mapped values optional, or use a -? to make the mapped value required.

If you are using a more recent version of TypeScript, you can re-map keys in mapped types with an as clause.

You can also filter out keys by conditionally producing the never type.

You can map over arbitrary unions, not just string | number | symbol.

Mapped types can be useful in enforcing compiler guarantees based on the data and constants in your programs.

Was this page helpful?