Do you need error handling if your code is typesafe?

Issue

If you write some TypeScript code that is typesafe and is not using third party libraries, do you still need error handling, just because who knows what may happen?

Solution

Yes. Not all errors are type-related. For instance, suppose you have a function that does (say) a fetch from an endpoint. No amount of type information will change the fact that you need to handle the possibility that the request may fail. Similarly, if notifying a bunch of listeners of an event, no amount of type information will protect you from one of those listeners unexpectedly throwing an error.

I’d say that since I switched primarily to TypeScript, I haven’t changed the amount of error handling I do or the level at which I do it (primarily only entry points like event handlers). I have reduced the amount of type-checking I do (I don’t validate that something is a number in a function if the parameter type says it has to be a number, which I might have done in JavaScript), but not the amount of error handling.

Answered By – T.J. Crowder

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.