Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
6 tasks done
NoahAndrews opened this issue Apr 4, 2024 · 4 comments
Closed
6 tasks done
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@NoahAndrews
Copy link

NoahAndrews commented Apr 4, 2024

πŸ” Search Terms

"noUncheckedIndexedAccess"

βœ… Viability Checklist

⭐ 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.

@NoahAndrews
Copy link
Author

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

@NoahAndrews NoahAndrews 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 Apr 4, 2024
@jcalz
Copy link
Contributor

jcalz commented Apr 4, 2024

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>).

@RyanCavanaugh
Copy link
Member

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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Apr 4, 2024
@NoahAndrews
Copy link
Author

Gotcha, that's fair. Thanks for responding!

@NoahAndrews NoahAndrews closed this as not planned Won't fix, can't repro, duplicate, stale Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants