Refinement
Refinement:
object
Defined in: Core/Refinement.ts:25
Type Declaration
Section titled “Type Declaration”and: <
A,C>(second) => <B>(first) =>Refinement<A,B&C>
Intersects two refinements: the result narrows A to B & C, passing only
when both refinements hold simultaneously.
Data-last — the first refinement is the data being piped.
Type Parameters
Section titled “Type Parameters”A
C
Parameters
Section titled “Parameters”second
Section titled “second”Refinement<A, C>
Returns
Section titled “Returns”<B>(first) => Refinement<A, B & C>
Example
Section titled “Example”compose
Section titled “compose”compose: <
A,B,C>(bc) => (ab) =>Refinement<A,C>
Chains two refinements: if ab narrows A to B and bc narrows B to C,
the result narrows A directly to C.
Data-last — the first refinement ab is the data being piped.
Type Parameters
Section titled “Type Parameters”A
B
C
Parameters
Section titled “Parameters”Refinement<B, C>
Returns
Section titled “Returns”(ab) => Refinement<A, C>
Example
Section titled “Example”from:
object
from.predicate
Section titled “from.predicate”predicate: <
A,B>(f) =>Refinement<A,B> =fromPredicate
Creates a Refinement<A, B> from a plain boolean predicate.
This is an unsafe cast — the caller is responsible for ensuring that the
predicate truly characterises values of type B. Use this only when
bootstrapping a new refinement; prefer compose, and, or or to build
derived refinements from existing ones.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”(a) => boolean
Returns
Section titled “Returns”Refinement<A, B>
Example
Section titled “Example”or: <
A,C>(second) => <B>(first) =>Refinement<A,C|B>
Unions two refinements: the result narrows A to B | C, passing when either
refinement holds.
Data-last — the first refinement is the data being piped.
Type Parameters
Section titled “Type Parameters”A
C
Parameters
Section titled “Parameters”second
Section titled “second”Refinement<A, C>
Returns
Section titled “Returns”<B>(first) => Refinement<A, C | B>
Example
Section titled “Example”to:
object
to.Maybe
Section titled “to.Maybe”Maybe: <
A,B>(r) => (a) =>Maybe<B>
Converts a Refinement<A, B> into a function (a: A) => Maybe<B>.
Returns Some(a) when the refinement holds, None otherwise. Useful for
integrating runtime validation into a Maybe-based pipeline.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”Refinement<A, B>
Returns
Section titled “Returns”(a) => Maybe<B>
Example
Section titled “Example”to.Result
Section titled “to.Result”Result: <
A,B,E>(r,onFail) => (a) =>Result<E,B>
Converts a Refinement<A, B> into a function (a: A) => Result<E, B>.
Returns Ok(a) when the refinement holds, Err(onFail(a)) otherwise. Use
this to surface validation failures as typed errors inside a Result pipeline.
Type Parameters
Section titled “Type Parameters”A
B
E
Parameters
Section titled “Parameters”Refinement<A, B>
onFail
Section titled “onFail”(a) => E
Returns
Section titled “Returns”(a) => Result<E, B>