svelte derived store returns Readable<Writable<boolean> | undefined> value

223 views Asked by At

How to do derived store right if value passed as readable store can be undefined? I have an object that contains isLoading value. Here the types example :

obj: Readable<{
isLoading: Writable<boolean>, 
loadData: () => Promise<void>,
....
}> | undefined

or

obj: {
isLoading: writable(false),
loadData: () => await fetch(),
}

When loadData() launches, isLoading is changing its value and i need to track state of this value.

so i have to make something like this (obj is in class):

const isLoading = derived(this.obj, (obj) => obj?.isLoading);

But it returns Readable<Writable<boolean> | undefined> and it is logical))) but i need Readable<boolean>

After some changes i got this

const isLoading = derived(this.obj, (obj, set) => active?.isLoading.subscribe(set))

turns out that it's working, but i am a bit confused because I don't really understand how it works and also TS returns Readable<unknown> . So, are there ways to get the value in a different way?

I know it can be really hard to understand the context without seeing all the code, so any answers and examples regarding derived store and undefined value will be welcome.

0

There are 0 answers