Fix PR19270 - type mismatch caused by invalid optimization.

Patch by Jingyue Wu.