Skip to content

narrowing object by property doesn't work if key isn't a constΒ #51368

Closed
@DetachHead

Description

@DetachHead

Bug Report

πŸ”Ž Search Terms

narrow property const

πŸ•— Version & Regression Information

4.9.0-dev.20221025

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Data {
    a?: number
}

declare const data: Data

let key = 'a' as const

if (data.a !== undefined) {
    key // "a"
    const a = data[key] // number | undefined
    const b = data['a'] // number
}

πŸ™ Actual behavior

data[key] is number | undefined

πŸ™‚ Expected behavior

data[key] is narrowed to number

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