I want to start by saying that this code is very unintuitive in my opinion, if anyone in the future will need to read and understand this code then he's in for a treat.

With that being said, the solution to what you're asking is very simple, it's just hard to see it with all of those generics, as the answer is just: add another generic type for the return type of the function: