3 ms·
I agree with Maybe not being that useful in TS but the point of an Either/Result type is that the caller is gonna handle it each branch differently. In your exa
by tym0 6y ago
I agree with Maybe not being that useful in TS but the point of an Either/Result type is that the caller is gonna handle it each branch differently. In your example the caller has no way to tell the difference between an error and the succesful return value.
- rattray 6y agoPerhaps I was unclear, the implicit typing is so: const result: FetchResult | Error = ... if (_.isError(result)) { // Here TS knows that result is of type Error return } // Here TS knows that result is of type FetchResult On my phone, apologies for shorthand In any case, with this pattern TS can easily tell the difference between success and error (or left and right or whatever) as long as the error / left case is a class of a different type than the success / right case. Tldr, where relevant, classes are cleaner than tagged disjoint unions in TS. Errors are one situation where this is relevant.