I am trying to declare a simple map structure in TypeScript with string keys and some object as a value. Looking at several StackOverflow answers, this is what I came up with:
class Person {
id: number;
name: string;
}
interface PersonMap {
[id: string]: Person;
}
When I try to use this declaration, TypeScript is interpreting PersonMap as an array and not as a map, for example I am not able to get keys or values from the map:
let personMap = {} as PersonMap;
personMap[1] = {
id: 1,
name: 'John',
}
let people = personMap.values();
TypeScript complains about the last statement above:
Property 'values' does not exist on type 'PersonMap'
What is the best way to declare a Map in TypeScript?
P.S. It does not look like TypeScript has the ES6 Map type built in. In any case, I would like to create my own Map, not an ES6 map.