Skip to content

Bad type checking inside typeguarded block #32625

@lonewarrior556

Description

@lonewarrior556

TypeScript Version: 3.5.1

Search Terms: Type Guard

interface FOO {
    [key:string]: number
}

declare var foo: FOO | null

if(foo) {
    var a =  foo['string'] // good
    Object.keys(foo).map(key => {
        var b =  foo[key]  // foo cannot possibly be null
    })
}

//related
foo && foo['string'] && Object.keys(foo).map(key => {
    var a = foo[key] // foo cannot possibly be null
})

Expected behavior:
Should know Object is not 'null'

Actual behavior:
Thinks Object is possibly 'null'

Playground Link: ts-playground

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions