So I want to implement a map with two-part keys in typescript. My key format is like this:
type Key = {
section: number,
index: number
}
I have tried to create a map like this:
let map = new Map<Key,Record>()
But whenever I try to get a value from the map:
cont val = map.get(myKey)
The value is undefined.
Is there any way to have a map in typescript with complex types?
Because you are using an object as a key of Map you need to make sure the reference key that you have used to set the map, is the same when you want to get from the map!
let myKey:Key = {index:1,section:2}
map.set(myKey,your object)
const val = map.get(myKey); // Now has value
But I suggest you to use primitive type as a key. So you can append section and index as new string key!