microsoft/TypeScript

Hashmap (unexpectedly) doesn't include undefined as a possible return value when using strictNullChecks

DanTup opened this issue · 4 comments

Apologies if this has been covered before an/or is not a bug but I can't seem to find it in the issue list or any discussion of it online.

TypeScript Version: 2.0.3

Given this code:

let danny: { [name: string]: string; } = {};
let whatIsMyType = danny["test2"];

the type of whatIsMyType is string (I would expect string | undefined). This means I can write a method that pulls from the map and claims to return a string without any warning:

getPackagePath(name: string): string {
    return this.map[name];
}

Shouldn't | undefined be added implicitly to the type returned from a map like this?

Same issue as #9235.

closing in favor of #9235.

@mhegazy I think you forgot to close