In my TypeScript project, I'm trying to define a simple map structure with string keys and an object as a value. After researching on StackOverflow, this is the code snippet I've put together:
class Person {
id: number;
name: string;
}
interface PersonMap {
[id: string]: Person;
}
Unfortunately, when attempting to use this declaration, TypeScript seems to interpret PersonMap as an array instead of a map. This means I am unable to access keys or values from the map:
let personMap = {} as PersonMap;
personMap[1] = {
id: 1,
name: 'John',
}
let people = personMap.values();
The error message I receive from TypeScript pertains to the last line above:
Property 'values' does not exist on type 'PersonMap'
I'm curious about the best approach for creating a Map in TypeScript, especially since it appears that TypeScript doesn't have a built-in ES6 Map type. Regardless, I prefer to come up with my custom Map implementation.