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

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

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 currently being planned for TypeScript 5.6, the resolved value type will change from any to T | void 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:86:29 - error TS2322: Type 'void | Value' is not assignable to type 'Value | Value[] | undefined'.
  Type 'void' is not assignable to type 'Value | Value[] | undefined'.

86   if (amount === undefined) return this.values().next().value;
                               ~~~~~~

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

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

src/collection.ts:103:29 - error TS2322: Type 'void | Key' is not assignable to type 'Key | Key[] | undefined'.
  Type 'void' is not assignable to type 'Key | Key[] | undefined'.

103   if (amount === undefined) return this.keys().next().value;
                                ~~~~~~

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

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

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

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

src/collection.ts:629:18 - error TS7053: Element implicitly has an 'any' type because expression of type '1' can't be used to index type 'void | [Key, Value]'.
  Property '1' does not exist on type 'void | [Key, Value]'.

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


Found 6 errors.

These errors fall into two categories:

  • Attempting to access or destructure a value of type T | void.
    • All of these errors occur in places where the length of the iterator has already been checked or derived, 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()).
  • Attempting to return a value of T | void from a function that returns T | undefined. Although the void value is by definition undefined, this is an assignability error unless coerced.

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 ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
discord-js ⬜️ Ignored (Inspect) Visit Preview May 23, 2024 10:39am
discord-js-guide ⬜️ Ignored (Inspect) Visit Preview May 23, 2024 10:39am

@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 47.10%. Comparing base (ba6476d) to head (02c7468).
Report is 68 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main   #10260       +/-   ##
===========================================
- Coverage   62.35%   47.10%   -15.26%     
===========================================
  Files         209       49      -160     
  Lines       16280     9910     -6370     
  Branches     1213      502      -711     
===========================================
- Hits        10152     4668     -5484     
+ Misses       6083     5238      -845     
+ Partials       45        4       -41     
Flag Coverage Δ
brokers 47.10% <100.00%> (-16.40%) ⬇️
builders 47.10% <100.00%> (-48.47%) ⬇️
collection 47.10% <100.00%> (-52.22%) ⬇️
discord.js 47.10% <100.00%> (?)
formatters 47.10% <100.00%> (-52.22%) ⬇️
guide 47.10% <100.00%> (+47.10%) ⬆️
next 47.10% <100.00%> (∅)
proxy 47.10% <100.00%> (-27.90%) ⬇️
rest 47.10% <100.00%> (-45.49%) ⬇️
util 47.10% <100.00%> (-21.77%) ⬇️
utilities 47.10% <100.00%> (-52.90%) ⬇️
voice 47.10% <100.00%> (-16.55%) ⬇️
website 47.10% <100.00%> (+47.10%) ⬆️
ws 47.10% <100.00%> (-5.43%) ⬇️

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 Renegade334 force-pushed the collection-unchecked-iterator-access branch from cb32ad5 to 02c7468 Compare May 23, 2024 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

None yet

1 participant