/** * @since 1.0.0 */ import type * as Data from "@effect/data/Data"; import * as Equivalence from "@effect/data/Equivalence"; import type { LazyArg } from "@effect/data/Function"; import type { TypeLambda } from "@effect/data/HKT"; import type { Inspectable } from "@effect/data/Inspectable"; import type { Option } from "@effect/data/Option"; import type { Pipeable } from "@effect/data/Pipeable"; import type * as Unify from "@effect/data/Unify"; import * as Gen from "@effect/data/UtilsGen"; /** * @category models * @since 1.0.0 */ export type Either = Left | Right; /** * @category symbols * @since 1.0.0 */ export declare const TypeId: unique symbol; /** * @category symbols * @since 1.0.0 */ export type TypeId = typeof TypeId; /** * @category models * @since 1.0.0 */ export interface Left extends Data.Case, Pipeable, Inspectable { readonly _tag: "Left"; readonly left: E; readonly [TypeId]: { readonly _A: (_: never) => A; readonly _E: (_: never) => E; }; [Unify.typeSymbol]?: unknown; [Unify.unifySymbol]?: EitherUnify; [Unify.blacklistSymbol]?: EitherUnifyBlacklist; } /** * @category models * @since 1.0.0 */ export interface Right extends Data.Case, Pipeable, Inspectable { readonly _tag: "Right"; readonly right: A; readonly [TypeId]: { readonly _A: (_: never) => A; readonly _E: (_: never) => E; }; [Unify.typeSymbol]?: unknown; [Unify.unifySymbol]?: EitherUnify; [Unify.blacklistSymbol]?: EitherUnifyBlacklist; } /** * @category models * @since 1.0.0 */ export interface EitherUnify { Either?: () => A[Unify.typeSymbol] extends Either | infer _ ? Either : never; } /** * @category models * @since 1.0.0 */ export interface EitherUnifyBlacklist { } /** * @category type lambdas * @since 1.0.0 */ export interface EitherTypeLambda extends TypeLambda { readonly type: Either; } /** * Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias * of this structure. * * @category constructors * @since 1.0.0 */ export declare const right: (a: A) => Either; /** * Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this * structure. * * @category constructors * @since 1.0.0 */ export declare const left: (e: E) => Either; /** * Takes a lazy default and a nullable value, if the value is not nully (`null` or `undefined`), turn it into a `Right`, if the value is nully use * the provided default as a `Left`. * * @example * import * as Either from '@effect/data/Either' * * assert.deepStrictEqual(Either.fromNullable(1, () => 'fallback'), Either.right(1)) * assert.deepStrictEqual(Either.fromNullable(null, () => 'fallback'), Either.left('fallback')) * * @category constructors * @since 1.0.0 */ export declare const fromNullable: { (onNullable: (a: A) => E): (self: A) => Either>; (self: A, onNullable: (a: A) => E): Either>; }; /** * @example * import * as Either from '@effect/data/Either' * import * as Option from '@effect/data/Option' * * assert.deepStrictEqual(Either.fromOption(Option.some(1), () => 'error'), Either.right(1)) * assert.deepStrictEqual(Either.fromOption(Option.none(), () => 'error'), Either.left('error')) * * @category constructors * @since 1.0.0 */ export declare const fromOption: { (self: Option, onNone: () => E): Either; (onNone: () => E): (self: Option) => Either; }; declare const try_: { (options: { readonly try: LazyArg; readonly catch: (error: unknown) => E; }): Either; (evaluate: LazyArg): Either; }; export { /** * Imports a synchronous side-effect into a pure `Either` value, translating any * thrown exceptions into typed failed eithers creating with `Either.left`. * * @category constructors * @since 1.0.0 */ try_ as try }; /** * Tests if a value is a `Either`. * * @param input - The value to test. * * @example * import { isEither, left, right } from '@effect/data/Either' * * assert.deepStrictEqual(isEither(right(1)), true) * assert.deepStrictEqual(isEither(left("a")), true) * assert.deepStrictEqual(isEither({ right: 1 }), false) * * @category guards * @since 1.0.0 */ export declare const isEither: (input: unknown) => input is Either; /** * Determine if a `Either` is a `Left`. * * @param self - The `Either` to check. * * @example * import { isLeft, left, right } from '@effect/data/Either' * * assert.deepStrictEqual(isLeft(right(1)), false) * assert.deepStrictEqual(isLeft(left("a")), true) * * @category guards * @since 1.0.0 */ export declare const isLeft: (self: Either) => self is Left; /** * Determine if a `Either` is a `Right`. * * @param self - The `Either` to check. * * @example * import { isRight, left, right } from '@effect/data/Either' * * assert.deepStrictEqual(isRight(right(1)), true) * assert.deepStrictEqual(isRight(left("a")), false) * * @category guards * @since 1.0.0 */ export declare const isRight: (self: Either) => self is Right; /** * Converts a `Either` to an `Option` discarding the `Left`. * * Alias of {@link toOption}. * * @example * import * as O from '@effect/data/Option' * import * as E from '@effect/data/Either' * * assert.deepStrictEqual(E.getRight(E.right('ok')), O.some('ok')) * assert.deepStrictEqual(E.getRight(E.left('err')), O.none()) * * @category getters * @since 1.0.0 */ export declare const getRight: (self: Either) => Option; /** * Converts a `Either` to an `Option` discarding the value. * * @example * import * as O from '@effect/data/Option' * import * as E from '@effect/data/Either' * * assert.deepStrictEqual(E.getLeft(E.right('ok')), O.none()) * assert.deepStrictEqual(E.getLeft(E.left('err')), O.some('err')) * * @category getters * @since 1.0.0 */ export declare const getLeft: (self: Either) => Option; /** * @category equivalence * @since 1.0.0 */ export declare const getEquivalence: (EE: Equivalence.Equivalence, EA: Equivalence.Equivalence) => Equivalence.Equivalence>; /** * @category mapping * @since 1.0.0 */ export declare const mapBoth: { (options: { readonly onLeft: (e: E1) => E2; readonly onRight: (a: A) => B; }): (self: Either) => Either; (self: Either, options: { readonly onLeft: (e: E1) => E2; readonly onRight: (a: A) => B; }): Either; }; /** * Maps the `Left` side of an `Either` value to a new `Either` value. * * @param self - The input `Either` value to map. * @param f - A transformation function to apply to the `Left` value of the input `Either`. * * @category mapping * @since 1.0.0 */ export declare const mapLeft: { (f: (e: E) => G): (self: Either) => Either; (self: Either, f: (e: E) => G): Either; }; /** * Maps the `Right` side of an `Either` value to a new `Either` value. * * @param self - An `Either` to map * @param f - The function to map over the value of the `Either` * * @category mapping * @since 1.0.0 */ export declare const map: { (f: (a: A) => B): (self: Either) => Either; (self: Either, f: (a: A) => B): Either; }; /** * Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the `onLeft function, * if the value is a `Right` the inner value is applied to the `onRight` function. * * @example * import * as E from '@effect/data/Either' * import { pipe } from '@effect/data/Function' * * const onLeft = (strings: ReadonlyArray): string => `strings: ${strings.join(', ')}` * * const onRight = (value: number): string => `Ok: ${value}` * * assert.deepStrictEqual(pipe(E.right(1), E.match({ onLeft, onRight })), 'Ok: 1') * assert.deepStrictEqual( * pipe(E.left(['string 1', 'string 2']), E.match({ onLeft, onRight })), * 'strings: string 1, string 2' * ) * * @category pattern matching * @since 1.0.0 */ export declare const match: { (options: { readonly onLeft: (e: E) => B; readonly onRight: (a: A) => C; }): (self: Either) => B | C; (self: Either, options: { readonly onLeft: (e: E) => B; readonly onRight: (a: A) => C; }): B | C; }; /** * @category getters * @since 1.0.0 */ export declare const merge: (self: Either) => E | A; /** * Returns the wrapped value if it's a `Right` or a default value if is a `Left`. * * @example * import * as Either from '@effect/data/Either' * * assert.deepStrictEqual(Either.getOrElse(Either.right(1), (error) => error + "!"), 1) * assert.deepStrictEqual(Either.getOrElse(Either.left("not a number"), (error) => error + "!"), "not a number!") * * @category getters * @since 1.0.0 */ export declare const getOrElse: { (onLeft: (e: E) => B): (self: Either) => B | A; (self: Either, onLeft: (e: E) => B): A | B; }; /** * @example * import * as Either from '@effect/data/Either' * * assert.deepStrictEqual(Either.getOrNull(Either.right(1)), 1) * assert.deepStrictEqual(Either.getOrNull(Either.left("a")), null) * * @category getters * @since 1.0.0 */ export declare const getOrNull: (self: Either) => A | null; /** * @example * import * as Either from '@effect/data/Either' * * assert.deepStrictEqual(Either.getOrUndefined(Either.right(1)), 1) * assert.deepStrictEqual(Either.getOrUndefined(Either.left("a")), undefined) * * @category getters * @since 1.0.0 */ export declare const getOrUndefined: (self: Either) => A | undefined; /** * Extracts the value of an `Either` or throws if the `Either` is `Left`. * * If a default error is sufficient for your use case and you don't need to configure the thrown error, see {@link getOrThrow}. * * @param self - The `Either` to extract the value from. * @param onLeft - A function that will be called if the `Either` is `Left`. It returns the error to be thrown. * * @example * import * as E from "@effect/data/Either" * * assert.deepStrictEqual( * E.getOrThrowWith(E.right(1), () => new Error('Unexpected Left')), * 1 * ) * assert.throws(() => E.getOrThrowWith(E.left("error"), () => new Error('Unexpected Left'))) * * @category getters * @since 1.0.0 */ export declare const getOrThrowWith: { (onLeft: (e: E) => unknown): (self: Either) => A; (self: Either, onLeft: (e: E) => unknown): A; }; /** * Extracts the value of an `Either` or throws if the `Either` is `Left`. * * The thrown error is a default error. To configure the error thrown, see {@link getOrThrowWith}. * * @param self - The `Either` to extract the value from. * @throws `Error("getOrThrow called on a Left")` * * @example * import * as E from "@effect/data/Either" * * assert.deepStrictEqual(E.getOrThrow(E.right(1)), 1) * assert.throws(() => E.getOrThrow(E.left("error"))) * * @category getters * @since 1.0.0 */ export declare const getOrThrow: (self: Either) => A; /** * Returns `self` if it is a `Right` or `that` otherwise. * * @param self - The input `Either` value to check and potentially return. * @param that - A function that takes the error value from `self` (if it's a `Left`) and returns a new `Either` value. * * @category error handling * @since 1.0.0 */ export declare const orElse: { (that: (e1: E1) => Either): (self: Either) => Either; (self: Either, that: (e1: E1) => Either): Either; }; /** * @category combining * @since 1.0.0 */ export declare const flatMap: { (f: (a: A) => Either): (self: Either) => Either; (self: Either, f: (a: A) => Either): Either; }; /** * Takes a structure of `Option`s and returns an `Option` of values with the same structure. * * - If a tuple is supplied, then the returned `Option` will contain a tuple with the same length. * - If a struct is supplied, then the returned `Option` will contain a struct with the same keys. * - If an iterable is supplied, then the returned `Option` will contain an array. * * @param fields - the struct of `Option`s to be sequenced. * * @example * import * as Either from "@effect/data/Either" * * assert.deepStrictEqual(Either.all([Either.right(1), Either.right(2)]), Either.right([1, 2])) * assert.deepStrictEqual(Either.all({ a: Either.right(1), b: Either.right("hello") }), Either.right({ a: 1, b: "hello" })) * assert.deepStrictEqual(Either.all({ a: Either.right(1), b: Either.left("error") }), Either.left("error")) * * @category combining * @since 1.0.0 */ export declare const all: > | Record>>(input: I) => [I] extends [ReadonlyArray>] ? Either] ? E : never, { -readonly [K in keyof I]: [I[K]] extends [Either] ? A : never; }> : [I] extends [Iterable>] ? Either> : Either] ? E : never, { -readonly [K in keyof I]: [I[K]] extends [Either] ? A : never; }>; /** * @since 1.0.0 */ export declare const reverse: (self: Either) => Either; /** * @category generators * @since 1.0.0 */ export declare const gen: Gen.Gen>; //# sourceMappingURL=Either.d.ts.map