fix: adjust order of `derived` function definition overloads (#11426)

Turns out the order is crucial for not getting a type error
fixes #11415
pull/11440/head
Simon H 8 months ago committed by GitHub
parent 9c5a9d8e20
commit 9c1a5063b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: adjust order of `derived` function definition overloads

@ -111,7 +111,7 @@ export function writable(value, start = noop) {
* @template T * @template T
* @overload * @overload
* @param {S} stores * @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] * @param {T} [initial_value]
* @returns {import('./public.js').Readable<T>} * @returns {import('./public.js').Readable<T>}
*/ */
@ -124,7 +124,7 @@ export function writable(value, start = noop) {
* @template T * @template T
* @overload * @overload
* @param {S} stores * @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] * @param {T} [initial_value]
* @returns {import('./public.js').Readable<T>} * @returns {import('./public.js').Readable<T>}
*/ */

@ -272,7 +272,6 @@ describe('derived', () => {
const number = writable(1); const number = writable(1);
const evens = derived( const evens = derived(
number, number,
// @ts-expect-error TODO feels like inference should work here
(n, set) => { (n, set) => {
if (n % 2 === 0) set(n); if (n % 2 === 0) set(n);
}, },
@ -303,10 +302,8 @@ describe('derived', () => {
const number = writable(1); const number = writable(1);
const evensAndSquaresOf4 = derived( const evensAndSquaresOf4 = derived(
number, number,
// @ts-expect-error TODO feels like inference should work here
(n, set, update) => { (n, set, update) => {
if (n % 2 === 0) set(n); if (n % 2 === 0) set(n);
// @ts-expect-error TODO feels like inference should work here
if (n % 4 === 0) update((n) => n * n); if (n % 4 === 0) update((n) => n * n);
}, },
0 0
@ -442,7 +439,6 @@ describe('derived', () => {
const values: number[] = []; const values: number[] = [];
const cleaned_up: number[] = []; const cleaned_up: number[] = [];
// @ts-expect-error TODO feels like inference should work here
const d = derived(num, ($num, set) => { const d = derived(num, ($num, set) => {
set($num * 2); set($num * 2);
@ -516,7 +512,6 @@ describe('derived', () => {
const a = writable(true); const a = writable(true);
let b_started = false; let b_started = false;
// @ts-expect-error TODO feels like inference should work here
const b = derived(a, (_, __) => { const b = derived(a, (_, __) => {
b_started = true; b_started = true;
return () => { return () => {
@ -525,7 +520,6 @@ describe('derived', () => {
}; };
}); });
// @ts-expect-error TODO feels like inference should work here
const c = derived(a, ($a, set) => { const c = derived(a, ($a, set) => {
if ($a) return b.subscribe(set); if ($a) return b.subscribe(set);
}); });

@ -11,3 +11,17 @@ derived([a], ([aVal]) => {
aVal === ''; aVal === '';
return aVal === true; return aVal === true;
}); });
derived(
a,
(value, set) => {
set('works');
// @ts-expect-error
set(true);
value === true;
// @ts-expect-error
value === '';
},
''
);

@ -2157,14 +2157,14 @@ declare module 'svelte/store' {
* *
* https://svelte.dev/docs/svelte-store#derived * 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 * Derived value store by synchronizing one or more readable stores and
* applying an aggregation function over its input values. * applying an aggregation function over its input values.
* *
* https://svelte.dev/docs/svelte-store#derived * 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. * Takes a store and returns a new one derived from the old one that is readable.
* *

Loading…
Cancel
Save