This is not intended for public/general use as the helper/utility aspect is very specific and very shallow, use ts-results-es
package directly instead.
A helper library for Rust's Result and Option objects, utilizing and extending the ts-results-es
package.
# Bun
bun add @deanrih/ts-lib-algebraic-data-type
# pnpm
pnpm add @deanrih/ts-lib-algebraic-data-type
# npm
npm install @deanrih/ts-lib-algebraic-data-type
import { Err, Ok, type Result } from "@deanrih/ts-lib-algebraic-data-type";
function fetchSomething(): Result<string> {
const shouldOk = Math.random() < 0.5;
if (shouldOk) {
return Ok("Success to fetch data");
} else {
return Err(new Error("Failed to fetch data"));
}
}
function main() {
const result = fetchSomething();
if (result.isErr()) {
throw result.error;
}
const value = result.value;
console.log(value);
}
main();
Checkout the example folder.
- Underlying library:
ts-results-es