<A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BConverts 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
truewhen the original function returnsSome - Returns
falsewhen the original function returnsNone - Narrows the input type to
Bon 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: falseexport const const toRefinement: <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
toRefinement = <function (type parameter) A in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BA, function (type parameter) B in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BB extends function (type parameter) A in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BA>(f: (a: A) => Option<B>f: (a: Aa: function (type parameter) A in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BA) => type Option<A> = None<A> | Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BB>): (a: Aa: function (type parameter) A in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BA) => a: Aa is function (type parameter) B in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BB => (a: Aa: function (type parameter) A in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BA): a: Aa is function (type parameter) B in <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is BB => const isSome: <A>(
self: Option<A>
) => self is Some<A>
Checks whether an Option contains a value (Some).
When to use
Use when you need to branch on a present Option before accessing .value.
Details
- Acts as a type guard, narrowing to
Some<A>
Example (Checking for Some)
import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false
isSome(f: (a: A) => Option<B>f(a: Aa))