と地図の使い方<>:活字体:以下の単純なクラスを考えるstrictNullChecks
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'
}
}
さらに、tsconfig.jsonで、フラグstrictNullChecks
とstrict
が有効になっています。
subscribers
は現在のイベントのキーがチェックされていますが、typescriptコンパイラは上記のエラーメッセージ(this.subscribers.get(event)
はおそらく定義されていません)に苦情を返します。
私は完全に間違っていないよ場合は、this.subscribers.get(event)
は、この場合にはundefined
なることはありません。
は、どのように私は、そのメッセージを取り除くことができますか?