Open shadow-identity opened 5 months ago
We can't change this in 4.x because it would be a breaking change strictly speaking. The culprit is the type definition looking like this:
export function writable<T>(value?: T | undefined, start?: StartStopNotifier<T> | undefined): Writable<T>;
which means that because of the optional, the undefined
is swallowed from the generic.
For it to be preserved it seems we need to do something like this.
Looks like a fix for this isn't planned for the svelte5 either?
There are even worse examples of the pitfall, one with typescript:
const myStore = writable<string>()
const myString: string = $myStore
And another using jsdoc:
/** @type {SvelteStore<string>} */
const myStore = writable()
/** @type {string} */
const myString = $myStore
It does not produce any typing error, although myString is undefined
.
It happens on svelte5 as well.
I expect type error when I try to initialize a typed store with an empty arguments list.
Describe the bug
If the content of a store is optional (e. g. is
Something | undefined
), the derived type of the store does not keep it. As a result, there is an exception in runtime, instead of error on compile/linting time.Reproduction
myStore
in your editor. It isDate
. No error highlighted on$myStore.toString()
despite it is undefined.TypeError: can't access property "toString", (intermediate value)(...) is undefined
exception.Workaround:
Logs
No response
System Info
Severity
annoyance