JS Dark Arts: Abusing prototypes and the Result type
Briefly

The article discusses a method to handle errors from external library functions more robustly by adding a 'try' method to all functions in JavaScript. This extension would catch errors in a try-catch block and return a Result instead of relying on exceptions. The implementation involves modifying the Function prototype in TypeScript, allowing for neat error handling, although the author warns against potential issues with generics. Overall, the approach presents a creative solution for managing errors gracefully.
We'll add a method to all functions named try, which will execute this inside a try...catch block, converting the return value to a Result type.
First, we must convince TypeScript this method exists by extending the built-in Function interface.
This should absolutely not be used and will mess with generics inferred from parameters, but still, its pretty neat.
But what if we could do this: Add a method to all functions that handles errors more gracefully by returning a Result type.
Read at Bomh
[
|
]