-
Notifications
You must be signed in to change notification settings - Fork 331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Event type guards #777
Comments
Hi Steve, There are actually type-narrowing functions available: https://baconjs.github.io/api3/globals.html#hasvalue |
Nice. I didn't know about these and will definitely use them. Ideally, they wouldn't be needed. I'm considering making a PR to support type narrowing in the event objects themselves, but wondering if it's possible without breaking changes. Thoughts? |
Well I wouldn't necessarily duplicate the functionality that already exists, especially when it may introduce breaking changes. Maybe hiding the fields altogether and improving documentation might be a better route? |
The type narrowing functions feel like a compromise. (Don't get me wrong — I'll use them.) They're not attached to event objects, so it's not OOP, and they have to be imported.
I'm happy to take matters into my own hands, being mindful of not making breaking changes. |
Okay, break a leg :) |
Looks like it can't be done without messy breaking changes. Specifically, the |
Reopening as a possible v4 enhancement. It could be done in a clean way by replacing the event classes with interfaces and factories. |
The following properties could be used to narrow down event types:
hasValue
isNext
isInitial
isError
isEnd
Rather than this:
it would be nice to do this instead:
Here's some working skeleton code (only implements the distinguishing features of various event types):
The key is to declare events as being of type
IEvent<V>
. Although not all event types have a value, the nice part about this is that when eitherhasValue
,isNext
orisInitial
aretrue
,value
doesn't need to be cast toV
.My own Bacon alternative (work in progress) doesn't use event classes at all. Events are created by factories and there's no need for weird prefixed type names such as
IEvent
.The text was updated successfully, but these errors were encountered: