Skip to content

These

These: object

Defined in: Core/These.ts:28

chainFirst: <A, C, B2>(transform) => <B1>(these) => These<C, B2 | B1>

Chains These computations by passing the first value to transform. Second propagates unchanged; First and Both apply transform to the first value.

A

C

B2 = never

(first) => These<C, B2>

<B1>(these) => These<C, B2 | B1>

These.chainSecond to chain based on the second value.

const double = (n: number): These<number, string> => These.make.first(n * 2);

pipe(These.make.first(5), These.chainFirst(double));            // First(10)
pipe(These.make.both(5, "warn"), These.chainFirst(double));     // First(10)
pipe(These.make.second("warn"), These.chainFirst(double));      // Second("warn")

chainSecond: <B, D, A2>(transform) => <A1>(these) => These<A2 | A1, D>

Chains These computations by passing the second value to transform. First propagates unchanged; Second and Both apply transform to the second value.

B

D

A2 = never

(second) => These<A2, D>

<A1>(these) => These<A2 | A1, D>

These.chainFirst to chain based on the first value.

const shout = (s: string): These<number, string> => These.make.second(s.toUpperCase());

pipe(These.make.second("warn"), These.chainSecond(shout));      // Second("WARN")
pipe(These.make.both(5, "warn"), These.chainSecond(shout));     // Second("WARN")
pipe(These.make.first(5), These.chainSecond(shout));            // First(5)

from: object

Pair: <A, B>(pair) => Both<A, B>

Creates a These.Both holding both values of a Pair.

A

B

Pair<A, B>

Both<A, B>

These.to.Pair to extract a Pair from a These when both values are present.

These.from.Pair(Pair.make("Paris", 2_161_000)); // Both("Paris", 2161000)

getFirstOrElse: <C>(fallback) => <A, B>(these) => C | A

Returns the first value, or a default if the These has no first value. The default can be a different type, widening the result to A | C.

C

() => C

<A, B>(these) => C | A

These.getSecondOrElse to retrieve the second value with fallback.

pipe(These.make.first(5), These.getFirstOrElse(() => 0));            // 5
pipe(These.make.both(5, "warn"), These.getFirstOrElse(() => 0));     // 5
pipe(These.make.second("warn"), These.getFirstOrElse(() => 0));      // 0
pipe(These.make.second("warn"), These.getFirstOrElse(() => null));   // null — typed as number | null

getSecondOrElse: <D>(fallback) => <B, A>(these) => D | B

Returns the second value, or a default if the These has no second value. The default can be a different type, widening the result to B | D.

D

() => D

<B, A>(these) => D | B

These.getFirstOrElse to retrieve the first value with fallback.

pipe(These.make.second("warn"), These.getSecondOrElse(() => "none")); // "warn"
pipe(These.make.both(5, "warn"), These.getSecondOrElse(() => "none")); // "warn"
pipe(These.make.first(5), These.getSecondOrElse(() => "none"));       // "none"
pipe(These.make.first(5), These.getSecondOrElse(() => null));         // null — typed as string | null

hasFirst: <A, B>(these) => these is First<A> | Both<A, B>

Returns true if the These contains a first value (First or Both).

A

B

These<A, B>

these is First<A> | Both<A, B>

These.hasSecond to check if These contains a second value.

These.hasFirst(These.make.first(42));       // true
These.hasFirst(These.make.both(42, "warn"));// true
These.hasFirst(These.make.second("warn"));  // false

hasSecond: <A, B>(these) => these is Second<B> | Both<A, B>

Returns true if the These contains a second value (Second or Both).

A

B

These<A, B>

these is Second<B> | Both<A, B>

These.hasFirst to check if These contains a first value.

These.hasSecond(These.make.second("warn"));  // true
These.hasSecond(These.make.both(42, "warn"));// true
These.hasSecond(These.make.first(42));       // false

is: object

both: <A, B>(these) => these is Both<A, B> = isBoth

Type guard — checks if a These holds both values simultaneously.

A

B

These<A, B>

these is Both<A, B>

const val = These.make.both(42, "warning");
if (These.is.both(val)) {
  console.log(val.first, val.second); // 42 "warning"
}

first: <A, B>(these) => these is First<A> = isFirst

Type guard — checks if a These holds only a first value.

A

B

These<A, B>

these is First<A>

const val = These.make.first(42);
if (These.is.first(val)) {
  console.log(val.first); // 42
}

second: <A, B>(these) => these is Second<B> = isSecond

Type guard — checks if a These holds only a second value.

A

B

These<A, B>

these is Second<B>

const val = These.make.second("warning");
if (These.is.second(val)) {
  console.log(val.second); // "warning"
}

make: object

both: <A, B>(first, second) => Both<A, B> = makeBoth

Creates a These holding both a first and a second value simultaneously.

A

B

A

B

Both<A, B>

These.make.both(42, "Deprecated API used"); // { kind: "Both", first: 42, second: "Deprecated API used" }

first: <A>(value) => First<A> = makeFirst

Creates a These holding only a first value.

A

A

First<A>

These.make.first(42); // { kind: "First", first: 42 }

second: <B>(value) => Second<B> = makeSecond

Creates a These holding only a second value.

B

B

Second<B>

These.make.second("warning"); // { kind: "Second", second: "warning" }

mapBoth: <A, C, B, D>(onFirst, onSecond) => (these) => These<C, D>

Transforms both the first and second values independently.

A

C

B

D

(first) => C

(second) => D

(these) => These<C, D>

pipe(
  These.make.both(5, "warn"),
  These.mapBoth(n => n * 2, e => e.toUpperCase())
); // Both(10, "WARN")

mapFirst: <A, C>(transform) => <B>(these) => These<C, B>

Transforms the first value, leaving the second unchanged.

A

C

(first) => C

<B>(these) => These<C, B>

pipe(These.make.first(5), These.mapFirst(n => n * 2));           // First(10)
pipe(These.make.both(5, "warn"), These.mapFirst(n => n * 2));    // Both(10, "warn")
pipe(These.make.second("warn"), These.mapFirst(n => n * 2));     // Second("warn")

mapSecond: <B, D>(transform) => <A>(these) => These<A, D>

Transforms the second value, leaving the first unchanged.

B

D

(second) => D

<A>(these) => These<A, D>

pipe(These.make.second("warn"), These.mapSecond(e => e.toUpperCase()));     // Second("WARN")
pipe(These.make.both(5, "warn"), These.mapSecond(e => e.toUpperCase()));    // Both(5, "WARN")

match: <A, B, C>(cases) => (these) => C

Pattern matches on a These, returning the result of the matching case.

A

B

C

(first, second) => C

(first) => C

(second) => C

(these) => C

pipe(
  these,
  These.match({
    first: a => `First: ${a}`,
    second: b => `Second: ${b}`,
    both: (a, b) => `Both: ${a} / ${b}`
  })
);

swap: <A, B>(these) => These<B, A>

Swaps the roles of first and second values.

  • First(a) → Second(a)
  • Second(b) → First(b)
  • Both(a, b) → Both(b, a)

A

B

These<A, B>

These<B, A>

These.swap(These.make.first(5));            // Second(5)
These.swap(These.make.second("warn"));      // First("warn")
These.swap(These.make.both(5, "warn"));     // Both("warn", 5)

tapBoth: <A, B>(sideEffect) => (these) => These<A, B>

Runs a side effect on both values when both are present simultaneously (Both variant only).

A

B

(first, second) => void

(these) => These<A, B>

pipe(
  These.make.both(5, "warn"),
  These.tapBoth((n, w) => console.log(`${n} with warning ${w}`)),
); // logs "5 with warning warn"

tapFirst: <A>(sideEffect) => <B>(these) => These<A, B>

Runs a side effect on the first value (if present in First or Both) without changing the These. Useful for logging or debugging.

A

(first) => void

<B>(these) => These<A, B>

  • These.tapSecond to run a side effect on the second value instead.
  • These.tapBoth to run a side effect on both values when both are present.
pipe(These.make.first(5), These.tapFirst(console.log)); // logs 5, returns First(5)
pipe(These.make.both(5, "warn"), These.tapFirst(console.log)); // logs 5, returns Both(5, "warn")

tapSecond: <B>(sideEffect) => <A>(these) => These<A, B>

Runs a side effect on the second value (if present in Second or Both) without changing the These. Useful for logging or reporting warnings.

B

(second) => void

<A>(these) => These<A, B>

  • These.tapFirst to run a side effect on the first value instead.
  • These.tapBoth to run a side effect on both values when both are present.
pipe(These.make.second("warn"), These.tapSecond(console.warn)); // logs "warn", returns Second("warn")
pipe(These.make.both(5, "warn"), These.tapSecond(console.warn)); // logs "warn", returns Both(5, "warn")

to: object

Pair: <A, B>(these) => Maybe<Pair<A, B>>

Converts a These to a Maybe containing a Pair if both values are present. Returns Some([first, second]) for Both; returns None for First and Second.

A

B

These<A, B>

Maybe<Pair<A, B>>

These.from.Pair to create a These.Both from a Pair.

These.to.Pair(These.make.both("Paris", 2_161_000)); // Some(["Paris", 2161000])
These.to.Pair(These.make.first("Paris"));           // None
These.to.Pair(These.make.second(2_161_000));        // None