3 ms·
I don’t agree with using Either or Result types in JavaScript, but I can see some arguments. TS function types don’t include expected exceptions. Unlike Java w
by dexwiz 3y ago
I don’t agree with using Either or Result types in JavaScript, but I can see some arguments.
TS function types don’t include expected exceptions. Unlike Java which has Exceptions in its method signatures (unless it’s a Runtime), you cannot know what Error might be thrown. It’s on the developer to write try/catch and the compiler can’t enforce it. The TS compile can enforce a Result or Either type.
Pattern matching is in vogue right now. Statement based try/catch it out of style compared to Enum pattern matching, since try/catch doesn’t evaluate to a value but pattern matching does. You can pattern match on Either.
There was a long standing bug with V8 that caused code with try/catch to not be optimized. This caused many JS developers to avoid throwing exceptions. I think it’s been addressed, but people now avoid exceptions as dogma.