(input: Input): Effect<Result.Result<Pass, Fail>, E, R>Represents an effectful filter function that can produce Effects.
Details
Similar to a regular Filter, but the filtering operation itself can be
effectful, allowing for asynchronous operations, error handling, and
dependency injection.
Example (Defining an effectful user filter)
import { Effect, Filter, Result } from "effect"
// An effectful filter that validates user data
type User = { id: string; isActive: boolean }
type ValidationError = { message: string }
const validateUser: Filter.FilterEffect<
string,
User,
User,
ValidationError,
never
> = (id) =>
Effect.gen(function*() {
const user: User = { id, isActive: id.length > 0 }
return user.isActive ? Result.succeed(user) : Result.fail(user)
})export interface interface FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>Represents an effectful filter function that can produce Effects.
Details
Similar to a regular Filter, but the filtering operation itself can be
effectful, allowing for asynchronous operations, error handling, and
dependency injection.
Example (Defining an effectful user filter)
import { Effect, Filter, Result } from "effect"
// An effectful filter that validates user data
type User = { id: string; isActive: boolean }
type ValidationError = { message: string }
const validateUser: Filter.FilterEffect<
string,
User,
User,
ValidationError,
never
> = (id) =>
Effect.gen(function*() {
const user: User = { id, isActive: id.length > 0 }
return user.isActive ? Result.succeed(user) : Result.fail(user)
})
FilterEffect<
in function (type parameter) Input in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>Input,
out function (type parameter) Pass in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>Pass,
out function (type parameter) Fail in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>Fail,
out function (type parameter) E in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>E = never,
out function (type parameter) R in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>R = never
> {
(input: in Inputinput: function (type parameter) Input in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>Input): import EffectEffect<import ResultResult.type Result<A, E = never> = Result.Success<A, E> | Result.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) Pass in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>Pass, function (type parameter) Fail in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>Fail>, function (type parameter) E in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>E, function (type parameter) R in FilterEffect<in Input, out Pass, out Fail, out E = never, out R = never>R>
}