Skip to content

The Effect Type

The Effect type represents an immutable value that lazily describes a workflow or job:

Effect<Success, Error, Requirements>

This type encapsulates the logic of a program, defining whether it succeeds, providing a value of type Success, or fails, resulting in an error of type Error. Additionally, the program requires a collection of contextual data Context<Requirements> to execute.

Conceptually, you can think of Effect as an effectful version of the following function type:

type Effect<Success, Error, Requirements> = (
context: Context<Requirements>
) => Error | Success

However, effects are not actually functions. They can model synchronous, asynchronous, concurrent, and resourceful computations.

The Effect type has three type parameters with the following meanings:

ParameterDescription
SuccessRepresents the type of value that an effect can succeed with when executed. If this type parameter is void, it means the effect produces no useful information, while if it is never, it means the effect runs forever (or until failure).
ErrorRepresents the expected errors that can occur when executing an effect. If this type parameter is never, it means the effect cannot fail, because there are no values of type never.
RequirementsRepresents the contextual data required by the effect to be executed. This data is stored in a collection named Context. If this type parameter is never, it means the effect has no requirements and the Context collection is empty.

Immutability. Effect values are immutable, and every function in the Effect library produces a new Effect value.

Modeling Interactions. These values do not perform any actions themselves, they simply model or describe effectful interactions.

Execution. An Effect can be executed by the Effect Runtime System, which interprets it into actual interactions with the external world. Ideally, this execution happens at a single entry point in your application, such as the main function where effectful operations are initiated.

By using the utility types Effect.Success, Effect.Error, and Effect.Context, you can extract the corresponding types from an effect.

Example (Extracting Success, Error, and Context Types)

1
import {
import Effect
Effect
,
import Context
Context
} from "effect"
2
3
class
class SomeContext
SomeContext
extends
import Context
Context
.
const Tag: <"SomeContext">(id: "SomeContext") => <Self, Shape>() => Context.TagClass<Self, "SomeContext", Shape> namespace Tag
Tag
("SomeContext")<
class SomeContext
SomeContext
, {}>() {}
4
5
// Assume we have an effect that succeeds with a number,
6
// fails with a string, and requires SomeContext
7
declare const
const program: Effect.Effect<number, string, SomeContext>
program
:
import Effect
Effect
.
interface Effect<out A, out E = never, out R = never> namespace Effect

The `Effect` interface defines a value that lazily describes a workflow or job. The workflow requires some context `R`, and may fail with an error of type `E`, or succeed with a value of type `A`. `Effect` values model resourceful interaction with the outside world, including synchronous, asynchronous, concurrent, and parallel interaction. They use a fiber-based concurrency model, with built-in support for scheduling, fine-grained interruption, structured concurrency, and high scalability. To run an `Effect` value, you need a `Runtime`, which is a type that is capable of executing `Effect` values.

Effect
<number, string,
class SomeContext
SomeContext
>
8
9
// Extract the success type, which is number
10
type
type S = number
S
=
import Effect
Effect
.
namespace Effect

The `Effect` interface defines a value that lazily describes a workflow or job. The workflow requires some context `R`, and may fail with an error of type `E`, or succeed with a value of type `A`. `Effect` values model resourceful interaction with the outside world, including synchronous, asynchronous, concurrent, and parallel interaction. They use a fiber-based concurrency model, with built-in support for scheduling, fine-grained interruption, structured concurrency, and high scalability. To run an `Effect` value, you need a `Runtime`, which is a type that is capable of executing `Effect` values.

Effect
.
type Effect<out A, out E = never, out R = never>.Success<T extends Effect.Effect<any, any, any>> = [T] extends [Effect.Effect<infer _A, infer _E, infer _R>] ? _A : never
Success
<typeof
const program: Effect.Effect<number, string, SomeContext>
program
>
11
12
// Extract the error type, which is string
13
type
type E = string
E
=
import Effect
Effect
.
namespace Effect

The `Effect` interface defines a value that lazily describes a workflow or job. The workflow requires some context `R`, and may fail with an error of type `E`, or succeed with a value of type `A`. `Effect` values model resourceful interaction with the outside world, including synchronous, asynchronous, concurrent, and parallel interaction. They use a fiber-based concurrency model, with built-in support for scheduling, fine-grained interruption, structured concurrency, and high scalability. To run an `Effect` value, you need a `Runtime`, which is a type that is capable of executing `Effect` values.

Effect
.
type Effect<out A, out E = never, out R = never>.Error<T extends Effect.Effect<any, any, any>> = [T] extends [Effect.Effect<infer _A, infer _E, infer _R>] ? _E : never
Error
<typeof
const program: Effect.Effect<number, string, SomeContext>
program
>
14
15
// Extract the context type, which is SomeContext
16
type
type C = SomeContext
C
=
import Effect
Effect
.
namespace Effect

The `Effect` interface defines a value that lazily describes a workflow or job. The workflow requires some context `R`, and may fail with an error of type `E`, or succeed with a value of type `A`. `Effect` values model resourceful interaction with the outside world, including synchronous, asynchronous, concurrent, and parallel interaction. They use a fiber-based concurrency model, with built-in support for scheduling, fine-grained interruption, structured concurrency, and high scalability. To run an `Effect` value, you need a `Runtime`, which is a type that is capable of executing `Effect` values.

Effect
.
type Effect<out A, out E = never, out R = never>.Context<T extends Effect.Effect<any, any, any>> = [T] extends [Effect.Effect<infer _A, infer _E, infer _R>] ? _R : never
Context
<typeof
const program: Effect.Effect<number, string, SomeContext>
program
>