I am getting various different data structures from external sources. In get()
i am checking for undefined
and existence
of the property, but still getting compiler errors.
Is there a quick fix for that as we are just prototyping at the moment ?
class MyCache {
private map: Map<string, object>;
constructor() {
this.map = new Map<string, object>();
}
populateMap(uuid: string): boolean {
// ... getting a JSON object from somewhere
let externalData: object = {};
if (uuid == 'abc...') {
externalData = {"bar": "BAR", "session": uuid}
} else if( uuid == "def...") {
externalData = {"foo": "FOO", "session": uuid}
// ... some more else ifs ...
} else {
externalData = {"baz": "BAZ", "session": uuid}
}
this.map.set(uuid, externalData);
console.log(externalData);
return true;
}
get(uuid: string) {
let response = this.map.get(uuid) || {'session': null};
if (response.hasOwnProperty('session')) {
return response.session;
}
return '';
}
}
Error:
Property 'session' does not exist on type 'object'.