Skip to content

Commit

Permalink
Add Either types (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Jan 9, 2023
1 parent 2490925 commit c5337a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ export type {
AppConfig,
RedisConfig,
} from './src/config/configTypes'

export type { Either } from './src/errors/either'
18 changes: 18 additions & 0 deletions src/errors/either.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type Left<T> = {
error: T
result?: never
}

type Right<U> = {
error?: never
result: U
}

/**
* Either is a functional programming type which is used to communicate errors happening in potentially recoverable scenarios.
* It can return either an error (Left side) or a resolved result (Right side), but not both.
* It is up to caller of the function to handle received error or throw (Public)NonRecoverableError if it cannot.
*
* @see {@link https://antman-does-software.com/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable Further reading on motivation for Either type}
*/
export type Either<T, U> = NonNullable<Left<T> | Right<U>>

0 comments on commit c5337a1

Please sign in to comment.