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?
您必须使用set
函数,例如
this.myMap.set(myObj, 1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.