Skip to content

Array callbacks take the element's index too - #59

Merged
ASDAlexander77 merged 1 commit into
mainfrom
array-callback-index
Oct 29, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
array-callback-index

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Array callbacks now take the element's index. For example, xs.map((x, i) => x + i).

  • Before: it failed with "can't assign '(p0, p1) => …' to '(p0) => …'".
  • Changed: map, filter, filter2, find, findIndex, findLast, findLastIndex, forEach, some and every (and the Array<T> class's forwarding methods) now take (v: T, i: number), as JavaScript passes them.
  • Unchanged for callers: a callback that takes only the element still works, through the compiler's fewer-parameters wrapper.
  • Not added: the third JavaScript argument, the array itself.

Merge after ASDAlexander77/TypeScriptCompiler#738. Without its fixes, a capturing callback for a generic method infers the wrong T, and own rejects a string returned through the wrapper.

Test: tests/array_callback_index.ts. Validation, with #738's compiler, release JIT: gc 182/182, rc and none 181 (1 skipped), own 171 (11 skipped), plus the declarations check.

🤖 Generated with Claude Code

`xs.map((x, i) => x + i)` was "can't assign '(p0, p1) => ...' to
'(p0) => ...'": every array callback took the element only. map, filter,
filter2, find, findIndex, findLast, findLastIndex, forEach, some and
every (and the Array<T> class's forwards) now take `(v: T, i: number)`,
as JavaScript gives them; a callback taking the element only is given
through the compiler's fewer-parameters thunk.

Needs TypeScriptCompiler#738 (inference and own fixes for that thunk).

Test: tests/array_callback_index.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit ff233f2 into main Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant