Skip to content

Suggestion: Let indexable types have return type (<type> | undefined) #13161

Closed
@jpanda109

Description

@jpanda109

TypeScript Version: 2.1.4

Allowing indexing of indexable interfaces act similar to ES6 Map.get seems like it would help the type safety of the language.

Example:

interface Foo {
    [name: string]: number
}
let foo: Foo = {};
let baz = foo["bar"];
// baz is of type number and compiles fine, even though is actually undefined
// possibly make baz of type (number | undefined) instead

Expected Behavior:
baz is of type (number | undefined)

Actual Behavior:
baz is of type number, despite actually being type undefined

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