When working with Angular's http client to fetch data from an API, I create DTOs using typescript interfaces. One of the endpoints returns a JSON object that contains dynamic keys which I want to map into a Map
. After trying out different approaches,
Version 1:
export interface ItemDistribution {
id: number;
distribution: Map<string, number>; // Keys are unknown beforehand
}
Version 2:
interface Distribution {
[key: string]: number;
}
export interface ItemDistribution {
id: number;
distribution: Distribution; // Keys are unknown beforehand
}
However, when I make a call to the backend using
http.get<ItemDistribution>(myUrl)
, the result is parsed into a JavaScript object instead. While I am aware that I could use Object.entries(...)
on an object, I am curious if there is a way to directly retrieve a Map?