Skip to content

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

Description

@DanTup

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions