1

I wanted to create a Map object with an object as key and a number as value in Typescript. I defined the map object as follows:

myMap: Map<MyObj,number>;
myObj: MyObj;

and when I try to add a pair to this map object:

this.myMap[myObj]=1;

It tells me that TS2538 Type 'MyObj' cannot be used as an index type. Is this possible in Typescript?

oksuzlerOmer
  • 125
  • 1
  • 2
  • 11

1 Answers1

2

You have to use the set function e.g.

this.myMap.set(myObj, 1);
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • Thanks, that works. Is there also a way that I can bind this map's value's to an input? I tried ` – oksuzlerOmer Apr 17 '19 at 11:54
  • @oksuzlerOmer You might want to read this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map and if you still have issues then open a new question. – Murat Karagöz Apr 17 '19 at 12:10