I have been trying to initialize a Map type from a JSON blob, when I have a defined type for the expected JSON response. For example, we have a t-shirt size type
.
This is how the JSON blob looks like:
const blob = `{
"name": "TShirtSize",
"description": {
"S": "Small",
"M": "Medium",
"L": "Large"
}
}`
and this is a type I have created for the JSON blob.
export type TShirtType = {
name?: string
description: Map<string, string>
}
when I parse the JSON as TShirtType
, I won't transform the description
to the Map of <string, string>
. I get an error tshirt.description.get
is not a function.
const tshirt = JSON.parse(blob) as TShirtType
if (tshirt.description !== undefined) {
console.log(tshirt.description)
console.log(tshirt.description.get('S'))
}
I could define it as { [key: string]: string }
and call tshirt.description['S']
to get value but I wish to use Map instead. I was wondering if this is possible.