How to correctly use Map from ES6 in TypeScript? Typing and examples
Hello everyone! I started learning TypeScript after JavaScript and encountered a problem when using Map. In JS I could easily write:
```typescript
let map = new Map();
map.set('name', 'John');
map.set(42, 'answer');
```
But in TypeScript this causes compilation errors if you don't specify types. I tried different options but keep getting confused:
1. Just `new Map()` — works, but TypeScript complains about `any`
2. Tried to specify `Map` — seems to work, but the point of typing is lost
3. I want to store objects with different keys and values, but don't know how to properly describe the types
Specific task: I need to create a Map where keys are strings and values are objects with fields id (number) and name (string). I also want to understand if it's possible to use objects or functions as keys, like in ES6
No answers yet.
Log in to leave an answer
Blogs