Skip to content

Type typed-array copyWithin as returning the typed array, and map as keeping the element type - #9112

Merged
cknitt merged 1 commit into
masterfrom
fix-9103-typedarray-copywithin-map
Oct 28, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-9103-typedarray-copywithin-map

Conversation

@cknitt

@cknitt cknitt commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Typed arrays' copyWithin and copyAllWithin return the typed array they mutate, but the stdlib typed them as returning an array, so Int32Array.copyAllWithin(v, ~target=1)->Array.push(5) compiled and threw r.push is not a function. The generic TypedArray externals had this signature, and #9050 copied it into the Impl functor that every typed-array module includes. Both now return the typed array (t<'a> in TypedArray, T.t in the modules), and the duplicated @send @send on TypedArray.copyWithin is gone.

TypedArray.map and mapWithIndex were typed (t<'a>, 'a => 'b) => t<'b>, but a typed array's map creates a typed array of the same kind and converts each result to its element type, so mapping a t<int> to strings gave a t<string> holding zeros. They are now (t<'a>, 'a => 'a) => t<'a>, like the per-module versions already were. This is a breaking change for code that used them to change the element type, so it has its own breaking changelog fragment; the copyWithin fix is a fix fragment.

The docstrings now say that copyWithin/copyAllWithin return the typed array and that map returns a typed array of the same kind.

Tests:

  • tests/tests/src/stdlib/Stdlib_TypedArrayTests.res: copyWithin/copyAllWithin on Int32Array and on TypedArray return the very array they mutated, with the right type annotation, and TypedArray.map/mapWithIndex with an element-type-preserving function.
  • tests/build_tests/super_errors/fixtures/TypedArrayMapElementType.res: TypedArray.map with a function returning string on a t<int> is now a type error.
  • The completion snapshot CompletionTypedArrays.res.txt shows the new return types and docstrings.

Breaking change: making TypedArray.map type-preserving breaks code that used it to change the element type (which only worked by accident, since the values are converted). This ships as a breaking change in v13, without a deprecation period.

Fixes #9103

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP

…keeping the element type

Why:
    `copyWithin` and `copyAllWithin` return the typed array they mutated,
    but were typed as returning an `array`, so `Array.push` on the result
    compiled and threw. A typed array's `map` converts each result to its
    own element type, but `TypedArray.map` let `f` change the type, so a
    `TypedArray.t<string>` held numbers.

Typing modified (TypedArray and per-module externals):
Before (rescript):
    external copyAllWithin: (t<'a>, ~target: int) => array<'a>
    external map: (t<'a>, 'a => 'b) => t<'b>
    external mapWithIndex: (t<'a>, ('a, int) => 'b) => t<'b>
    // Int32Array and the other modules
    external copyAllWithin: (t, ~target: int) => array<int>
After (rescript):
    external copyAllWithin: (t<'a>, ~target: int) => t<'a>
    external map: (t<'a>, 'a => 'a) => t<'a>
    external mapWithIndex: (t<'a>, ('a, int) => 'a) => t<'a>
    // Int32Array and the other modules
    external copyAllWithin: (t, ~target: int) => t

`copyWithin` changes like `copyAllWithin`, and loses a duplicated `@send`.

Breaking: `TypedArray.map` and `mapWithIndex` no longer change the element
type; to get values of another type, build an `array` from the elements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

Copy link
Copy Markdown

Changelog

💥 Breaking Change

  • TypedArray.map and TypedArray.mapWithIndex now keep the element type, because a typed array converts the results of map to its own element type.

🐛 Bug fix

  • Type copyWithin and copyAllWithin of typed arrays as returning the typed array instead of an array.

Semantic Diff

1 commit · Full page

Type typed-array copyWithin as returning the typed array, and map as keeping the element type

489f49e · cknitt + Claude · Oct 28, 2026, 15:26 UTC

Why

copyWithin and copyAllWithin return the typed array they mutated, but were typed as returning an array, so Array.push on the result compiled and threw. A typed array's map converts each result to its own element type, but TypedArray.map let f change the type, so a TypedArray.t<string> held numbers.

Typing modified (TypedArray and per-module externals)

rescript

-external copyAllWithin: (t<'a>, ~target: int) => array<'a>
-external map: (t<'a>, 'a => 'b) => t<'b>
-external mapWithIndex: (t<'a>, ('a, int) => 'b) => t<'b>
+external copyAllWithin: (t<'a>, ~target: int) => t<'a>
+external map: (t<'a>, 'a => 'a) => t<'a>
+external mapWithIndex: (t<'a>, ('a, int) => 'a) => t<'a>
 // Int32Array and the other modules
-external copyAllWithin: (t, ~target: int) => array<int>
+external copyAllWithin: (t, ~target: int) => t

copyWithin changes like copyAllWithin, and loses a duplicated @send.

⚠️ Breaking: TypedArray.map and mapWithIndex no longer change the element type; to get values of another type, build an array from the elements.

@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9112

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9112

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9112

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9112

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9112

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9112

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9112

commit: 489f49e

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T17:49:14.216212Z 489f49e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cknitt

cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 489f49e09a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt
cknitt marked this pull request as ready for review October 28, 2026 17:55
@cknitt
cknitt requested a review from fhammerschmidt October 28, 2026 17:56
@cknitt
cknitt enabled auto-merge (squash) October 28, 2026 17:56
@cknitt
cknitt merged commit e3a2ae1 into master Oct 28, 2026
26 checks passed
@cknitt
cknitt deleted the fix-9103-typedarray-copywithin-map branch October 28, 2026 18:01
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.

TypedArray: copyWithin/copyAllWithin are typed as returning array, and TypedArray.map can change the element type

2 participants