Skip to content

TypeScript failing to narrow field types indexed w/ const assertions #45965

Closed

Description

Bug Report

🔎 Search Terms

const assertion
const narrow
assertion narrow

🕗 Version & Regression Information

First seen in 4.1.2, reproduced in 4.4.3 and 4.5.0-dev.20210919.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Type System Behavior", "Type Guards", "Things That Don't Work"

⏯ Playground Link

Playground link with relevant code

💻 Code

const test: { myfield?: number } = {};
const constField = 'myfield' as const;

if (test[constField]) {
  console.log(test[constField] + 1); // Object is possibly 'undefined'
  console.log(test.myfield + 1); // Object is possibly 'undefined'
}

🙁 Actual behavior

TypeScript is not able to narrow test[constField] from undefined|number to number

🙂 Expected behavior

TypeScript infers that test[constField] is simply number in the if block

Activity

  1. IllusionMH commented on Sep 20, 2021

    @IllusionMH
    Contributor

    Looks like duplicate of #44899 and linked issues.

  2. easyrider commented on Sep 20, 2021

    @easyrider

    This is also possibly a #10530 duplicate - an issue that appears frequently since 2016, the solution was already proposed as a PR but was declined.

  3. bsuth commented on Sep 21, 2021

    @bsuth
    Author

    indeed this seems to be a duplicate, closing in favor of #10530

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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