Giter Site home page Giter Site logo

result's People

Contributors

dependabot[bot] avatar jviide avatar niklasf avatar pawndev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

result's Issues

return empty error?

Hello,
sometimes I jsut want to return an error but with no value, for the moment I do this :

return Result.err(new Error())

But I would prefer something like this

return Result.err()

Switch error-type during chain

Let's say I have the following result:

const result: Result<number, ErrorA> = someOperation();

and I want to chain like this:

// Assuming ErrorA and ErrorB are structurally different.
result.chain(value => {
  if(value < 0) return err(new ErrorB());
  else return ok(value);
});

then I get the error:

TS2345: Argument of type (value: number) => Result<number, ErrorA> | Result<never, ErrorB> is
  not assignable to parameter of type (value: number) => Result<number, ErrorA>
Type Result<number, ErrorA> | Result<never, ErrorB> is not assignable to type 
  Result<number, ErrorA>
Type Ok<never, ErrorB> is not assignable to type Result<number, ErrorA>
Type Ok<never, ErrorB> is not assignable to type Ok<number, ErrorA>
Property x is missing in type ErrorB but required in type ErrorA

It seems like it is not possible to switch error type inside a chain with only a ok-mapper. The only workaround is:

result.chain<number, ErrorA | ErrorB>(
  (value) => {
    if (value < 0) return err(new ErrorB());
    else return ok(value);
  },
  (error) => err(error)
);

but that is clearly too verbose.

declare modifier required

Hi,

I am not sure how you generate index.d.ts but it seems there is an issue with it as I get the following error when running tsc:

$ tsc
node_modules/@badrap/result/dist/index.d.ts:1:1 - error TS1046: A 'declare' modifier is required for a top level declaration in a .d.ts file.

1 abstract class _Result<T, E extends Error> {
  ~~~~~~~~


Found 1 error.

error Command failed with exit code 1.

Question: How would I chain async functions?

I'm trying to chain an async function, and am getting a type error. Can anyone point me in the right direction?

const fooRes: Result<string, Error> = await foo();
const barRes = fooRes.chain(async e => await bar(e)).unwrap(); 
Argument of type 'e => Promise<Result<string, Error>>' is not assignable to parameter of type '(value: string) => Result<unknown, Error>'.

Thoughts on an orElse/unwrapOrElse equivalent?

I'm liking the lib so far! What are your thoughts on orElse methods? Right now if I want to have errors fallback I do:

const res = fn().chain(
  (x) => Result.ok(x),
  () => otherFn()
);

But that could be made a little more ergonomic if it was something like:

const res = fn().orElse((err) => otherFn());

Or the equivalent unwrap:

const val = fn().unwrapOrElse((err) => otherFn());

I get that it's a balance of keeping the lib small and having things be ergonomic - rust has soo many Result methods 😆

Want to use if/else condition

Thanks for the great library.
I would like to use the following

  const res = Result.ok('test');  

  if (res.isOk) {
      console.log(res.value);
  } else {
      console.log(res.error);  // compile error
  }

However, I get a compile error ”console.log(res.error);”, and I'm thinking the typescript compiler is not able to infer types.

It returns the following error

[tsserver 2339] [E] Property 'error' does not exist on type 'Result<string,Error>'. Property 'error' does not exist on type 'Ok<string, Error>'.

To pass through the compiler, Explicit type definitions are necessary, but not elegant.

    const res = Result.ok('test');
    if (res.isOk) {
      console.log(res.value);
    } else {
      console.log((res as Result.Err<string, Error>).error);
    }

Is there a simpler way to define it, or a way to customize the library itself?

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.