VSCode TypeScript Map get is still considered undefined even with a has check before

Viewed 136

If I defined a simple Map with: const map = new Map<string, string>(); and later want to do something with it I would expect this to work:

if (map.has(key)) {
    const value = map.get(key);
    // do something with it
}

But VSCode gives me this error for get:

Object is possibly 'undefined'.ts(2532)

So I have to revert to this:

const value = map.get(key);
if (value) {
    // do something with it
}

This just doesn't feel right and makes the has method almost useless. Is this a TypeScript problem or a VSCode Problem?

0 Answers
Related