Skip to content

noUncheckedIndexedAccess does not narrow properly with "prop" in obj #43614

Description

Bug Report

Apologies if this is a duplicate, but I wasn't able to find anything.

🔎 Search Terms

noUncheckedIndexedAccess property in object

⏯ Playground Link

Playground link

💻 Code

declare function invariant(condition: boolean): asserts condition;

function foo(obj: Readonly<Record<string, string>>) {
  invariant(obj && "test" in obj);
  // this one below does narrow to `string`
  // invariant(!!obj["test"]);

  const x = obj["test"] // string | undefined but should be string
}

🙁 Actual behavior

x is typed as string | undefined

🙂 Expected behavior

x should be typed as string

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions