Comment

Comment by 59nadir

You can have `Result<number, number>` in both.

You generally have to treat `E` as an error because of the semantics of `.map()` in every language, though, because you have to essentially ignore one of the sides. `map<Result<T, E>>` is untypable for any practical scenario where both sides matter equally.

Also, just to clarify how you can have essentially the same thing you would in f.e. Haskell but in TypeScript:

```

    export type Result<T, E> = Ok<T, E> | Err<T, E>;

    interface Ok<T, E> {
      type: "Ok";
      unwrap(): T;
      map<B>(f: (x: T) => B): Result<B, E>;
      toMaybe(): Just<T>;
      or(errorValue: T): T;
      apply<U>(f: Result<(x: T) => U, E>): Result<U, E>;
      bind<U>(f: (x: T) => Result<U, E>): Result<U, E>;
    }

    interface Err<T, E> {
      type: "Err";
      unwrap(): E;
      map<B>(f: (x: T) => B): Result<B, E>;
      toMaybe(): Nothing<T>;
      or(errorValue: T): T;
      apply<U>(f: Result<(x: T) => U, E>): Result<U, E>;
      bind<U>(f: (x: T) => Result<U, E>): Result<U, E>;
    }
```
parent 59nadir 7 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton