Need guidance on return types of query
Hi, I need help understanding why the return type of the given query contains null in the object return of get despite having a null check?
return type
but putting "!" solves the issue?
return type
6 Replies
Array.filter doesn't do type narrowing well. I personally use Array.flatMap instead - just return an empty array in place of false to filter items out. The result will be narrowed the way you're expecting.
Yep, I'll do this instead. By the way, thank you for the tip!
Wow TIL! I've always been curious about that and ended up writing helper functions to get the types right. thanks!
Yeah type predicate works too, and is more idiomatic than abusing flatmap lol
flatMap what's magical about this it works as well!
Honestly, far as I understand, they just typed it better than filter, they should both work this well