From 776ac3c1762da5f8147c457a997a417cfae67e4c Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 19 Jan 2024 17:54:40 +0100 Subject: [PATCH] fix: add back `derived` type overload fixes #9866 --- .changeset/famous-falcons-melt.md | 5 +++++ packages/svelte/src/store/index.js | 30 ++++++++++++++++++++++++++-- packages/svelte/tests/types/store.ts | 13 ++++++++++++ packages/svelte/types/index.d.ts | 21 +++++++++++++++++-- 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 .changeset/famous-falcons-melt.md create mode 100644 packages/svelte/tests/types/store.ts diff --git a/.changeset/famous-falcons-melt.md b/.changeset/famous-falcons-melt.md new file mode 100644 index 0000000000..e891fdfbcd --- /dev/null +++ b/.changeset/famous-falcons-melt.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: add back `derived` type overload diff --git a/packages/svelte/src/store/index.js b/packages/svelte/src/store/index.js index e18f7df8b2..728eec3d49 100644 --- a/packages/svelte/src/store/index.js +++ b/packages/svelte/src/store/index.js @@ -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) => T} fn + * @param {T} [initial_value] + * @returns {import('./public.js').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 + * @template {import('./private.js').Stores} S + * @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 {T} [initial_value] + * @returns {import('./public.js').Readable} + */ +/** + * @template {import('./private.js').Stores} S * @template T * @param {S} stores * @param {Function} fn * @param {T} [initial_value] - * @returns {import('./public').Readable} + * @returns {import('./public.js').Readable} */ export function derived(stores, fn, initial_value) { const single = !Array.isArray(stores); diff --git a/packages/svelte/tests/types/store.ts b/packages/svelte/tests/types/store.ts new file mode 100644 index 0000000000..a6e681898c --- /dev/null +++ b/packages/svelte/tests/types/store.ts @@ -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; +}); diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index 4526ff1e46..a70f6e7992 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -1983,6 +1983,11 @@ declare module 'svelte/store' { | Readable | [Readable, ...Array>] | Array>; + + /** One or more values from `Readable` stores. */ + type StoresValues = T extends Readable + ? U + : { [K in keyof T]: T[K] extends Readable ? U : never }; /** * Creates a `Readable` store that allows reading by subscription. * @@ -1999,8 +2004,20 @@ declare module 'svelte/store' { * @param value initial value * */ export function writable(value?: T | undefined, start?: StartStopNotifier | undefined): Writable; - - export function derived(stores: S, fn: Function, 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) => T, 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; /** * Takes a store and returns a new one derived from the old one that is readable. *