2021年4月4日星期日

Why typescript complains object's key is undefined even if the previous command sets a value to that key?

type MaybeThereIsAValue = {    [p: string]: string | undefined  }    ...    let bar: MaybeThereIsAValue = {};  const key = "carpe";  bar[key] = "diem";    const why = bar[key];  // why is string | undefined    

Why is why string | undefined since I am literally assigning a value to bar[key] in the previous line ?

How can I avoid it ?

enter image description here

Check example here

https://stackoverflow.com/questions/66930004/why-typescript-complains-objects-key-is-undefined-even-if-the-previous-command April 03, 2021 at 06:20PM

没有评论:

发表评论