Onan was not killed for being unwilling to impregnate her. He was unwilling to fulfill his role as his brother’s keeper and provide for his brother’s wife and her children.
Actually, that's not what the text says. The text says "God slew him for the thing which he did." It doesn't say "for the thing which he didn't do."
It also doesn't say, one way or the other, whether Onan provided for Tamar. That isn't even mentioned in the account. It just says "the thing which he did." Which was immediately described in the previous verse: deliberately impairing the fertility of the act.