Skip to content

Type guard "in" for undefined not working when using strictNullChecks + noUncheckedIndexedAccess #54241

Closed
@kkmuffme

Description

@kkmuffme

Bug Report

type guard in undefined array indexed access

🔎 Search Terms

🕗 Version & Regression Information

  • This changed from version 4 with the introduction of noUncheckedIndexedAccess
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&filetype=ts#code/FAYw9gdgzgLgBAQwEYgFxwEoFNwCcAmAPLLgJYQDmANCeRQHxwC8cA3gL4DcwwAZgK4QQMUpDhIEuABSJcFdLUpwAlG2BwNcUrzgzJFLREQoVazebjho8fAhgJmxkAG1ZBgLrdz7YD6A

💻 Code

const abc: Record<string,string> = {};

function bar( arg: string ) {
    if ( arg in abc ) {
        const data = abc[ arg ];
    }
}

🙁 Actual behavior

data is string|undefined despite it being typeguarded with "in" on a Record<string, string> which means that it cannot be undefined

🙂 Expected behavior

data should be string

The current bug causes either lots of redundant code (as we have to typeguard for undefined again separately) or disabling noUncheckedIndexAccess which would snooze tons of potential bugs though.

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