Skip to content

Commit

Permalink
fix: adjust order of derived function definition overloads
Browse files Browse the repository at this point in the history
Turns out the order is crucial for not getting a type error
fixes #11415
  • Loading branch information
dummdidumm committed May 2, 2024
1 parent fcdad4c commit 44f23d2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-bananas-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: adjust order of `derived` function definition overloads
4 changes: 2 additions & 2 deletions packages/svelte/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function writable(value, start = noop) {
* @template T
* @overload
* @param {S} stores
* @param {(values: import('./private.js').StoresValues<S>) => T} fn
* @param {(values: import('./private.js').StoresValues<S>, set: (value: T) => void, update: (fn: import('./public.js').Updater<T>) => void) => import('./public.js').Unsubscriber | void} fn
* @param {T} [initial_value]
* @returns {import('./public.js').Readable<T>}
*/
Expand All @@ -124,7 +124,7 @@ export function writable(value, start = noop) {
* @template T
* @overload
* @param {S} stores
* @param {(values: import('./private.js').StoresValues<S>, set: (value: T) => void, update: (fn: import('./public.js').Updater<T>) => void) => import('./public.js').Unsubscriber | void} fn
* @param {(values: import('./private.js').StoresValues<S>) => T} fn
* @param {T} [initial_value]
* @returns {import('./public.js').Readable<T>}
*/
Expand Down
14 changes: 14 additions & 0 deletions packages/svelte/tests/types/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '';
},
''
);
4 changes: 2 additions & 2 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2157,14 +2157,14 @@ declare module 'svelte/store' {
*
* https://svelte.dev/docs/svelte-store#derived
* */
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>) => T, initial_value?: T | undefined): Readable<T>;
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>, set: (value: T) => void, update: (fn: Updater<T>) => void) => Unsubscriber | void, initial_value?: T | undefined): Readable<T>;
/**
* 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<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>, set: (value: T) => void, update: (fn: Updater<T>) => void) => Unsubscriber | void, initial_value?: T | undefined): Readable<T>;
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>) => T, initial_value?: T | undefined): Readable<T>;
/**
* Takes a store and returns a new one derived from the old one that is readable.
*
Expand Down

0 comments on commit 44f23d2

Please sign in to comment.