result-in-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Result-in-ts

Overview

Result-in-ts is a TypeScript library designed to handle results and errors in a functional programming style. It provides a simple and effective way to manage success and failure cases without relying on exceptions.

Features

  • Easy-to-use API for handling results and errors
  • Functional programming style
  • Type-safe error handling
  • Lightweight and performant

Installation

From npm

npm install result-in-ts       # npm
yarn add result-in-ts          # yarn
bun add result-in-ts           # bun
pnpm add result-in-ts          # pnpm

Usage

Here's a basic example of how to use Result-in-ts:

import { Result } from "./result";

function divide(a: number, b: number): Result<number, string> {
  if (b === 0) {
    return Result.err("Division by zero");
  }
  return Result.ok(a / b);
}

const result = divide(10, 2);

if (result.isOk()) {
  console.log("Result:", result.unwrap());
} else {
  console.error("Error:", result.unwrapErr());
}

const expectErrorResult = divide(10, 0);

if (expectErrorResult.isOk()) {
  console.log("Result:", expectErrorResult.unwrap());
} else {
  console.error("Error:", expectErrorResult.unwrapErr());
}

Readme

Keywords

Package Sidebar

Install

npm i result-in-ts

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

5.6 kB

Total Files

4

Last publish

Collaborators

  • kepennar