TypeScript reporting Object is possibly 'undefined' when it shouldn't

前端 未结 3 2077
面向向阳花
面向向阳花 2021-01-20 16:53

I\'m trying to understand why TypeScript is giving me the following error: Object is possibly \'undefined\'

Here\'s the code snippet:



        
3条回答
  •  -上瘾入骨i
    2021-01-20 17:16

    If you query for a key not in the map, you will get undefined. Not because the map holds an undefined value, but because it does not hold a value at all.

    Try

    results.get(key)!

    Or

    results.get(key) as number

提交回复
热议问题