Damn Simple Result Implement
npm install result-typescript``bash`
npm install result-typescript
`typescript
import { Result, Ok, Err } from 'result-typescript';
function Risk(n: number): Result
if (n >= 0) {
return Ok(n);
} else {
return Err('Negative number');
}
}
for (const n of [-1, 0, 1]) {
const result = Risk(n);
if (!result.ok) {
// result is of type Err
console.log('Oops', result.error);
return;
}
// result is of type Ok
console.log('Ok', result.value);
}
`
That's all of it.
There are no methods on the Result type. Avoid using callback and enjoy if` statement :\)
This library is not recommended for JavaScript, because this library is based on Typescript Interface, so some features like Type Guards are not available in JavaScript.
If you are looking for a JavaScript Result library, try resulty, which is base on class inheritance.