1.0.3 • Published 11 months ago
result-typescript v1.0.3
result-typescript
Installation
npm install result-typescript
Usage
import { Result, Ok, Err } from 'result-typescript';
function Risk(n: number): Result<number, string> {
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<string>
console.log('Oops', result.error);
return;
}
// result is of type Ok<number>
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 :)
How About JavaScript?
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.