diff --git a/.changeset/giant-bananas-turn.md b/.changeset/giant-bananas-turn.md new file mode 100644 index 0000000000..df4f40e5a8 --- /dev/null +++ b/.changeset/giant-bananas-turn.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: adjust order of `derived` function definition overloads diff --git a/packages/svelte/src/store/index.js b/packages/svelte/src/store/index.js index e6a2f0a6d3..108af184b0 100644 --- a/packages/svelte/src/store/index.js +++ b/packages/svelte/src/store/index.js @@ -111,7 +111,7 @@ export function writable(value, start = noop) { * @template T * @overload * @param {S} stores - * @param {(values: import('./private.js').StoresValues) => T} fn + * @param {(values: import('./private.js').StoresValues, set: (value: T) => void, update: (fn: import('./public.js').Updater) => void) => import('./public.js').Unsubscriber | void} fn * @param {T} [initial_value] * @returns {import('./public.js').Readable} */ @@ -124,7 +124,7 @@ export function writable(value, start = noop) { * @template T * @overload * @param {S} stores - * @param {(values: import('./private.js').StoresValues, set: (value: T) => void, update: (fn: import('./public.js').Updater) => void) => import('./public.js').Unsubscriber | void} fn + * @param {(values: import('./private.js').StoresValues) => T} fn * @param {T} [initial_value] * @returns {import('./public.js').Readable} */ diff --git a/packages/svelte/tests/store/test.ts b/packages/svelte/tests/store/test.ts index 9664d35701..6a89e39df8 100644 --- a/packages/svelte/tests/store/test.ts +++ b/packages/svelte/tests/store/test.ts @@ -272,7 +272,6 @@ describe('derived', () => { const number = writable(1); const evens = derived( number, - // @ts-expect-error TODO feels like inference should work here (n, set) => { if (n % 2 === 0) set(n); }, @@ -303,10 +302,8 @@ describe('derived', () => { const number = writable(1); const evensAndSquaresOf4 = derived( number, - // @ts-expect-error TODO feels like inference should work here (n, set, update) => { if (n % 2 === 0) set(n); - // @ts-expect-error TODO feels like inference should work here if (n % 4 === 0) update((n) => n * n); }, 0 @@ -442,7 +439,6 @@ describe('derived', () => { const values: number[] = []; const cleaned_up: number[] = []; - // @ts-expect-error TODO feels like inference should work here const d = derived(num, ($num, set) => { set($num * 2); @@ -516,7 +512,6 @@ describe('derived', () => { const a = writable(true); let b_started = false; - // @ts-expect-error TODO feels like inference should work here const b = derived(a, (_, __) => { b_started = true; return () => { @@ -525,7 +520,6 @@ describe('derived', () => { }; }); - // @ts-expect-error TODO feels like inference should work here const c = derived(a, ($a, set) => { if ($a) return b.subscribe(set); }); diff --git a/packages/svelte/tests/types/store.ts b/packages/svelte/tests/types/store.ts index a6e681898c..af1bb65ea9 100644 --- a/packages/svelte/tests/types/store.ts +++ b/packages/svelte/tests/types/store.ts @@ -11,3 +11,17 @@ derived([a], ([aVal]) => { aVal === ''; return aVal === true; }); + +derived( + a, + (value, set) => { + set('works'); + // @ts-expect-error + set(true); + + value === true; + // @ts-expect-error + value === ''; + }, + '' +); diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index daff299768..3a26e79e91 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -2157,14 +2157,14 @@ declare module 'svelte/store' { * * https://svelte.dev/docs/svelte-store#derived * */ - export function derived(stores: S, fn: (values: StoresValues) => T, initial_value?: T | undefined): Readable; + export function derived(stores: S, fn: (values: StoresValues, set: (value: T) => void, update: (fn: Updater) => void) => Unsubscriber | void, initial_value?: T | undefined): Readable; /** * Derived value store by synchronizing one or more readable stores and * applying an aggregation function over its input values. * * https://svelte.dev/docs/svelte-store#derived * */ - export function derived(stores: S, fn: (values: StoresValues, set: (value: T) => void, update: (fn: Updater) => void) => Unsubscriber | void, initial_value?: T | undefined): Readable; + export function derived(stores: S, fn: (values: StoresValues) => T, initial_value?: T | undefined): Readable; /** * Takes a store and returns a new one derived from the old one that is readable. *