I encountered an issue with my typescript dictionary. Whenever I try to assign a value to it, a specific error occurs.
TypeError: Cannot set property 'v1/items/someItemType/someItemId/EVENT/some
DataTypeId' of undefined
at MainClass.subscribe (G:\Projects\src\main\MainClass
.js:9:3522)
at Context.<anonymous> (G:\Projects\test\main\Wish.
spec.js:45:18)
at callFn (G:\Projects\node_modules\mocha\lib\runna
ble.js:315:21)
at Hook.Runnable.run (G:\Projects\node_modules\moch
a\lib\runnable.js:308:7)
at next (G:\Projects\node_modules\mocha\lib\runner.
js:298:10)
at Immediate._onImmediate (G:\Projects\node_modules
\mocha\lib\runner.js:320:5)
This is the structure of my dictionary:
interface StringTopicToMyTopicMap {
[topic: string]: MyTopic;
};
The interface for MyTopic is as follows:
export interface MyTopic {
dataTypeID: string;
itemID: string;
itemType: string;
dataType: MyDataType;
qos: number;
}
Here is how I attempted to assign a value to the dictionary:
private subscriptionTopicsMap: StringTopicToMyTopicMap;
subscribe(myTopic: MyTopic) {
if (_.isEmpty(myTopic)) {
throw new MyError('myTopic cannot be empty');
} else {
let topic: string;
topic = 'v1/items/' + myTopic.itemType + '/' + myTopic.itemID + '/'
+ myTopic.dataType + '/' + myTopic.dataTypeID;
this.subscriptionTopicsMap[topic] = myTopic;
this.client.subscribe(topic);
}
}
The error occurs at this line:
this.subscriptionTopicsMap[topic] = myTopic;
As someone new to typescript, I would appreciate any guidance on what might be going wrong here. Thank you for your advice.