Skip to content

Commit 1f6e0eb

Browse files
mrkishiConduitry
authored andcommitted
improve derived store typing for users
1 parent 6d2d025 commit 1f6e0eb

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/runtime/store/index.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,30 @@ type StoresValues<T> = T extends Readable<infer U> ? U :
122122
/**
123123
* Derived value store by synchronizing one or more readable stores and
124124
* applying an aggregation function over its input values.
125-
* @param {Stores} stores input stores
126-
* @param {function(Stores=, function(*)=):*}fn function callback that aggregates the values
127-
* @param {*=}initial_value when used asynchronously
125+
*
126+
* @param stores - input stores
127+
* @param fn - function callback that aggregates the values
128128
*/
129-
export function derived<T, S extends Stores>(
129+
export function derived<S extends Stores, T>(
130130
stores: S,
131-
fn: (values: StoresValues<S>, set: Subscriber<T>) => T | Unsubscriber | void,
132-
initial_value?: T,
133-
): Readable<T> {
131+
fn: (values: StoresValues<S>) => T
132+
): Readable<T>;
134133

134+
/**
135+
* Derived value store by synchronizing one or more readable stores and
136+
* applying an aggregation function over its input values.
137+
*
138+
* @param stores - input stores
139+
* @param fn - function callback that aggregates the values
140+
* @param initial_value - when used asynchronously
141+
*/
142+
export function derived<S extends Stores, T>(
143+
stores: S,
144+
fn: (values: StoresValues<S>, set: (value: T) => void) => Unsubscriber | void,
145+
initial_value?: T
146+
): Readable<T>;
147+
148+
export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Readable<T> {
135149
const single = !Array.isArray(stores);
136150
const stores_array: Array<Readable<any>> = single
137151
? [stores as Readable<any>]
@@ -141,7 +155,7 @@ export function derived<T, S extends Stores>(
141155

142156
return readable(initial_value, (set) => {
143157
let inited = false;
144-
const values: StoresValues<S> = [] as StoresValues<S>;
158+
const values = [];
145159

146160
let pending = 0;
147161
let cleanup = noop;

0 commit comments

Comments
 (0)