Hyperlinkv0.8.0-beta.28

Predicate

Predicate.andconsteffect/Predicate.ts:1621
<A, C extends A>(that: Refinement<A, C>): <B extends A>(
  self: Refinement<A, B>
) => Refinement<A, B & C>
<A, B extends A, C extends A>(
  self: Refinement<A, B>,
  that: Refinement<A, C>
): Refinement<A, B & C>
<A>(that: Predicate<A>): (self: Predicate<A>) => Predicate<A>
<A>(self: Predicate<A>, that: Predicate<A>): Predicate<A>

Creates a predicate that returns true only if both predicates are true.

When to use

Use when you want to combine Predicates with AND, accepting values that satisfy multiple conditions, including refinements that narrow to an intersection.

Details

Evaluation short-circuits on the first false. For refinements, the output type is an intersection.

Example (Checking both conditions)

import { Predicate } from "effect"

const hasAAndB = Predicate.and(
  Predicate.hasProperty("a"),
  Predicate.hasProperty("b")
)

const input: unknown = JSON.parse(`{"a":1,"b":"ok"}`)
if (hasAAndB(input)) {
  // input has both properties at this point
  const a = input.a
  const b = input.b
}
combinatorsornot
export const and: {
  <A, C extends A>(that: Refinement<A, C>): <B extends A>(self: Refinement<A, B>) => Refinement<A, B & C>
  <A, B extends A, C extends A>(self: Refinement<A, B>, that: Refinement<A, C>): Refinement<A, B & C>
  <A>(that: Predicate<A>): (self: Predicate<A>) => Predicate<A>
  <A>(self: Predicate<A>, that: Predicate<A>): Predicate<A>
} = dual(2, <A>(self: Predicate<A>, that: Predicate<A>): Predicate<A> => (a) => self(a) && that(a))