fix: add back `derived` type overload

fixes #9866
pull/10237/head
Simon Holthausen 11 months ago
parent 35b500c399
commit 776ac3c176

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: add back `derived` type overload

@ -123,12 +123,38 @@ function run_all(fns) {
} }
/** /**
* @template {import('./private').Stores} S * 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
* @template {import('./private.js').Stores} S
* @template T
* @overload
* @param {S} stores
* @param {(values: import('./private.js').StoresValues<S>) => T} fn
* @param {T} [initial_value]
* @returns {import('./public.js').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
* @template {import('./private.js').Stores} S
* @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 {T} [initial_value]
* @returns {import('./public.js').Readable<T>}
*/
/**
* @template {import('./private.js').Stores} S
* @template T * @template T
* @param {S} stores * @param {S} stores
* @param {Function} fn * @param {Function} fn
* @param {T} [initial_value] * @param {T} [initial_value]
* @returns {import('./public').Readable<T>} * @returns {import('./public.js').Readable<T>}
*/ */
export function derived(stores, fn, initial_value) { export function derived(stores, fn, initial_value) {
const single = !Array.isArray(stores); const single = !Array.isArray(stores);

@ -0,0 +1,13 @@
import { derived, writable } from 'svelte/store';
const a = writable(false);
derived(a, (aVal) => {
// @ts-expect-error
aVal === '';
return aVal === true;
});
derived([a], ([aVal]) => {
// @ts-expect-error
aVal === '';
return aVal === true;
});

@ -1983,6 +1983,11 @@ declare module 'svelte/store' {
| Readable<any> | Readable<any>
| [Readable<any>, ...Array<Readable<any>>] | [Readable<any>, ...Array<Readable<any>>]
| Array<Readable<any>>; | Array<Readable<any>>;
/** One or more values from `Readable` stores. */
type StoresValues<T> = T extends Readable<infer U>
? U
: { [K in keyof T]: T[K] extends Readable<infer U> ? U : never };
/** /**
* Creates a `Readable` store that allows reading by subscription. * Creates a `Readable` store that allows reading by subscription.
* *
@ -1999,8 +2004,20 @@ declare module 'svelte/store' {
* @param value initial value * @param value initial value
* */ * */
export function writable<T>(value?: T | undefined, start?: StartStopNotifier<T> | undefined): Writable<T>; export function writable<T>(value?: T | undefined, start?: StartStopNotifier<T> | undefined): Writable<T>;
/**
export function derived<S extends Stores, T>(stores: S, fn: Function, 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>) => T, 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>;
/** /**
* 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