Skip to content

Bad type checking inside typeguarded block #32625

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:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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