tryCatch
utility function for catching errors, it aims to replace try-catch blocks. it returns a tuple of [error, data] and its fully type safe.
Compare with try-catch
with try and catch
with tryCatch:
the return type is a tuple: either [error, undefined] or [undefined, data]. This enforces type safety by requiring you to handle potential errors before accessing the data.
typescript example
You can also add type arguments to the tryCatch function to enforce type safety. the first type argument is the type of the data, the second type argument is the type of the error.