Closed as not planned
Description
Describe the problem
When declaring a derived store that has null
as the initial value and has two or more stores as dependencies, I can't specify
the type that the derived store contains without first having so specify the type of the dependency array, which is somewhat frustrating.
// ideal situation (does not currently work)
// the type of the the store's value is `DerivedData | null`
// and the type of the dependecies is inferred
const myData = derived<DerivedData | null>([a, b, c], ([$a, $b, $c], set) => {
if(!$a || !$b || !$c) {
set(null);
return;
}
const { data, dispose } = generateData($a, $b, $c);
set(data);
return dispose;
});
// reality
// one way to work around the problem
// manually typing the `set` parameter will yield the same result, but it's more verbose
const myData = derived([a, b, c], ([$a, $b, $c], set: (value: DerivedData | null) => void) => {
if(!$a || !$b || !$c) {
set(null);
return;
}
const { data, dispose } = generateData($a, $b, $c);
set(data);
return dispose;
});
// another workaround
// like before, it yields the same result, but it's also more verbose than it should be
const deps = [a, b, c] as const
const myData = derived<typeof deps, DerivedData | null>(deps, ([$a, $b, $c], set) => {
if(!$a || !$b || !$c) {
set(null);
return;
}
const { data, dispose } = generateData($a, $b, $c);
set(data);
return dispose;
});
Describe the proposed solution
Swapping the order of the generic parameters of the derived
function would solve the problem.
svelte/packages/svelte/src/runtime/store/index.js
Lines 124 to 132 in f4c4d99
Alternatives considered
The alternatives were listed above.
Importance
would make my life easier