Type check with expected type when checking recursive loops.

Closes PR12678.