diff --git a/.changeset/young-beds-beam.md b/.changeset/young-beds-beam.md new file mode 100644 index 000000000000..4349d82dd9f2 --- /dev/null +++ b/.changeset/young-beds-beam.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: correctly increment/decrement bigints diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 8931d02009a6..c2188b187cfe 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -984,23 +984,32 @@ function get_parent_context(component_context) { } /** - * @param {Value} signal + * @template {number | bigint} T + * @param {Value} signal * @param {1 | -1} [d] - * @returns {number} + * @returns {T} */ export function update(signal, d = 1) { - var value = +get(signal); - set(signal, value + d); - return value; + var value = get(signal); + var result = d === 1 ? value++ : value--; + + set(signal, value); + + // @ts-expect-error + return result; } /** - * @param {Value} signal + * @template {number | bigint} T + * @param {Value} signal * @param {1 | -1} [d] - * @returns {number} + * @returns {T} */ export function update_pre(signal, d = 1) { - return set(signal, +get(signal) + d); + var value = get(signal); + + // @ts-expect-error + return set(signal, d === 1 ? ++value : --value); } /** diff --git a/packages/svelte/tests/signals/test.ts b/packages/svelte/tests/signals/test.ts index db061bfb5543..6796655cc8d2 100644 --- a/packages/svelte/tests/signals/test.ts +++ b/packages/svelte/tests/signals/test.ts @@ -765,4 +765,20 @@ describe('signals', () => { assert.deepEqual(a.deriveds, null); }; }); + + test('bigint states update correctly', () => { + return () => { + const count = state(0n); + + assert.doesNotThrow(() => $.update(count)); + assert.equal($.get(count), 1n); + assert.doesNotThrow(() => $.update(count, -1)); + assert.equal($.get(count), 0n); + + assert.doesNotThrow(() => $.update_pre(count)); + assert.equal($.get(count), 1n); + assert.doesNotThrow(() => $.update_pre(count, -1)); + assert.equal($.get(count), 0n); + }; + }); });