Skip to content

toResult

toResult<A, B, E>(r, onFail): (a) => Result<E, B>

Defined in: Core/Refinement.ts:152

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.

A

B

E

Refinement<A, B>

(a) => E

(a): Result<E, B>

A

Result<E, B>

type NonEmptyString = string & { readonly _tag: "NonEmpty" };
const isNonEmpty: Refinement<string, NonEmptyString> =
  Refinement.make(s => s.length > 0);

pipe("", Refinement.toResult(isNonEmpty, () => "must not be empty")); // Err(...)
pipe("hi", Refinement.toResult(isNonEmpty, () => "must not be empty")); // Ok("hi")