diff --git a/index.ts b/index.ts index 20d154c..b7532a2 100644 --- a/index.ts +++ b/index.ts @@ -30,3 +30,5 @@ export type { AppConfig, RedisConfig, } from './src/config/configTypes' + +export type { Either } from './src/errors/either' diff --git a/src/errors/either.ts b/src/errors/either.ts new file mode 100644 index 0000000..e5c374e --- /dev/null +++ b/src/errors/either.ts @@ -0,0 +1,18 @@ +type Left = { + error: T + result?: never +} + +type Right = { + 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 = NonNullable | Right>