Interface Either<L, R>

Represents a type that can be either of type L or R. Either

Example: Use Either to handle erros

import { Either, left, right } from '@ortense/functors'

const divide = (
numerator: number,
denominator: number,
): Either<Error, number> => {
if (Number.isNaN(numerator)) {
return left(new Error('Numerator is not a number.'))
}
if (Number.isNaN(denominator)) {
return left(new Error('Denominator is not a number.'))
}

if (denominator === 0) {
return left(new Error('Division by zero is not posible.'))
}

return right(numerator / denominator)
}

Example: capture correct side value

const numerator = Number(document.querySelector('input#numerator').value)
const denominator = Number(document.querySelector('input#denominator').value)
const display = document.querySelector('div#display-result')
const onSuccess = (result: number) => {
display.textContent = `${numerator} / ${denominator} = ${result}`
}
const onError = (error: Error) => {
display.textContent = error.message
}

divide(numerator, denominator)
.right(onSuccess)
.left(onError)

Example: type-safe error handler

app.post('/users', async (req, res) => {
const result: Either<Error, User> = await createUser(req.body)
result
.right(user => ({ id: user.id, name: user.name }))
.right(responseBody => res.status(201).json(responseBody))
.left(error => ({ error: error.name, message: error.message }))
.left(responseBody => res.status(400).json(responseBody))
})
interface Either<L, R> {
    isLeft(): boolean;
    isRight(): boolean;
    left<T>(fn): Either<T, R>;
    right<T>(fn): Either<L, T>;
    unwrap(): L | R;
}

Type Parameters

  • L

    The type of the left side.

  • R

    The type of the right side.

Implemented by

Methods

  • Checks if the Either instance is on the left side.

    Returns boolean

    True if the instance is on the left side, otherwise false.

  • Checks if the Either instance is on the right side.

    Returns boolean

    True if the instance is on the right side, otherwise false.

  • Applies a function to the value inside the Either instance if it is on the left side, returning a new Either instance containing the result of the function. If the Either is on the right side, it returns itself.

    Type Parameters

    • T

    Parameters

    • fn: ((value) => T)

      A function to transform the value on the left side.

        • (value): T
        • Parameters

          • value: L

          Returns T

    Returns Either<T, R>

    An Either instance representing the transformed left side.

  • Applies a function to the value inside the Either instance if it is on the right side, returning a new Either instance containing the result of the function. If the Either is on the left side, it returns itself.

    Type Parameters

    • T

    Parameters

    • fn: ((value) => T)

      A function to transform the value on the right side.

        • (value): T
        • Parameters

          • value: R

          Returns T

    Returns Either<L, T>

    An Either instance representing the transformed right side.

  • Unwraps the value contained in the Either instance.

    Returns L | R

    The value contained in the Either instance.

Generated using TypeDoc