1 karma · joined Nov 4, 2019 (7y ago) · 2 submissions
comment 6y ago · link · parent
if ('error' in result) {
console.error(result.error); // ok: result inferred as 'Failure'
} else {
doThings(result.value) // ok: result inferred as 'Success<T>'
}
Even though you used string there, it's pretty type-safe because if you have a typo in your string, the inferred types will propagate other type errors if ('errors' in result) { // typo
console.error(result.errors); // Type error: result inferred as 'never'
} else {
doThings(result.value) // Type error: result inferred as Success<T> | Failure
}comment 7y ago · link · parent