I recommend to you to read up on the Tueller Drill.
Your question is a very good one, and there has been a lot of thought and research into proper responses to imminent threats. In the case of the person with a knife, the rule of thumb is that within 21 feet, they can attack and severely harm or kill a person who has not yet drawn their firearm.
There are actually self defense pistol drills that can help you find your own “Tueller distance”, defined as a number of feet an attacker can be away from you, where you can make two hits into center body mass before they reach you. Some people, with fast draws and good reflexes, might be able to stop an attacker before 21 feet. If you have your weapon drawn and aimed, it can be even shorter.
Knowing that there is a minimum critical distance lets you understand the point at which you need to make a decision, one way or another, as to whether or not to engage an aggressor, before you put yourself at risk of severe injury or death.