<I, S>(key: Key<I, S>, service: Types.NoInfer<S>): Context<I>Creates a new Context with a single service associated to the key.
Example (Creating a context with one service)
import { Context } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const context = Context.make(Port, { PORT: 8080 })
assert.deepStrictEqual(Context.get(context, Port), { PORT: 8080 })export const const make: <I, S>(
key: Key<I, S>,
service: Types.NoInfer<S>
) => Context<I>
Creates a new Context with a single service associated to the key.
Example (Creating a context with one service)
import { Context } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const context = Context.make(Port, { PORT: 8080 })
assert.deepStrictEqual(Context.get(context, Port), { PORT: 8080 })
make = <function (type parameter) I in <I, S>(key: Key<I, S>, service: Types.NoInfer<S>): Context<I>I, function (type parameter) S in <I, S>(key: Key<I, S>, service: Types.NoInfer<S>): Context<I>S>(
key: Key<I, S>(parameter) key: {
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;
}
key: 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 <I, S>(key: Key<I, S>, service: Types.NoInfer<S>): Context<I>I, function (type parameter) S in <I, S>(key: Key<I, S>, service: Types.NoInfer<S>): Context<I>S>,
service: Types.NoInfer<S>service: 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>(key: Key<I, S>, service: Types.NoInfer<S>): Context<I>S>
): 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) I in <I, S>(key: Key<I, S>, service: Types.NoInfer<S>): Context<I>I> => const makeUnsafe: <Services = never>(
mapUnsafe: ReadonlyMap<string, any>
) => Context<Services>
Creates a Context from an existing service map.
When to use
Use when constructing a low-level Context from a trusted map whose lifecycle
you control.
Gotchas
This is unsafe because later mutation of the provided map can affect the
created Context. Prefer empty, make, add, or merge for normal
Context construction.
Example (Creating a context from a map)
import { Context } from "effect"
// Create a context from a Map (unsafe)
const map = new Map([
["Logger", { log: (msg: string) => console.log(msg) }]
])
const context = Context.makeUnsafe(map)
makeUnsafe(new var Map: MapConstructor
new <string, Types.NoInfer<S>>(iterable?: Iterable<readonly [string, Types.NoInfer<S>]> | null | undefined) => Map<string, Types.NoInfer<S>> (+3 overloads)
Map([[key: Key<I, S>(parameter) key: {
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;
}
key.Key<out Identifier, out Shape>.key: stringkey, service: Types.NoInfer<S>service]]))