'How to specify a declared interface as the type of Map? [duplicate]
There was a problem studying the typescript.
How to specify a declared interface as the type of map?
exmaple code)
interface User {
id: number;
name: string;
age: number;
house: {
id: number;
address: string;
...,
...
}
...,
...
}
const userMap = new Map<User>(); // a different motion than expected...
Solution 1:[1]
Map Generics require either 0 or 2 parameters.
If you want the value of the Map to be the User interface, and assuming you want a string as the key, you can do as follows:
const userMap = new Map<string, User>();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luke Storry |
