T
TanStack2mo ago
passive-yellow

Should type inference detect `where` clause filters?

For example, if my collection has an attribute that could be string | undefined, but I have a where condition of not(isUndefined(item.attribute)) is it desirable and possible that the type inference of items in the returned dataset is just string? I've found several situations where that would be helpful in my codebase, but open to the possibility that this should not work that way.
3 Replies
optimistic-gold
optimistic-gold2mo ago
yeah some type narrowing would be very nice here — dunno if either @samwillis or @kevindp have looked into this
exotic-emerald
exotic-emerald2mo ago
Hmm, interesting but i think this would be highly complex. If we want to achieve this we essentially need to reify the entire where clause to the type system. In other words, instead of the where clause being a generic where clause type it would need to be a type that precisely represents that where clause. Pretty sure it can be done in typescript but i’m not sure if we really want to go there, types are great but going too far with them can lead to type errors that are pretty much impossible to understand.
metropolitan-bronze
metropolitan-bronze2mo ago
Yep. I think it could be achievable, but will be complex. For comparison, we already have some quite complex types to handle the optionality of joined collection - a left join will make the props it attaches optional, an inner join will not. So I think we likely have some of the moving parts, but it will be complex. May be a v2 feature.

Did you find this page helpful?