<Services, I extends Services, S>(service: Key<I, S>): (
self: Context<Services>
) => S
<Services, I extends Services, S>(
self: Context<Services>,
service: Key<I, S>
): SGets a service from the context that corresponds to the given key.
When to use
Use when you need type-checked access to a service already included in the context type.
Example (Getting a service from a context)
import { Context, pipe } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const Timeout = Context.Service<{ TIMEOUT: number }>("Timeout")
const context = pipe(
Context.make(Port, { PORT: 8080 }),
Context.add(Timeout, { TIMEOUT: 5000 })
)
assert.deepStrictEqual(Context.get(context, Timeout), { TIMEOUT: 5000 })export const const get: {
<Services, I extends Services, S>(
service: Key<I, S>
): (self: Context<Services>) => S
<Services, I extends Services, S>(
self: Context<Services>,
service: Key<I, S>
): S
}
Gets a service from the context that corresponds to the given key.
When to use
Use when you need type-checked access to a service already included in the
context type.
Example (Getting a service from a context)
import { Context, pipe } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const Timeout = Context.Service<{ TIMEOUT: number }>("Timeout")
const context = pipe(
Context.make(Port, { PORT: 8080 }),
Context.add(Timeout, { TIMEOUT: 5000 })
)
assert.deepStrictEqual(Context.get(context, Timeout), { TIMEOUT: 5000 })
get: {
<function (type parameter) Services in <Services, I extends Services, S>(service: Key<I, S>): (self: Context<Services>) => SServices, function (type parameter) I in <Services, I extends Services, S>(service: Key<I, S>): (self: Context<Services>) => SI extends function (type parameter) Services in <Services, I extends Services, S>(service: Key<I, S>): (self: Context<Services>) => SServices, function (type parameter) S in <Services, I extends Services, S>(service: Key<I, S>): (self: Context<Services>) => SS>(service: 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: interface Key<out Identifier, out Shape>Typed identifier for a service stored in a Context.
When to use
Use as the typed handle for storing, retrieving, and requiring a specific
service in a Context.
Details
Identifier tracks the requirement in Effect types, while Shape is the
service implementation retrieved by the key. A key is also an Effect value,
so yielding it inside Effect.gen retrieves the service from the current
fiber context.
Key<function (type parameter) I in <Services, I extends Services, S>(service: Key<I, S>): (self: Context<Services>) => SI, function (type parameter) S in <Services, I extends Services, S>(service: Key<I, S>): (self: Context<Services>) => SS>): (self: Context<Services>(parameter) self: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
self: interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) Services in <Services, I extends Services, S>(service: Key<I, S>): (self: Context<Services>) => SServices>) => function (type parameter) S in <Services, I extends Services, S>(service: Key<I, S>): (self: Context<Services>) => SS
<function (type parameter) Services in <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): SServices, function (type parameter) I in <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): SI extends function (type parameter) Services in <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): SServices, function (type parameter) S in <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): SS>(self: Context<Services>(parameter) self: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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;
}
self: interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) Services in <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): SServices>, service: 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: interface Key<out Identifier, out Shape>Typed identifier for a service stored in a Context.
When to use
Use as the typed handle for storing, retrieving, and requiring a specific
service in a Context.
Details
Identifier tracks the requirement in Effect types, while Shape is the
service implementation retrieved by the key. A key is also an Effect value,
so yielding it inside Effect.gen retrieves the service from the current
fiber context.
Key<function (type parameter) I in <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): SI, function (type parameter) S in <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): SS>): function (type parameter) S in <Services, I extends Services, S>(self: Context<Services>, service: Key<I, S>): SS
} = const getUnsafe: {
<S, I>(service: Key<I, S>): <Services>(
self: Context<Services>
) => S
<Services, S, I>(
self: Context<Services>,
services: Key<I, S>
): S
}
Gets the service for a key, throwing if an absent non-reference key cannot be
resolved.
When to use
Use when you need to read a service from a context whose type does not prove
the service is present.
Details
If the key is a Context.Reference and no override is stored in the
context, its cached default value is returned. For absent non-reference keys,
this function throws a runtime error.
Example (Getting services unsafely)
import { Context } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const Timeout = Context.Service<{ TIMEOUT: number }>("Timeout")
const context = Context.make(Port, { PORT: 8080 })
assert.deepStrictEqual(Context.getUnsafe(context, Port), { PORT: 8080 })
assert.throws(() => Context.getUnsafe(context, Timeout))
getUnsafe