It's a big balancing act and contributing factors would include: whether or not the assailant is armed, the kind of weapon the assailant has, location, ability to retreat, local laws concerning duty to retreat, reasonable ability to defend yourself using nonlethal means (if you're a 5 foot female being attacked by a 7 foot male, you'll get in less trouble for using a knife than a 6 foot male being attacked by a 6 foot male), etc. etc.
In reality, you're going to get in some trouble no matter what the situation is. If you're going to use a knife for self defense, then you need to make sure that it would be completely obvious to anyone who will hear about it later (courts) that your life was in immediate danger and that you had no other choice.
And the "life in immediate danger" bit is the key. If someone tries to mug you and demands money, your only choices are to run or give up the money. Even brandishing the knife in the situation will get you in trouble, and may even escalate the situation to the point that the mugger uses lethal force. Even if a mugger punches or kicks you, you still can't (legally) use a knife if giving up your money would end the confrontation.
As for the baseball bat question, it's marginal, but I'd probably pull out a knife at that point and announce my clear intent to use it should the attack continue. A person can be killed quite easily with a bat. If the bat attacker continues the attack after clearly being warned that you are armed and intend to react with lethal force, then it stands to reason that it is the complete intent of the attacker to cause you serious harm or even death, and thus knife may be warranted in that situation.
Knives seem to have a worse stigma than guns and thus much care has to be taken when considering a knife as a self defense weapon. From a legal standpoint, it's safest to carry a knife that is legal in every way (blade length, opening mechanism, etc.) as a tool (NEVER label a knife as a weapon in the presence of an officer) and maintain it to make sure it is sharp if it ever needs to be used in self defense. Even then, you'll need to know all the laws in your area regarding what lengths you have to go to to escape or end a confrontation without violence before resorting to something as serious as a knife.