Skip to content

These

These: object

Defined in: Core/These.ts:26

chainFirst: <A, B, C>(f) => (data) => These<C, B>

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

A

B

C

(a) => These<C, B>

(data) => These<C, B>

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: <A, B, D>(f) => (data) => These<A, D>

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

A

B

D

(b) => These<A, D>

(data) => These<A, D>

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)

fold: <A, B, C>(onFirst, onSecond, onBoth) => (data) => C

Extracts a value from a These by providing handlers for all three cases.

A

B

C

(a) => C

(b) => C

(a, b) => C

(data) => C

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

getFirstOrElse: <A, C>(defaultValue) => <B>(data) => A | C

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.

A

C

() => C

<B>(data) => A | C

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: <B, D>(defaultValue) => <A>(data) => B | D

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.

B

D

() => D

<A>(data) => B | D

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>(data) => data is TheseFirst<A> | TheseBoth<A, B>

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

A

B

These<A, B>

data is TheseFirst<A> | TheseBoth<A, B>

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>(data) => data is TheseSecond<B> | TheseBoth<A, B>

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

A

B

These<A, B>

data is TheseSecond<B> | TheseBoth<A, B>

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>(data) => data is TheseBoth<A, B> = isBoth

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

A

B

These<A, B>

data is TheseBoth<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>(data) => data is TheseFirst<A> = isFirst

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

A

B

These<A, B>

data is TheseFirst<A>

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

second: <A, B>(data) => data is TheseSecond<B> = isSecond

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

A

B

These<A, B>

data is TheseSecond<B>

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

make: object

both: <A, B>(f, s) => TheseBoth<A, B> = makeBoth

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

A

B

A

B

TheseBoth<A, B>

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

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

Creates a These holding only a first value.

A

A

TheseFirst<A>

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

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

Creates a These holding only a second value.

B

B

TheseSecond<B>

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

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

Transforms both the first and second values independently.

A

C

B

D

(a) => C

(b) => D

(data) => These<C, D>

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

mapFirst: <A, C>(f) => <B>(data) => These<C, B>

Transforms the first value, leaving the second unchanged.

A

C

(a) => C

<B>(data) => 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>(f) => <A>(data) => These<A, D>

Transforms the second value, leaving the first unchanged.

B

D

(b) => D

<A>(data) => 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) => (data) => C

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

A

B

C

(a, b) => C

(a) => C

(b) => C

(data) => C

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

swap: <A, B>(data) => 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)

tap: <A>(f) => <B>(data) => These<A, B>

Runs a side effect on the first value without changing the These. Useful for logging or debugging.

A

(a) => void

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

pipe(These.make.first(5), These.tap(console.log)); // logs 5, returns First(5)