Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Want to use if/else condition #19

Closed
fukaoi opened this issue Jun 9, 2022 · 2 comments
Closed

Want to use if/else condition #19

fukaoi opened this issue Jun 9, 2022 · 2 comments

Comments

@fukaoi
Copy link

fukaoi commented Jun 9, 2022

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?

@jviide
Copy link
Contributor

jviide commented Jun 11, 2022

Thanks for the report! It's interesting that we do use this same pattern quite extensively in our internal code. However, we have compilerOptions.strict enabled in our tsconfig.json files, and disabling it causes TypeScript start giving out the errors you describe.

That makes me think that this behavior is likely related to the TypeScript issue microsoft/TypeScript#10564. As such there's not much we can do on the library level, so I recommend you try one of the following approaches:

  1. Enable compilerOptions.strictNullChecks (or compilerOptions.strict) in your tsconfig.json if it's feasible in your environment.

  2. If you can't modify the TypeScript configuration, then use unwrap:

    const res = Result.ok("test");
    
    res.unwrap(
      (value) => {
        console.log(value);
      },
      (err) => {
        console.log(err);
      }
    );
  3. Or instead of unwrap, try isErr if you so prefer:

    const res = Result.ok("test");
    
    if (res.isOk) {
      console.log(res.value);
    } else if (res.isErr) {
      console.log(res.error);
    }

@fukaoi
Copy link
Author

fukaoi commented Jun 13, 2022

@jviide

Thanks for the reply!
I also appreciate your detailed explanation, it sounds like a typescript issue.

I will try to deal with this res.unwrap((value) => {}, (err) => {}) . I think it is a very cool solution with the same format as match in functional languages.

I hopefully this gets fixed in Typescript 👍  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants