Given the following simple class:
class Observer {
private subscribers: Map<string, Array<((data: any) => void)>> = new Map();
public subscribe(event: string, callback: (data: any) => void) {
if (!this.subscribers.has(event)) {
this.subscribers.set(event, []);
}
this.subscribers.get(event).push(callback); //tsc says: Object is possibly 'undefined'
}
}
Furthermore, in tsconfig.json, the flags strictNullChecks
and strict
are enabled.
Although subscribers
is checked for a key of the current event, the typescript compiler complains with the error message shown above (this.subscribers.get(event)
is possibly undefined).
If I'm not completely wrong, this.subscribers.get(event)
can never be undefined
in this case.
How can I get rid of that message?