types.functionTo: Use mergeDefinitions on the function return

This symplifies typechecking and allows properties to be used inside the
function body. It also makes possible checking the type of the result.