discord.js
discord.js copied to clipboard
refactor(collection): `!` overrides for unchecked iterator access
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 noinitialValue
provided thenInitialValue = Value
, so it's safe to force the accumulator type (in a similar vein to what already occurs inreduceRight()
).
- 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
- Attempting to return a value of
T | void
from a function that returnsT | undefined
. Although the void value is by definitionundefined
, 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.