ServiceClass<Self, Identifier, Shape>Class-style service key produced by Context.Service<Self, Shape>()("Id").
When to use
Use when declaring a service as a class so the class value can serve as the
Context key.
Details
The class itself is the Context key, and its string key identifies the
service at runtime.
export interface interface ServiceClass<in out Self, in out Identifier extends string, in out Shape>Class-style service key produced by Context.Service<Self, Shape>()("Id").
When to use
Use when declaring a service as a class so the class value can serve as the
Context key.
Details
The class itself is the Context key, and its string key identifies the
service at runtime.
Namespace containing helper types for class-style Context.Service
declarations.
ServiceClass<in out function (type parameter) Self in ServiceClass<in out Self, in out Identifier extends string, in out Shape>Self, in out function (type parameter) Identifier in ServiceClass<in out Self, in out Identifier extends string, in out Shape>Identifier extends string, in out function (type parameter) Shape in ServiceClass<in out Self, in out Identifier extends string, in out Shape>Shape>
extends interface Service<in out Identifier, in out Shape>Context key with helper methods for working with a service.
Details
context creates a one-service Context, use and useSync retrieve the
service from the current Effect context before applying a function, and of
is a type-level helper for service values.
Example (Defining a service key)
import { Context } from "effect"
// Define an identifier for a database service
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
// The key can be used to store and retrieve services
const context = Context.make(Database, { query: (sql) => `Result: ${sql}` })
Creates a Context service key.
When to use
Use when you need to define a context service key for a dependency that must
be provided by the surrounding context.
Details
Call Context.Service("Key") for a function-style key, or use the two-stage
form Context.Service<Self, Shape>()("Key") for class-style service
declarations. The returned key can be yielded as an Effect and passed to
Context.make, Context.add, and the Context getter functions.
Gotchas
The string key is the runtime identity of the service. Reusing the same key
string for unrelated services makes them occupy the same slot in a
Context.
Example (Creating service keys)
import { Context } from "effect"
// Create a simple service
const Database = Context.Service<{
query: (sql: string) => string
}>("Database")
// Create a service class
class Config extends Context.Service<Config, {
port: number
}>()("Config") {}
// Use the services to create contexts
const db = Context.make(Database, {
query: (sql) => `Result: ${sql}`
})
const config = Context.make(Config, { port: 8080 })
Namespace containing utility types for Context service keys.
Example (Extracting service types)
import { Context } from "effect"
const Database = Context.Service<{
query: (sql: string) => string
}>("Database")
// Extract service type from a key
type DatabaseService = Context.Service.Shape<typeof Database>
// Extract identifier type from a key
type DatabaseId = Context.Service.Identifier<typeof Database>
Service<function (type parameter) Self in ServiceClass<in out Self, in out Identifier extends string, in out Shape>Self, function (type parameter) Shape in ServiceClass<in out Self, in out Identifier extends string, in out Shape>Shape>
{
new(_: never_: never): ServiceClass.interface ServiceClass<in out Self, in out Identifier extends string, in out Shape>.Shape<Identifier extends string, Service>Runtime and type-level metadata carried by a class-style service key,
including its service type identifier, string key, and service shape.
Shape<function (type parameter) Identifier in ServiceClass<in out Self, in out Identifier extends string, in out Shape>Identifier, function (type parameter) Shape in ServiceClass<in out Self, in out Identifier extends string, in out Shape>Shape>
readonly ServiceClass<in out Self, in out Identifier extends string, in out Shape>.key: in out Identifier extends stringkey: function (type parameter) Identifier in ServiceClass<in out Self, in out Identifier extends string, in out Shape>Identifier
}
/**
* Namespace containing helper types for class-style `Context.Service`
* declarations.
*
* @since 4.0.0
*/
export declare namespace ServiceClass {
/**
* Runtime and type-level metadata carried by a class-style service key,
* including its service type identifier, string key, and service shape.
*
* @category models
* @since 4.0.0
*/
export interface interface ServiceClass<in out Self, in out Identifier extends string, in out Shape>.Shape<Identifier extends string, Service>Runtime and type-level metadata carried by a class-style service key,
including its service type identifier, string key, and service shape.
Shape<function (type parameter) Identifier in Shape<Identifier extends string, Service>Identifier extends string, function (type parameter) Service in Shape<Identifier extends string, Service>Service> {
readonly [const ServiceTypeId: ServiceTypeIdString literal type used as the runtime type identifier for Context
service keys.
Runtime type identifier attached to Context service keys and used by
isKey to recognize them.
ServiceTypeId]: typeof const ServiceTypeId: ServiceTypeIdString literal type used as the runtime type identifier for Context
service keys.
Runtime type identifier attached to Context service keys and used by
isKey to recognize them.
ServiceTypeId
readonly ServiceClass<in out Self, in out Identifier extends string, in out Shape>.Shape<Identifier extends string, Service>.key: Identifier extends stringkey: function (type parameter) Identifier in Shape<Identifier extends string, Service>Identifier
readonly type Service: ServiceService: function (type parameter) Service in Shape<Identifier extends string, Service>Service
}
}