How to Initialize a Map in TypeScript (With Examples)

To initialize a Map in TypeScript with data, pass an array of [key, value] pairs to the constructor and put the types in angle brackets:

const prices = new Map<string, number>([["SKU-1001", 19.99]]);

That gives a typed Map<string, number>, and TypeScript rejects later set() calls with the wrong types.

In this tutorial, I’ll show you how to initialize a Map in TypeScript five ways, using a small SKU price list I compiled with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode.

How do you initialize an empty Map in TypeScript?

Write the types in the generic, then add entries with set(), which returns the Map so calls chain:

const prices = new Map<string, number>();
prices.set("SKU-1001", 19.99).set("SKU-1002", 4.5);

console.log("size:", prices.size);
console.log("SKU-1002:", prices.get("SKU-1002"));
console.log("SKU-9999:", prices.get("SKU-9999"));

Output:

size: 2
SKU-1002: 4.5
SKU-9999: undefined

The size is 2, and a missing key gives undefined. Skip the generic and you’ll get Map<any, any>. For the bigger picture, see key value pairs in TypeScript.

Initialize a Map with values from an array of pairs

Same price list, now passed straight into the constructor:

const prices = new Map<string, number>([
  ["SKU-1001", 19.99],
  ["SKU-1002", 4.5],
  ["SKU-1003", 129],
]);

for (const [sku, price] of prices) {
  console.log(sku, `$${price.toFixed(2)}`);
}

Output:

SKU-1001 $19.99
SKU-1002 $4.50
SKU-1003 $129.00
VS Code terminal output of a TypeScript Map initialized from an array of key-value pairs and printed with a for...of loop
Each SKU prints with its price in dollars, in the order the pairs were written.

Leave out the generic and TypeScript infers the types from the pairs. The for…of loop in TypeScript gives you each pair as a destructured [key, value].

Got an array of objects instead? You can create a Map from an array in TypeScript too.

Initialize a Map from an object with Object.entries()

The constructor won’t take a plain object, but Object.entries() turns one into pairs. Here the price list starts as a Record:

const priceList: Record<string, number> = {
  "SKU-1001": 19.99,
  "SKU-1002": 4.5,
  "SKU-1003": 129,
};

const prices = new Map(Object.entries(priceList));
console.log(prices);

const back = Object.fromEntries(prices);
console.log(back);

Output:

Map(3) { 'SKU-1001' => 19.99, 'SKU-1002' => 4.5, 'SKU-1003' => 129 }
{ 'SKU-1001': 19.99, 'SKU-1002': 4.5, 'SKU-1003': 129 }
VS Code showing a TypeScript Map initialized from Object.entries of a Record and converted back with Object.fromEntries
Node prints the Map with arrows between keys and prices, then the plain object rebuilt by Object.fromEntries().

Object.fromEntries() goes the other way, which is handy for JSON. If you’re unsure which shape to keep, compare Record vs Map in TypeScript.

Initialize a Map with object values or object keys

Values can be any type, so an interface works well. Keys can be objects too:

interface Product { name: string; price: number; inStock: boolean }

const catalog = new Map<string, Product>([
  ["SKU-1001", { name: "Desk lamp", price: 19.99, inStock: true }],
  ["SKU-1002", { name: "Coffee mug", price: 4.5, inStock: false }],
]);
console.log(catalog.get("SKU-1001")?.name);

const megan = { name: "Megan Bowen" };
const visits = new Map<object, number>([[megan, 3]]);
console.log(visits.get(megan));
console.log(visits.get({ name: "Megan Bowen" }));

Output:

Desk lamp
3
undefined
VS Code terminal showing a TypeScript Map of Product objects and an object-keyed Map returning undefined for a look-alike key
The catalog lookup prints Desk lamp; the object key finds 3 visits, but a look-alike object returns undefined.

Important: object keys match by reference, not by content.

The ?. guards get(), which can return undefined. To list the values later, convert the Map to an array.

Copy an existing Map in TypeScript

Pass one Map to the constructor and you’ll get a new Map with the same entries:

const prices = new Map<string, number>([["SKU-1001", 19.99]]);

const sale = new Map(prices);
sale.set("SKU-1001", 17.99);
console.log("original:", prices.get("SKU-1001"));
console.log("copy:    ", sale.get("SKU-1001"));

Output:

original: 19.99
copy:     17.99

The original keeps 19.99. The copy is shallow, so object values stay shared; see how to deep clone an object in TypeScript. Combining two Maps is a separate job covered in merging Maps in TypeScript.

ReadonlyMap drops set(), delete() and clear() from the type; use it when a Map shouldn’t change after setup.

Common errors when initializing a Map

Two mistakes come up most: passing an object to the constructor and writing to a ReadonlyMap:

const fromObject = new Map({ "SKU-1001": 19.99 });

const locked: ReadonlyMap<string, number> = new Map();
locked.set("SKU-1002", 4.5);

Output:

map_errors.ts(1,30): error TS2769: No overload matches this call.
  The last overload gave the following error.
    Object literal may only specify known properties, and '"SKU-1001"' does not exist in type 'readonly (readonly [unknown, unknown])[]'.
map_errors.ts(4,8): error TS2339: Property 'set' does not exist on type 'ReadonlyMap<string, number>'.
VS Code Problems badge and terminal showing TypeScript errors TS2769 for new Map with an object and TS2339 for set on ReadonlyMap
Both lines get red squiggles, the Problems badge shows 2, and the terminal shows TS2339 for set on ReadonlyMap.

Fix the first with Object.entries(). For the second, edit a copy: new Map(locked).

Working with key-value data elsewhere? These pair well with Maps:

Frequently asked questions

How do I initialize a Map with values in TypeScript?

Pass an array of pairs to the constructor: new Map<string, number>([["a", 1], ["b", 2]]).

How do I declare an empty typed Map?

Write new Map<KeyType, ValueType>(), then add entries with set().

Can I pass an object to new Map()?

No. Use new Map(Object.entries(obj)); a plain object causes TS2769.

Does new Map(oldMap) make a deep copy?

No. Object values are still shared between both Maps.

When should I use a Map instead of an object?

When you need non-string keys, frequent adds and deletes, or a built-in size.

Where is the Map constructor documented?