Discriminated unions in signals #596
Unanswered
Isaac-Leonard
asked this question in
Q&A
Replies: 2 comments 1 reply
-
I think this an inherent issue in the TS type system that isn't likely to be resolved anytime soon; Solid has ran up against it quite a bit from what I've seen. Relevant thread here. I think you're going to need to make do w/ guards and/or assertions. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Would it be possible to make some sort of conditional computed signal? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Does anyone know how to successfully use signals with discriminated unions?
I can make it work fine if I just use the value but if I want to pass the signal after type narrowing I get an error.
I understand that in the following code trying to use the SuccessScreen component doesn't work as the signal is able to update, and change the inner value while SuccessScreen is still rendered.
I also can't use a useComputed hook in each case of the switch statement as that breaks the rules of not using hooks in conditionals so I'm not sure what the best practice is.
This code will demonstrate the behaviour I'm talking about with a minimal preact app with vite and typescript.
My actual use case is trying to run a large app off of signals with each screens data being held in a top level signal and trying to use a discriminated union in that signal to determine which screen should render without rerendering the app when everything changes.
I know I could build out computed signals for each screen that just return null if the relavent screen doesn't match but then I lose the exhaustative checking for each branch in the switch statement.
Beta Was this translation helpful? Give feedback.
All reactions