You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Me, and probably a good few others, like to shorten single-statement callbacks into one line
()=>doDoStuffWith(myState)
However, this callback cant always be passed to $effect because of an incompatible return type
declarefunction$effect(fn: ()=>void|(()=>void)): void// ^ if the single expression resolves to a value,// it violates the `void` type
A more tangible example;
I have three boolean states, call them A, X, and Y.
When X changes, set A = X, when Y changes, set A = Y.
I can't do this with a derived, as I can't know which of X or Y changed
letA=$derived.by(()=>{returnXreturnY// you can't even express the above idea})
I can do it with effects though;
$effect(()=>{A=X})$effect(()=>{A=Y})
I would prefer to write it as follows to save a bit on vertical space, as I don't find the readability to be impaired by collapsing the function body (tbh, I find the following more readable);
$effect(()=>A=X)$effect(()=>A=Y)
But assignment expressions resolve to the assigned value, and in this case thats always a boolean, not assignable to void.
The return value of an effect can be void but if you return something it needs to be the cleanup function. I think this change will be confusing because then nothing stops you from doing
Describe the problem
An abstract example;
Sometimes an effect is a single expression
Me, and probably a good few others, like to shorten single-statement callbacks into one line
However, this callback cant always be passed to
$effect
because of an incompatible return typeA more tangible example;
I have three boolean states, call them A, X, and Y.
When X changes, set
A = X
, whenY
changes, setA = Y
.I can't do this with a derived, as I can't know which of X or Y changed
I can do it with effects though;
I would prefer to write it as follows to save a bit on vertical space, as I don't find the readability to be impaired by collapsing the function body (tbh, I find the following more readable);
But assignment expressions resolve to the assigned value, and in this case thats always a
boolean
, not assignable tovoid
.Describe the proposed solution
Use
unknown
instead;The same for other effects;
$effect.root
,$effect.pre
Workarounds;
just use javascript
void
. I find this reduces readability, as it adds more boilerplate and forces brackets to stack up at the end.or...
Adding this to
app.d.ts
(or other global declaration) applies the proposed change as a patchImportance
nice to have
The text was updated successfully, but these errors were encountered: