<S extends ReadonlyArray<Key<any, any>>>(...keys: S): <Services>(
self: Context<Services>
) => Context<Exclude<Services, Service.Identifier<S[number]>>>Returns a new Context with the specified service keys removed.
When to use
Use when you want to remove a denylist of services from a Context.
Example (Omitting services from a context)
import { Context, Option, pipe } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const Timeout = Context.Service<{ TIMEOUT: number }>("Timeout")
const someContext = pipe(
Context.make(Port, { PORT: 8080 }),
Context.add(Timeout, { TIMEOUT: 5000 })
)
const context = pipe(someContext, Context.omit(Timeout))
assert.deepStrictEqual(
Context.getOption(context, Port),
Option.some({ PORT: 8080 })
)
assert.deepStrictEqual(Context.getOption(context, Timeout), Option.none())export const const omit: <
S extends ReadonlyArray<Key<any, any>>
>(
...keys: S
) => <Services>(
self: Context<Services>
) => Context<
Exclude<Services, Service.Identifier<S[number]>>
>
Returns a new Context with the specified service keys removed.
When to use
Use when you want to remove a denylist of services from a Context.
Example (Omitting services from a context)
import { Context, Option, pipe } from "effect"
import * as assert from "node:assert"
const Port = Context.Service<{ PORT: number }>("Port")
const Timeout = Context.Service<{ TIMEOUT: number }>("Timeout")
const someContext = pipe(
Context.make(Port, { PORT: 8080 }),
Context.add(Timeout, { TIMEOUT: 5000 })
)
const context = pipe(someContext, Context.omit(Timeout))
assert.deepStrictEqual(
Context.getOption(context, Port),
Option.some({ PORT: 8080 })
)
assert.deepStrictEqual(Context.getOption(context, Timeout), Option.none())
omit = <function (type parameter) S in <S extends ReadonlyArray<Key<any, any>>>(...keys: S): <Services>(self: Context<Services>) => Context<Exclude<Services, Service.Identifier<S[number]>>>S extends interface ReadonlyArray<T>ReadonlyArray<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<any, any>>>(
...keys: S extends ReadonlyArray<Key<any, any>>keys: function (type parameter) S in <S extends ReadonlyArray<Key<any, any>>>(...keys: S): <Services>(self: Context<Services>) => Context<Exclude<Services, Service.Identifier<S[number]>>>S
) =>
<function (type parameter) Services in <Services>(self: Context<Services>): Context<Exclude<Services, Service.Identifier<S[number]>>>Services>(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>(self: Context<Services>): Context<Exclude<Services, Service.Identifier<S[number]>>>Services>): 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<type Exclude<T, U> = T extends U
? never
: T
Exclude from T those types that are assignable to U
Exclude<function (type parameter) Services in <Services>(self: Context<Services>): Context<Exclude<Services, Service.Identifier<S[number]>>>Services, Service.type Service<in out Identifier, in out Shape>.Identifier<T> = T extends Key<infer I, infer _S> ? I : neverExtracts the identifier, or requirement type, associated with a Context
service key.
Example (Extracting a service identifier)
import { Context } from "effect"
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
// Extract the identifier type from a key
type DatabaseId = Context.Service.Identifier<typeof Database>
// DatabaseId is the identifier type
Identifier<function (type parameter) S in <S extends ReadonlyArray<Key<any, any>>>(...keys: S): <Services>(self: Context<Services>) => Context<Exclude<Services, Service.Identifier<S[number]>>>S[number]>>> =>
const withMapUnsafe: <Services, B>(
self: Context<Services>,
f: (map: Map<string, any>) => void
) => Context<B>
withMapUnsafe(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, (map: Map<string, any>map) => {
for (let let i: numberi = 0; let i: numberi < keys: S extends ReadonlyArray<Key<any, any>>keys.ReadonlyArray<Key<any, any>>.length: numberGets the length of the array. This is a number one higher than the highest element defined in an array.
length; let i: numberi++) {
map: Map<string, any>map.Map<string, any>.delete(key: string): booleandelete(keys: S extends ReadonlyArray<Key<any, any>>keys[let i: numberi].Key<out Identifier, out Shape>.key: stringkey)
}
})