<I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>
<I, S>(
service: Context.Key<I, S>,
evaluate: LazyArg<Types.NoInfer<S>>
): Layer<I>Constructs a layer lazily that provides a single service.
When to use
Use when you need a Layer that provides one service whose value is created
synchronously, but creation should be deferred until the layer is built.
Details
This is a lazy version of succeed where the service value is computed
synchronously only when the layer is built.
Example (Lazily providing a service)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layer = Layer.sync(Database, () => ({
query: (sql: string) => Effect.succeed(`Query: ${sql}`)
}))export const const sync: {
<I, S>(service: Context.Key<I, S>): (
evaluate: LazyArg<S>
) => Layer<I>
<I, S>(
service: Context.Key<I, S>,
evaluate: LazyArg<Types.NoInfer<S>>
): Layer<I>
}
Constructs a layer lazily that provides a single service.
When to use
Use when you need a Layer that provides one service whose value is created
synchronously, but creation should be deferred until the layer is built.
Details
This is a lazy version of succeed where the service value is computed
synchronously only when the layer is built.
Example (Lazily providing a service)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layer = Layer.sync(Database, () => ({
query: (sql: string) => Effect.succeed(`Query: ${sql}`)
}))
sync: {
<function (type parameter) I in <I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>I, function (type parameter) S in <I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>S>(service: Context.Key<I, S>(parameter) service: {
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service: import ContextContext.type Context.Key = /*unresolved*/ anyKey<function (type parameter) I in <I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>I, function (type parameter) S in <I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>S>): (evaluate: LazyArg<S>evaluate: import LazyArgLazyArg<function (type parameter) S in <I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>S>) => interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) I in <I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>I>
<function (type parameter) I in <I, S>(service: Context.Key<I, S>, evaluate: LazyArg<Types.NoInfer<S>>): Layer<I>I, function (type parameter) S in <I, S>(service: Context.Key<I, S>, evaluate: LazyArg<Types.NoInfer<S>>): Layer<I>S>(service: Context.Key<I, S>(parameter) service: {
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service: import ContextContext.type Context.Key = /*unresolved*/ anyKey<function (type parameter) I in <I, S>(service: Context.Key<I, S>, evaluate: LazyArg<Types.NoInfer<S>>): Layer<I>I, function (type parameter) S in <I, S>(service: Context.Key<I, S>, evaluate: LazyArg<Types.NoInfer<S>>): Layer<I>S>, evaluate: LazyArg<Types.NoInfer<S>>evaluate: import LazyArgLazyArg<import TypesTypes.type NoInfer<A> = [A][A extends any
? 0
: never]
Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) S in <I, S>(service: Context.Key<I, S>, evaluate: LazyArg<Types.NoInfer<S>>): Layer<I>S>>): interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<function (type parameter) I in <I, S>(service: Context.Key<I, S>, evaluate: LazyArg<Types.NoInfer<S>>): Layer<I>I>
} = function() {
if (function (local var) arguments: IArgumentsarguments.IArguments.length: numberlength === 1) {
return (evaluate: LazyArg<any>evaluate: import LazyArgLazyArg<any>) => const syncContext: <A>(
evaluate: LazyArg<Context.Context<A>>
) => Layer<A>
Constructs a layer lazily that provides all services in a Context.
When to use
Use when you need a Layer that creates multiple services synchronously but
defers that work until the layer is built.
Details
This is a lazy version of succeedContext where the Context is computed
synchronously only when the layer is built.
Example (Lazily providing a context)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layer = Layer.syncContext(() =>
Context.make(Database, {
query: (sql: string) => Effect.succeed(`Query: ${sql}`)
})
)
syncContext(() => import ContextContext.make(function (local var) arguments: IArgumentsarguments[0], evaluate: LazyArg<any>evaluate()))
}
return const syncContext: <A>(
evaluate: LazyArg<Context.Context<A>>
) => Layer<A>
Constructs a layer lazily that provides all services in a Context.
When to use
Use when you need a Layer that creates multiple services synchronously but
defers that work until the layer is built.
Details
This is a lazy version of succeedContext where the Context is computed
synchronously only when the layer is built.
Example (Lazily providing a context)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layer = Layer.syncContext(() =>
Context.make(Database, {
query: (sql: string) => Effect.succeed(`Query: ${sql}`)
})
)
syncContext(() => import ContextContext.make(function (local var) arguments: IArgumentsarguments[0], function (local var) arguments: IArgumentsarguments[1]()))
} as any