Skip to content

Boolean.ts

Works with TypeScript boolean values.

This module exposes the native Boolean constructor together with helpers for checking values, choosing between lazy branches, combining booleans with logical operations, checking collections with every or some, ordering booleans, and reducing boolean values.

Since v2.0.0



Combines two booleans using logical AND: self && that.

When to use

Use to require both boolean operands to be true.

Details

Supports both data-first and data-last forms.

Example (Combining booleans with AND)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.and(true, true), true)
assert.deepStrictEqual(Boolean.and(true, false), false)
assert.deepStrictEqual(Boolean.and(false, true), false)
assert.deepStrictEqual(Boolean.and(false, false), false)

Signature

declare const and: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Source

Since v2.0.0

Combines two booleans using EQV (aka XNOR): !xor(self, that).

When to use

Use to accept when both boolean operands have the same truth value.

Example (Checking boolean equivalence)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.eqv(true, true), true)
assert.deepStrictEqual(Boolean.eqv(true, false), false)
assert.deepStrictEqual(Boolean.eqv(false, true), false)
assert.deepStrictEqual(Boolean.eqv(false, false), true)

Signature

declare const eqv: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Source

Since v2.0.0

Combines two booleans using an implication: (!self || that).

When to use

Use to model logical implication between a condition and a consequence.

Example (Checking boolean implication)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.implies(true, true), true)
assert.deepStrictEqual(Boolean.implies(true, false), false)
assert.deepStrictEqual(Boolean.implies(false, true), true)
assert.deepStrictEqual(Boolean.implies(false, false), true)

Signature

declare const implies: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Source

Since v2.0.0

Combines two booleans using NAND: !(self && that).

When to use

Use to negate a logical AND result.

Example (Combining booleans with NAND)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.nand(true, true), false)
assert.deepStrictEqual(Boolean.nand(true, false), true)
assert.deepStrictEqual(Boolean.nand(false, true), true)
assert.deepStrictEqual(Boolean.nand(false, false), true)

Signature

declare const nand: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Source

Since v2.0.0

Combines two booleans using NOR: !(self || that).

When to use

Use to accept only when both boolean operands are false.

Example (Combining booleans with NOR)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.nor(true, true), false)
assert.deepStrictEqual(Boolean.nor(true, false), false)
assert.deepStrictEqual(Boolean.nor(false, true), false)
assert.deepStrictEqual(Boolean.nor(false, false), true)

Signature

declare const nor: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Source

Since v2.0.0

Negates the given boolean: !self

When to use

Use to invert a boolean value.

Example (Negating booleans)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.not(true), false)
assert.deepStrictEqual(Boolean.not(false), true)

Signature

declare const not: (self: boolean) => boolean

Source

Since v2.0.0

Combines two booleans using OR: self || that.

When to use

Use to accept when either boolean operand is true.

Example (Combining booleans with OR)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.or(true, true), true)
assert.deepStrictEqual(Boolean.or(true, false), true)
assert.deepStrictEqual(Boolean.or(false, true), true)
assert.deepStrictEqual(Boolean.or(false, false), false)

Signature

declare const or: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Source

Since v2.0.0

Combines two booleans using XOR: (!self && that) || (self && !that).

When to use

Use to accept when exactly one boolean operand is true.

Example (Combining booleans with XOR)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.xor(true, true), false)
assert.deepStrictEqual(Boolean.xor(true, false), true)
assert.deepStrictEqual(Boolean.xor(false, true), true)
assert.deepStrictEqual(Boolean.xor(false, false), false)

Signature

declare const xor: { (that: boolean): (self: boolean) => boolean; (self: boolean, that: boolean): boolean }

Source

Since v2.0.0

Exposes the global boolean constructor for JavaScript truthiness coercion.

When to use

Use to access native JavaScript truthiness coercion from the Effect module namespace.

Gotchas

This follows native truthiness rules. For example, non-empty strings such as "false" coerce to true.

Example (Coercing values to booleans)

import { Boolean } from "effect"
const bool = Boolean.Boolean(1)
console.log(bool) // true
const fromString = Boolean.Boolean("false")
console.log(fromString) // true (non-empty string)
const fromZero = Boolean.Boolean(0)
console.log(fromZero) // false

Signature

declare const Boolean: BooleanConstructor

Source

Since v4.0.0

Checks whether a value is a boolean.

When to use

Use to validate unknown input and narrow it to boolean.

Example (Checking for booleans)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.isBoolean(true), true)
assert.deepStrictEqual(Boolean.isBoolean("true"), false)

Signature

declare const isBoolean: (input: unknown) => input is boolean

Source

Since v2.0.0

Equivalence instance for booleans using strict equality (===).

When to use

Use when checking boolean equality through APIs that accept an equivalence relation.

Example (Comparing booleans for equivalence)

import { Boolean } from "effect"
console.log(Boolean.Equivalence(true, true)) // true
console.log(Boolean.Equivalence(true, false)) // false

Signature

declare const Equivalence: Equ.Equivalence<boolean>

Source

Since v2.0.0

Provides an Order instance for boolean that allows comparing and sorting boolean values. In this ordering, false is considered less than true.

When to use

Use when you need to sort or compare boolean values through APIs that accept an ordering instance where false comes before true.

Example (Comparing booleans)

import { Boolean } from "effect"
console.log(Boolean.Order(false, true)) // -1 (false < true)
console.log(Boolean.Order(true, false)) // 1 (true > false)
console.log(Boolean.Order(true, true)) // 0 (true === true)

Signature

declare const Order: order.Order<boolean>

Source

Since v2.0.0

Reducer for combining booleans using AND.

When to use

Use to require every accumulated boolean to be true through APIs that consume a Reducer.

Details

The initialValue is true, so combineAll([]) returns true.

Gotchas

combineAll uses the default left-to-right Reducer.make fold and does not short-circuit on false.

See

  • ReducerOr for reducing with OR semantics
  • every for checking an iterable directly

Signature

declare const ReducerAnd: Reducer.Reducer<boolean>

Source

Since v4.0.0

Reducer for combining booleans using OR.

When to use

Use to reduce boolean values where the result should be true if any combined value is true.

Details

The initialValue is false.

See

  • ReducerAnd for reducing with AND semantics
  • some for checking an iterable directly

Signature

declare const ReducerOr: Reducer.Reducer<boolean>

Source

Since v4.0.0

Chooses between two lazy branches based on a boolean value.

When to use

Use to choose between two lazy branches based on a boolean value.

Example (Pattern matching on booleans)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(
Boolean.match(true, {
onFalse: () => "It's false!",
onTrue: () => "It's true!"
}),
"It's true!"
)

Signature

declare const match: {
<A, B = A>(options: { readonly onFalse: LazyArg<A>; readonly onTrue: LazyArg<B> }): (value: boolean) => A | B
<A, B>(value: boolean, options: { readonly onFalse: LazyArg<A>; readonly onTrue: LazyArg<B> }): A | B
}

Source

Since v2.0.0

Checks whether every boolean in a collection is true.

When to use

Use to check that every boolean in an iterable is true.

Example (Checking every boolean)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.every([true, true, true]), true)
assert.deepStrictEqual(Boolean.every([true, false, true]), false)

See

  • some for checking whether at least one value is true
  • ReducerAnd for reducing booleans with AND through a Reducer

Signature

declare const every: (collection: Iterable<boolean>) => boolean

Source

Since v2.0.0

Checks whether at least one boolean in a collection is true.

When to use

Use to check that at least one boolean in an iterable is true.

Example (Checking some booleans)

import { Boolean } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Boolean.some([true, false, true]), true)
assert.deepStrictEqual(Boolean.some([false, false, false]), false)

See

  • every for checking whether all values are true
  • ReducerOr for reducing booleans with OR through a Reducer

Signature

declare const some: (collection: Iterable<boolean>) => boolean

Source

Since v2.0.0