In my code, I have a specific type defined as follows:
type mapOptions = {
'a': {},
'b': {
'somethingElse': string,
'somethingDiff': number
},
'c': {
'somethingC': string
}
}
Now, what I am aiming to do is create a Map where the keys correspond to the keys of my object, and each value corresponds to a particular object structure, like this:
type innerMapObject<T extends keyof mapOptions> {
status: boolean,
options: mapOptions[T]
}
The main goal here is to ensure that when interacting with the map - whether fetching or setting values - the correct underlying option types are enforced:
const MyMap: Map<...> = new Map();
MyMap.set('d', {}); // This should trigger an error since "d" is not a property within "mapOptions"
MyMap.set('a', {}); // This operation should work without any issues
MyMap.set('c', {
"somethingD": "test"
}); // Here, we expect an error because the value object does not align with "mapOptions["c"]"
/** The expected type for myBValue should be:
*
* {
* status: boolean,
* options: {
* somethingElse: string,
* somethingDiff: number
* }
* }
*
*
*/
const myBValue = MyMap.get("b");
Are there any possible solutions to reference the key of the map within the associated value?