solid icon indicating copy to clipboard operation
solid copied to clipboard

observable `from` can be undefined

Open faassen opened this issue 3 years ago • 0 comments

Describe the bug

I've been using from for a bit and I was confused by the signal returned from it could be undefined; after all the types don't indicate that's a possibility. But looking at the source of from it becomes clear why that is so:

createSignal<T | undefined>(undefined, { equals: false }) as [Accessor<T>, Setter<T>];

The as seems to be incorrect: if the producer never calls the function passed with subscribe, the signal starts out as undefined. Unless there is something I don't understand something about Observable and there's a guarantee the subscribed function is always called upon creation, the signal can be undefined. That's what I see in my experiments with an observable in the wild (from Dexie's liveQuery).

So is the return type of from correct or should it really be Accessor<T | undefined>?

Alternatively the signature of from could be modified in some way to be able to provide a default value for the signal created, or possibly passing in a signal tuple and letting this be a concern of the caller.

Your Example Website or App

https://stackblitz.com/edit/solidjs-template-7ftca6?file=src/App.tsx

Steps to Reproduce the Bug or Issue

Run the stackblitz project above, click 'open in new window' (top-right) so you can open the console, and observe "undefined" appearing in the browser console.

Expected behavior

See bug description. I don't know what the proper behavior should be. The type of s in the example says its Accessor<string> but it behaves like Accessor<string | undefined>.

Screenshots or Videos

No response

Platform

Node (in my tests), browsers.

Additional context

No response

faassen avatar Jul 06 '22 16:07 faassen