Skip to content

refactor(collection): ! overrides for unchecked iterator access #10260

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

Conversation

Renegade334
Copy link
Contributor

@Renegade334 Renegade334 commented May 7, 2024

Please describe the changes this PR makes and why it should be merged:

Currently, for backward compatibility reasons, when manually traversing builtin iterators, the type of Iterator<T>.next().value resolves to T | any, or in other words, just any.

Under changes to iterators in TypeScript 5.6, the resolved value type will change from any to T | undefined in strict mode.

There are a few instances in collection.ts where a result value no longer being any will cause issues. A test build shows the following errors:

src/collection.ts:90:54 - error TS2322: Type 'Value | undefined' is not assignable to type 'Value'.
  'Value' could be instantiated with an arbitrary type which could be unrelated to 'Value | undefined'.

90   return Array.from({ length: amount }, (): Value => iter.next().value);
                                                        ~~~~~~~~~~~~~~~~~

src/collection.ts:107:52 - error TS2322: Type 'Key | undefined' is not assignable to type 'Key'.
  'Key' could be instantiated with an arbitrary type which could be unrelated to 'Key | undefined'.

107   return Array.from({ length: amount }, (): Key => iter.next().value);
                                                       ~~~~~~~~~~~~~~~~~

src/collection.ts:515:10 - error TS2488: Type '[Key, Value] | undefined' must have a '[Symbol.iterator]()' method that returns an iterator.

515    const [key, value] = iter.next().value;
             ~~~~~~~~~~~~

src/collection.ts:629:4 - error TS2322: Type 'Value' is not assignable to type 'InitialValue'.
  'InitialValue' could be instantiated with an arbitrary type which could be unrelated to 'Value'.

629    accumulator = iterator.next().value[1];
       ~~~~~~~~~~~

  src/collection.ts:27:34
    27 export interface Collection<Key, Value> extends Map<Key, Value> {
                                        ~~~~~
    This type parameter might need an `extends InitialValue` constraint.

src/collection.ts:629:18 - error TS2532: Object is possibly 'undefined'.

629    accumulator = iterator.next().value[1];
                     ~~~~~~~~~~~~~~~~~~~~~


Found 5 errors in the same file, starting at: src/collection.ts:90

All of these errors occur in places where the length of the iterator has already been checked, so we know that the iterator will never be "done" and will therefore always return a value of type T. It's therefore safe to use ! in these instances. Additionally, under reduce(), if there's no initialValue provided then InitialValue = Value, so it's safe to force the accumulator type (in a similar vein to what already occurs in reduceRight()).

Status and versioning classification:

  • These are changes related to compilation only, and have no effects on the shape or behaviour of the API.
  • These changes are fully build-compatible with current versions of TypeScript.

The type of Iterator<T>.next().value is currently `any`,
but will soon change to `T | undefined` if noUncheckedIndexedAccess
is set in tsconfig.
Copy link

vercel bot commented May 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
discord-js ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 19, 2024 10:47pm
discord-js-guide ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 19, 2024 10:47pm

@Renegade334 Renegade334 changed the title fix(collection): ! overrides for unchecked iterator access refactor(collection): ! overrides for unchecked iterator access May 17, 2024
Copy link

codecov bot commented May 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 50.52%. Comparing base (efa16a6) to head (89363f6).
Report is 34 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10260   +/-   ##
=======================================
  Coverage   50.52%   50.52%           
=======================================
  Files         228      228           
  Lines       20705    20705           
  Branches     1252     1252           
=======================================
  Hits        10462    10462           
  Misses      10199    10199           
  Partials       44       44           
Flag Coverage Δ
collection 100.00% <100.00%> (ø)
proxy 78.52% <ø> (ø)
rest 92.68% <ø> (ø)
ws 51.99% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Renegade334
Copy link
Contributor Author

Merged as #10394.

@Renegade334 Renegade334 deleted the collection-unchecked-iterator-access branch January 30, 2025 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

1 participant