For God's threatening of the child's death might be conditional, as that was of Nineveh's destruction, Jonah 3:4.
Related Commentaries of 2 Samuel 12:22
2 Samuel 12:22
22 And he said, While the child was yet alive, I fasted and wept: for I said, Who can tell whether GOD will be gracious to me, that the child may live?