Skip to main content

Overview

The extract function allows you to “extract” service dependencies from an Effect function, moving those dependencies from the return type to the calling context. This is essential when building service containers that expose methods which internally depend on other services.

Signature

Parameters

(...params: P) => Effect.Effect<A, E, R>
required
The Effect function whose dependencies should be extracted. The function can accept any number of parameters.
Array<Context.Tag<any, any>>
Optional array of service tags to exclude from extraction. Excluded services remain as requirements in the returned function.

Returns

Returns an Effect that yields a new function with the same signature as the input, but with service dependencies moved to the outer Effect’s requirements.
  • Input function type: (...params: P) => Effect.Effect<A, E, R>
  • Output function type: (...params: P) => Effect.Effect<A, E, Extract<R, EXCLUDED>>
  • Effect requirements: Exclude<R, EXCLUDED>

Basic Usage

Without extract

Here’s what happens when you don’t use extract:

With extract

extract solves this by capturing the dependencies when the container is created:
The dependencies are captured at the time the extracted function is created, not when it’s called. This allows the returned function to be called without requiring those services in its context.

Excluding Services

Sometimes you want to extract most dependencies but keep some as runtime requirements. Use the exclude option:

Type Signature with Exclusions

When using exclude, the type signature changes:

Real-World Example

Here’s a practical example using extract to build a user service:

Common Patterns

Service Containers

Use extract when building service containers that expose multiple methods:

Selective Extraction

Mix extracted and non-extracted methods based on your needs:

See Also