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

Either from try types docs #66

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions either/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,18 @@ function fromTry<L, R>(fn: () => R): Either<L, R>;
fromTry(() => 2); // Either<never, number>.Right
fromTry(() => {
throw new Error("test");
}); // Either<Error, never>.Left
}); // Either<unknown, never>.Left

// enforcing error type
fromTry<number, ParsingError>(() => {
const result = parseInt(value, 10);
if (Number.isNaN(result)) {
throw new ParsingError('Invalid number');
}
return result;
})
```
Enforcing error types might be error-prone. [Read more](#enforcing-error-types) about it.

#### `fromPromise`

Expand All @@ -227,9 +237,14 @@ function fromPromise<L, R>(promise: Promise<R>): Promise<Either<L, R>>;

```typescript
fromPromise(Promise.resolve(2)); // Either<never, number>.Right
fromPromise(Promise.reject(new Error("test"))); // Either<Error, never>.Left
fromPromise(Promise.reject(new Error("test"))); // Either<unknown, never>.Left

// enforcing error type
fromPromise<number, Error>(Promise.resolve(2));
```

Enforcing error types might be error-prone. [Read more](#enforcing-error-types) about it.

#### `isEither`

```typescript
Expand Down Expand Up @@ -521,6 +536,71 @@ left(2).unwrapOrElse(num => num * 2) // returns 4
right(2).unwrapOrElse(num => num * 2) // returns 2
```

#### Enforcing error types

Typescript cannot detect type of thrown errors or rejected promises.

```typescript
class ParsingError {
constructor(readonly message: string) {}
}

// type is Either<unknown, number> but in theory should be Either<ParsingError, number>
const newVal1 = fromTry(() => {
if (Math.random() > 0.5) {
return 10;
}
throw new ParsingError('Error')
});

// type is Either<unknown, never> but in theory should be Either<ParsingError, never>
const newVal2 = fromPromise(Promise.reject(new ParsingError('Error')))
```

Having that on might you might sometimes need to add a hint for Typescript.
```typescript
// Either<ParsingError, number>
const newVal1 = fromTry<number, ParsingError>(() => {
if (Math.random() > 0.5) {
return 10;
}
throw new ParsingError('Error')
});

// Either<ParsingError, never>.Left
const newVal2 = fromPromise<number, ParsingError>(() => {
throw new Error("test");
});
```

This might be error-prone.
You might change type of thrown error and forgot to change hint for typescript. In that case Typescript will never spot the error.
```typescript
const newVal1 = fromTry<number, ParsingError>(() => { // invalid type hint
if (Math.random() > 0.5) {
return 10;
}
throw new AnotherErrorType('Error')
});
```

That is why it is safer to `mapLeft` thrown errors with type guards.
```typescript
// Either<AnotherErrorType, number>
const newVal1 = fromTry(() => {
if (Math.random() > 0.5) {
return 10;
}
throw new AnotherErrorType();
})
.mapLeft(x=> {
if (x instanceof AnotherErrorType) {
return x;
}
throw new Error('Invalid error type thrown');
})
```

## License

MIT (c) Artem Kobzar see LICENSE file.
5 changes: 3 additions & 2 deletions either/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type StaticCheck = ClassImplements<
typeof EitherConstructor,
[MonadConstructor, ApplicativeConstructor, AsyncChainable<Either<any, any>>]
>;

class EitherConstructor<L, R, T extends EitherType = EitherType>
implements AsyncMonad<R>, Alternative<T>, Container<R> {
static chain<L, R, NR>(f: (v: R) => Promise<Either<never, NR>>): (m: Either<L, R>) => Promise<Either<L, NR>>;
Expand Down Expand Up @@ -193,11 +194,11 @@ class EitherConstructor<L, R, T extends EitherType = EitherType>
return EitherConstructor.right(v);
}

static fromPromise<L, R>(promise: Promise<R>): Promise<Either<L, R>> {
static fromPromise<R, L = unknown>(promise: Promise<R>): Promise<Either<L, R>> {
return promise.then(EitherConstructor.right).catch(EitherConstructor.left);
}

static fromTry<L, R>(fn: () => R): Either<L, R> {
static fromTry<R, L = unknown>(fn: () => R): Either<L, R> {
try {
return EitherConstructor.right(fn());
} catch (e) {
Expand Down