Error Handling
When we write functions, we need to bear in mind that not everything always works perfectly. What will we do if the function receives unexpected input? How will our program react if some other function that we need to call doesn't work perfectly? It's always a good idea to validate function input. Yes, we're using TypeScript, and we can be reasonably sure that if we expect a string, we won't get an object instead, but sometimes, external input doesn't conform to our types. Sometimes, our own logic may be erroneous. Consider this function:
const divide = (numerator: number, denominator: number) => { return numerator / denominator; }
It looks fine, but what if I pass in the number 0
as the denominator? We cannot divide by zero, and so the result will be the constant, NaN
. NaN
, when used in any mathematical equation, will always return NaN
. This could introduce a serious bug into our system, and this needs to be...