5 ms·
Perhaps I was unclear, the implicit typing is so: const result: FetchResult | Error = ... if (_.isError(result)) { // Here TS knows that result i
by rattray 6y ago
Perhaps 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.