-
Notifications
You must be signed in to change notification settings - Fork 8
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
Union Types #12
Comments
Can't believe I never encountered this as I use unions everywhere :D You are right, it should be easy to do with immupdate. Right now you would have to do something like this : deepUpdate(c)
.at('aOrB')
.modify(u=> u.name === 'a'
? update(u, { foo, 2 })
: u
) But that looks a bit funny and isn't as readable/declarative. What would you think about a filter function? e.g const isA = (u: A | B): u is A => u.name === 'a'
deepUpdate(c)
.at('aOrB')
.filter(isA)
.at('foo')
.set(2) It would get even easier when/if this get worked on: microsoft/TypeScript#5101 As you wouldn't even need the artificial function annotated with What do you think? |
Thanks for the fast response! The
and it seems similar to how the
Maybe there is a better name than |
I quite like the name filter because you may use it for other things than guarding against a particular type union instance, like "only update if this intermediary key has this value"; sort of like an assertion, but keeping the declarative style and only browsing the (sometimes nullable) keys once. In lib.d.ts, Array.prototype.filter actually has an extra definition to recognize type guards, so I thought it was quite similar ! abortIfNot is very descriptive though, I like it and abortIfUndef would just be a special case. |
Oh wow I didn't know that
is actually possible in TS right now! So yeah, I'm personally fine with calling it Also I would like So to close this comment from my point of view: |
Should be good in version 1.1.7 :) Went with |
Awesome work! Just did a quick test in my actual code with immupdate 1.1.7 and now I can use |
Nice library! I fully agree with what you wrote in the README about "Why Object/Array instead of immutable data structures". I'd love to use it everywhere instead of deeply nested object splice expressions.
What keeps me from completely switching to deepUpdate though is that it does not deal well with union types. For example this:
results in the following type error for me (using lots of strict flags in tsconfig and Typescript 2.6):
I mean, raising a compilation error here totally makes sense because there are no typeguards anywhere around
.at('foo')
.Do you have any thoughts about how one could solve this without giving up too much of the deepUpdate convenience? Is it even possible to use typeguards and generics together in TS?
The text was updated successfully, but these errors were encountered: