Skip to content

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

Closed
@andreialecu

Description

@andreialecu

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