GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `noUncheckedIndexedAccess` does not narrow properly when iterating with `for...in` · Issue #58082 · microsoft/TypeScript

noUncheckedIndexedAccess does not narrow properly when iterating with for...in #58082

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
noUncheckedIndexedAccess does not narrow properly when iterating with for...in#58082
Copy link
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript

Description

@ItsHarper
Harper Andrews (ItsHarper)
opened on Apr 4, 2024
Issue body actions

🔍 Search Terms

"noUncheckedIndexedAccess"

✅ Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

When iterating through a Record type with a for...in loop, detect that the record's key is guaranteed to be present when doing lookups on the object. If the Record does not have null or undefined keys, this will allow code to not have to handle the possibility of an undefined value when noUncheckedIndexedAccess is enabled.

⏯ Playground Link

Playground link

📃 Motivating Example

Iterating through a Record with non-null/undefined values with noUncheckedIndexedAccess enabled will no longer force handling of undefined

💻 Use Cases

Without this change, I have to either leave noUncheckedIndexAccess disabled, or add unnecessary boilerplate whenever I'm iterating through a Record.

Activity

  1. ItsHarper commented on Apr 4, 2024

    @ItsHarper
    Harper Andrews (ItsHarper)
    on Apr 4, 2024
    Author
    More actions

    A similar case to this was reported as #43614, and resolved in #51653.

  2. Harper Andrews (ItsHarper)
    changed the title [-]Allow iterating through a Record with `noUncheckedIndexedAccess` enabled without having to handle `undefined`[/-] [+]`noUncheckedIndexedAccess` does not narrow properly when iterating with `for...in`[/+] on Apr 4, 2024
  3. jcalz commented on Apr 4, 2024

    @jcalz
    Joe Calzaretta (jcalz)
    on Apr 4, 2024
    Contributor
    More actions

    Could you edit to talk about index signatures instead of Records? A Record<K, V> isn't required to have an index signature (e.g., Record<"a", string>).

  4. RyanCavanaugh commented on Apr 4, 2024

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Apr 4, 2024
    Member
    More actions

    You could still write

    const arr = [1, 2, 3, 4, 5];
    for (const idx in arr) {
        arr.length = 0; // n.b. might be an indirect side effect of other code!
        const m = arr[idx];
    }

    Which would give you a wrong type at m

    We don't intend to make noUncheckedIndexedAccess any smarter than it already is - nearly any attempt to make it smarter induces soundness holes in one case or another, and the point of the flag was to not have any indexing holes.

  5. Ryan Cavanaugh (RyanCavanaugh)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript vision
    on Apr 4, 2024
  6. ItsHarper commented on Apr 4, 2024

    @ItsHarper
    Harper Andrews (ItsHarper)
    on Apr 4, 2024
    Author
    More actions

    Gotcha, that's fair. Thanks for responding!

  7. Harper Andrews (ItsHarper)
    closed this as not plannedon Apr 4, 2024
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

    DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.