Repository navigation
Type typed-array copyWithin as returning the typed array, and map as keeping the element type - #9112
Conversation
…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 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP
Signed-off-by: Christoph Knittel <[email protected]>
Changelog💥 Breaking Change
🐛 Bug fix
Semantic Diff1 commit · Full page Type typed-array copyWithin as returning the typed array, and map as keeping the element type489f49e · cknitt + Claude · Oct 28, 2026, 15:26 UTC
Typing modified (TypedArray and per-module externals)
-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
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9112 |
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Typed arrays'
copyWithinandcopyAllWithinreturn the typed array they mutate, but the stdlib typed them as returning anarray, soInt32Array.copyAllWithin(v, ~target=1)->Array.push(5)compiled and threwr.push is not a function. The genericTypedArrayexternals had this signature, and #9050 copied it into theImplfunctor that every typed-array module includes. Both now return the typed array (t<'a>inTypedArray,T.tin the modules), and the duplicated@send @sendonTypedArray.copyWithinis gone.TypedArray.mapandmapWithIndexwere typed(t<'a>, 'a => 'b) => t<'b>, but a typed array'smapcreates a typed array of the same kind and converts each result to its element type, so mapping at<int>to strings gave at<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 ownbreakingchangelog fragment; thecopyWithinfix is afixfragment.The docstrings now say that
copyWithin/copyAllWithinreturn the typed array and thatmapreturns a typed array of the same kind.Tests:
tests/tests/src/stdlib/Stdlib_TypedArrayTests.res:copyWithin/copyAllWithinonInt32Arrayand onTypedArrayreturn the very array they mutated, with the right type annotation, andTypedArray.map/mapWithIndexwith an element-type-preserving function.tests/build_tests/super_errors/fixtures/TypedArrayMapElementType.res:TypedArray.mapwith a function returningstringon at<int>is now a type error.CompletionTypedArrays.res.txtshows the new return types and docstrings.Breaking change: making
TypedArray.maptype-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