import * as O from 'fp-ts/Option'; import { noop } from 'lodash'; import { pipe } from 'fp-ts/function'; import * as E from 'fp-ts/Either'; import * as TE from 'fp-ts/TaskEither'; export function assertNone(e: O.Option): asserts e is O.None { pipe( e, O.fold( () => ({}), a => { throw new Error(`None expected, got a Some: ${a}`); } ) ); } export function assertSome(e: O.Option, onSome: (a: A) => void = noop): asserts e is O.Some { pipe( e, O.fold(() => { throw new Error('Some expected, got a None'); }, onSome) ); } export function assertRight(e: E.Either, onRight: (a: A) => void = noop): asserts e is E.Right { pipe( e, E.fold(l => { throw new Error(`Right expected, got a Left: ${l}`); }, onRight) ); } export function assertLeft(e: E.Either, onLeft: (a: L) => void = noop): asserts e is E.Left { pipe( e, E.fold(onLeft, a => { throw new Error(`Left expected, got a Right: ${a}`); }) ); } export async function assertResolvesRight(e: TE.TaskEither, onRight: (a: A) => void = noop) { assertRight(await e(), onRight); } export async function assertResolvesLeft(e: TE.TaskEither, onLeft: (a: L) => void = noop) { assertLeft(await e(), onLeft); }