Hyperlinkv0.8.0-beta.28

Option

Option.toRefinementconsteffect/Option.ts:480
<A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is B

Converts an Option-returning function into a type guard (refinement).

When to use

Use when you need to turn an Option-returning parser into a type-narrowing predicate, such as for Array.prototype.filter.

Details

  • Returns true when the original function returns Some
  • Returns false when the original function returns None
  • Narrows the input type to B on success

Example (Converting a parser to a type guard)

import { Option } from "effect"

type MyData = string | number

const parseString = (data: MyData): Option.Option<string> =>
  typeof data === "string" ? Option.some(data) : Option.none()

//      ┌─── (a: MyData) => a is string
//      ▼
const isString = Option.toRefinement(parseString)

console.log(isString("a"))
// Output: true

console.log(isString(1))
// Output: false
convertingliftPredicate
Source effect/Option.ts:4801 lines
export const toRefinement = <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is B => (a: A): a is B => isSome(f(a))