typescript
/

TypeScript Mapped Types

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Mapped Types

What are Mapped Types?

Mapped types allow you to create new types by transforming properties of an existing type using iteration.

Basic Syntax

TypeScriptRead-only
1
type User = {
  name: string;
  age: number;
};

type ReadonlyUser = {
  [K in keyof User]: User[K];
};

Using Modifiers

TypeScriptRead-only
1
type PartialUser = {
  [K in keyof User]?: User[K];
};

Readonly Mapped Type

TypeScriptRead-only
1
type ReadonlyUser = {
  readonly [K in keyof User]: User[K];
};

Remove Modifiers

TypeScriptRead-only
1
type Mutable<T> = {
  -readonly [K in keyof T]: T[K];
};

Mapped Types with Generics

TypeScriptRead-only
1
type Nullable<T> = {
  [K in keyof T]: T[K] | null;
};

Relation with Utility Types

Utility types like Partial, Required, and Readonly are built using mapped types.

Best Practices

  • Use mapped types for transformations
  • Combine with generics for flexibility
  • Keep types readable
  • Reuse existing types effectively

Common Mistakes

  • Overcomplicating mapped types
  • Not understanding keyof usage
  • Ignoring readability
  • Using mapped types unnecessarily

Conclusion

Mapped types provide powerful capabilities to transform and reuse types efficiently in TypeScript.

Try it yourself

type User = { name: string };
type U = { [K in keyof User]: User[K] };

Test Your Knowledge

Q1
of 3

Mapped types use?

A
Loop
B
keyof
C
if
D
class
Q2
of 3

keyof returns?

A
Values
B
Keys
C
Types
D
Functions
Q3
of 3

Readonly modifier?

A
+
B
-
C
readonly
D
?

Frequently Asked Questions

What is mapped type?

A type created by transforming another type.

What is keyof?

It gets keys of a type.

Used for?

Type transformations.

Previous

ts type guards

Next

ts conditional types

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.