The closure parameter for Query Builder methods such as whereHas etc. receives a Builder instance as a parameter.
You could source-dive the appropriate Query Builder code to see how it works; but generally you will see that the Builder method has a callable $callback parameter (the Closure) which is called with the $query, e.g.
return $callback($query, $type);