Skip to content

type incorrectly narrowed because assignment of a different value in function not seenΒ #62672

Closed as duplicate

Description

@190n

πŸ”Ž Search Terms

"incorrect narrowing," "control flow assignment"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about incorrect narrowing

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.0-dev.20251024#code/DYUwLgBAZg9jBcEDeEBGBDATogdgVwFtURMIBfCAHwn2GAgF4a86BuAKCjxwGMwBLGDgjoAzqP4BzHAAoAlMnYRocRsjRZEAVnIcy7AO4ALfqAgywmPCAVIlEflHOwYjBk1rBb95WInT5DmUKEGBREEVlZQA3GH4AExUYADoMTCDydn0gA

πŸ’» Code

let foo: { bar: number } | null = null;
function assign() {
  foo = { bar: 5 };
}
while (true) {
  if (foo === null) {
    assign();
  } else {
    void foo.bar;
  }
}

πŸ™ Actual behavior

There is an error on the bar property lookup. foo has been narrowed to null because TypeScript thinks we never assign it to something other than null, and in the else branch it is narrowed to never because we've checked it is not null so there are no more types it could possibly be.

πŸ™‚ Expected behavior

There should be no error. If TypeScript cannot prove that foo is never assigned a non-null value then it should not assume that.

Additional information about the issue

No response

Activity

  1. jcalz commented on Oct 25, 2025

    @jcalz
    Contributor

    Yet another duplicate of #9998

  2. 190n commented on Oct 25, 2025

    @190n
    Author

    Ah sorry I didn't find that one, thanks.

  3. locked as resolved and limited conversation to collaborators on Jan 7, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions