Git Product home page Git Product logo

results-ts's Introduction

Result and Option types, like in Rust! 🤯

Npm package version

Utility functions for creating and handling Rust-like Result and Options types.

Install

pnpm add @jeppech/results-ts

Usage

Hint: Try enabling inlay types in your IDE. The inferred types, should match the comments, in the snippet below.

import { Ok, Err, type Result } from '@jeppech/results-ts';

// The generic `Result`-type (or `Option`-type) must be explicitly set on function signatures.
// Otherwise typescript will infer the underlaying Union type instead.
function greetings(name?: string): Result<string, Error> {
  if (name === 'jeppech') {
    return Err(new Error('I will not greet jeppech!'));
  }

  if (name === undefined) {
    return Ok('Guest');
  }

  return Ok(name);
}

// Result<string, Error>
const t1 = greetings('jeppech');

// Result<number, Error>
const t2 = greetings().map((name) => name.length);

// Result<string, string>
const t3 = greetings('admin').map_err((err) => err.message);

// Option<string>
const t4 = greetings('user').ok();

// `is_ok` and `is_err` can be used to narrow the type
if (t1.is_ok()) {
  // OkResult<string, Error>
  const t4 = t1;
} else {
  // ErrResult<string, Error>
  const t4 = t1;
}

// Option<string>
const o1 = t4;

// `is_some` and `is_none` can be used to narrow the type
if (o1.is_none()) {
  // NoneOption<string>
  const o2 = o1;
} else {
  // SomeOption<string>
  const o2 = o1;
}

// string
const e1 = t1.expect("we do not expect the name to be 'jeppech'. This throws!");
//         ^- this is a Result<string, Error>

// string
const e2 = o1.expect('this does not throw, because the option is not None.');
//         ^- this is a Option<string>

// Result<void, never>
const void_result = Ok()

Acknowledgement

  • This is inspired by the Monads project

results-ts's People

Contributors

jeppech avatar

Stargazers

 avatar  avatar

Watchers

 avatar

results-ts's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.