A classic Result type that can either be Ok or Err.
Result
Ok
Err
Result wraps a value under the value field, and provides convenience methods for checking if Result is Ok or Err:
value
const answer: Result<number, never> = Ok(42);answer.isOk(); // => trueanswer.isErr(); // => false Copy
const answer: Result<number, never> = Ok(42);answer.isOk(); // => trueanswer.isErr(); // => false
If the function returns a compatible Result type, instead of unwrapping the result, and wrapping it back, map and mapErr methods can be used:
map
mapErr
function someFunctionA(x: number): Result<number, string> { if (0 === x % 2) return Err("number was even"); return Ok(x);}function someFunctionB(num: number): Result<number, string> { return someFunctionA(num).map((x) => x + 1);} Copy
function someFunctionA(x: number): Result<number, string> { if (0 === x % 2) return Err("number was even"); return Ok(x);}function someFunctionB(num: number): Result<number, string> { return someFunctionA(num).map((x) => x + 1);}
A classic
Resulttype that can either beOkorErr.Resultwraps a value under thevaluefield, and provides convenience methods for checking ifResultisOkorErr:If the function returns a compatible
Resulttype, instead of unwrapping the result, and wrapping it back,mapandmapErrmethods can be used: