SolidJSS
SolidJSโ€ข2y agoโ€ข
7 replies
Wes

How to better handle `from`'s possible `undefined` state?

I'm using from to observe a value from an external source. Because that's an observed value, it will be undefined at start.

Further in my code I am using the value, for instance on a <Match>:

const volume = from<number>(bridge.observeVolume);

...

<Match when={volume() > 0}>


The problem here is that volume() is possibly undefined, which makes TypeScript mad because comparing undefined with number is a sin.

Did I choose the wrong pattern nere? Do I need to further wrap the volume accessor in a memo to guarantee an initial value?

Shouldn't it be possible to set the initial value as such?
const volume = from<number>(bridge.observeVolume, bridge.volume);

...

<Match when={volume() > 0}>
Was this page helpful?