Skip to content

Commit 776ac3c

Browse files
committed
fix: add back derived type overload
fixes #9866
1 parent 35b500c commit 776ac3c

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

.changeset/famous-falcons-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: add back `derived` type overload

packages/svelte/src/store/index.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,38 @@ function run_all(fns) {
123123
}
124124

125125
/**
126-
* @template {import('./private').Stores} S
126+
* Derived value store by synchronizing one or more readable stores and
127+
* applying an aggregation function over its input values.
128+
*
129+
* https://svelte.dev/docs/svelte-store#derived
130+
* @template {import('./private.js').Stores} S
131+
* @template T
132+
* @overload
133+
* @param {S} stores
134+
* @param {(values: import('./private.js').StoresValues<S>) => T} fn
135+
* @param {T} [initial_value]
136+
* @returns {import('./public.js').Readable<T>}
137+
*/
138+
/**
139+
* Derived value store by synchronizing one or more readable stores and
140+
* applying an aggregation function over its input values.
141+
*
142+
* https://svelte.dev/docs/svelte-store#derived
143+
* @template {import('./private.js').Stores} S
144+
* @template T
145+
* @overload
146+
* @param {S} stores
147+
* @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
148+
* @param {T} [initial_value]
149+
* @returns {import('./public.js').Readable<T>}
150+
*/
151+
/**
152+
* @template {import('./private.js').Stores} S
127153
* @template T
128154
* @param {S} stores
129155
* @param {Function} fn
130156
* @param {T} [initial_value]
131-
* @returns {import('./public').Readable<T>}
157+
* @returns {import('./public.js').Readable<T>}
132158
*/
133159
export function derived(stores, fn, initial_value) {
134160
const single = !Array.isArray(stores);

packages/svelte/tests/types/store.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { derived, writable } from 'svelte/store';
2+
3+
const a = writable(false);
4+
derived(a, (aVal) => {
5+
// @ts-expect-error
6+
aVal === '';
7+
return aVal === true;
8+
});
9+
derived([a], ([aVal]) => {
10+
// @ts-expect-error
11+
aVal === '';
12+
return aVal === true;
13+
});

packages/svelte/types/index.d.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,11 @@ declare module 'svelte/store' {
19831983
| Readable<any>
19841984
| [Readable<any>, ...Array<Readable<any>>]
19851985
| Array<Readable<any>>;
1986+
1987+
/** One or more values from `Readable` stores. */
1988+
type StoresValues<T> = T extends Readable<infer U>
1989+
? U
1990+
: { [K in keyof T]: T[K] extends Readable<infer U> ? U : never };
19861991
/**
19871992
* Creates a `Readable` store that allows reading by subscription.
19881993
*
@@ -1999,8 +2004,20 @@ declare module 'svelte/store' {
19992004
* @param value initial value
20002005
* */
20012006
export function writable<T>(value?: T | undefined, start?: StartStopNotifier<T> | undefined): Writable<T>;
2002-
2003-
export function derived<S extends Stores, T>(stores: S, fn: Function, initial_value?: T | undefined): Readable<T>;
2007+
/**
2008+
* Derived value store by synchronizing one or more readable stores and
2009+
* applying an aggregation function over its input values.
2010+
*
2011+
* https://svelte.dev/docs/svelte-store#derived
2012+
* */
2013+
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>) => T, initial_value?: T | undefined): Readable<T>;
2014+
/**
2015+
* Derived value store by synchronizing one or more readable stores and
2016+
* applying an aggregation function over its input values.
2017+
*
2018+
* https://svelte.dev/docs/svelte-store#derived
2019+
* */
2020+
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>;
20042021
/**
20052022
* Takes a store and returns a new one derived from the old one that is readable.
20062023
*

0 commit comments

Comments
 (0)