Skip to content

Managing Services

A service is a named capability — a database, clock, mail client, or config object — that Effect functions ask for without threading implementations through every parameter list. service declares the key and contract; try ServiceName reads the implementation and adds it to channel R; a Layer or provide block supplies it at the edge.

Services declare contracts, Layers build implementations, and provide wires them once at main()Services declare contracts, Layers build implementations, and provide wires them once at main()
Services declare contracts, Layers build implementations, and provide wires them once at main()

Declaring a service

service Sequence {
  next(): number!!
}
 
service Reporter {
  write(message: string): void!!
}
// Run in an effect function:
//   provide Sequence = { next: effect () => 17 }, Reporter = { write: (message) => Console.log(message) } {
//     const sequence = try Sequence
//     const reporter = try Reporter
//     try reporter.write(`ticket-${try sequence.next()}`)
//   }
// Output: ticket-17

A service body lists member signatures just like a TypeScript interface — methods and properties, no implementations. Effect-returning methods write A!E!R (next(): number!!); plain methods and value properties keep ordinary TypeScript types. Export a service with export service Sequence { ... }.

Declaring a service of any shape

When the implementation shape already exists as an interface, primitive, or function type, attach it after a colon:

interface SearchApi {
  find(query: string): string!!
}
 
service Search: SearchApi { static readonly key = "app/Search" }
service Prefix: string { static readonly key = "app/Prefix" }
 
effect function lookup(query: string) {
  const prefix = try Prefix
  const search = try Search
  return `${prefix}${try search.find(query)}`
}
// Run in an effect function: provide Prefix = "> ", Search = { find: effect (query) => `hit:${query}` } { try Console.log(try lookup("vibe")) }
// Output: > hit:vibe

In a whole-shape service, the braces hold only static readonly key and/or static readonly defaultValue (or stay empty {} when using a derived key).

Requesting and providing implementations

Look up a service with try ServiceName, then call its methods or read its properties:

// expect-warning: EK4836
effect function nextLabel() {
  const sequence = try Sequence
  return `ticket-${try sequence.next()}`
}
// Inferred: string!!Sequence
 
effect function fixedLabel() {
  return try provideService(nextLabel(), Sequence, {
    next: effect () => 17
  })
}
// Inferred: string!!
 
effect function readImplementation() {
  const sequence = try Sequence
  return try sequence.next()
}
// Inferred: number!!Sequence
// Run in an effect function:
//   try Console.log(try fixedLabel())
//   provide Sequence = { next: effect () => 23 } { try Console.log(try nextLabel(), try readImplementation()) }
// Output: ticket-17
// Output: ticket-23 23

Value properties work the same way — reading try Settings adds Settings to R even though settings.baseUrl is a plain string:

service Settings {
  baseUrl: string
}
 
effect function endpoint(path: string) {
  const settings = try Settings
  return `${settings.baseUrl}/${path}`
}
// Inferred: string!!Settings
// Run in an effect function: provide Settings = { baseUrl: "https://example.test" } { try Console.log(try endpoint("tickets")) }
// Output: https://example.test/tickets

Note: A service name is a key, not an instance. Write const users = try Users; try users.find(id) (or try (try Users).find(id)), never Users.find(id).

Service identifiers vs. implementation shapes

type SequenceImplementation = Sequence.Shape
type SameImplementation = Context.Service.Shape<typeof Sequence>

Every body-declared service exposes two distinct types:

  • Sequence — the nominal identifier used in channel R (string!!Sequence). Two services with identical methods are still distinct dependencies.
  • Sequence.Shape — the plain member object type implemented by a layer, test fake, or class (implements Sequence.Shape).

Whole-shape services (service Search: SearchApi {}) use SearchApi (or Context.Service.Shape<typeof Search>) directly without generating a Search.Shape namespace.

Combining multiple dependencies

When a function reads several services, their identifiers union in R. Supply them together with Layer.merge or a Context inside provide:

effect function announceTicket() {
  const label = try nextLabel()
  const reporter = try Reporter
  try reporter.write(`Created ${label}`)
  return label
}
// Inferred: string!!(Sequence | Reporter)
 
const SequenceTest = Layer.succeed(Sequence, {
  effect next() { return 17 }
})
const ReporterTest = Layer.succeed(Reporter, {
  effect write(_message) {}
})
 
effect function testAnnouncement() {
  provide Layer.merge(SequenceTest, ReporterTest) { return try announceTicket() }
}
// Run in an effect function:
//   {
//     const jsonValue = try testAnnouncement()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
// Output: "ticket-17"
const testContext = Context.add(
  Context.make(Sequence, { next: effect () => 17 }),
  Reporter,
  { write: effect (_message) => {} }
)
 
effect function testWithContext() {
  provide testContext { return try announceTicket() }
}
// Run in an effect function: try Console.log(try testWithContext())
// Output: ticket-17

Optional services (serviceOption)

Read an optional capability without adding it to R:

effect function optionalAnnouncement(message: string) {
  const reporter = try serviceOption(Reporter)
  if (Option.isNone(reporter)) return false
  try reporter.value.write(message)
  return true
}
// Inferred: boolean!!
// Run in an effect function:
//   try Console.log(try optionalAnnouncement("hello"))
//   provide Reporter = { write: (message) => Console.log(message) } { try Console.log(try optionalAnnouncement("hello")) }
// Output: false
// Output: hello
// Output: true

Capturing dependencies inside layers (no leakage)

Resolve a service's own dependencies inside Layer.effect when constructing it, so callers depend only on Reporter and never on ReportSettings:

service ReportSettings {
  prefix: string
}
 
const ReporterLive = Layer.effect(Reporter, effect {
  const settings = try ReportSettings
  return {
    effect write(message) {
      try Console.log(`${settings.prefix}: ${message}`)
    }
  }
})
// Inferred: Layer<Reporter, never, ReportSettings>
// Run in an effect function: provide ReportSettings = { prefix: "audit" } { provide ReporterLive { const reporter = try Reporter; try reporter.write("ready") } }
// Output: audit: ready

Service keys

At module scope inside a named package (package.json with a string "name"), VibeLang derives a unique key automatically (<pkg>/<relative-path>#<Name>). Override it with static readonly key = "app/Users" to keep an existing Context.Service identity across migrations or when declaring a service locally inside a function (EK0903).

Default implementations

Add static readonly defaultValue = () => implementation to turn a service into a Context.Reference that works with no provider at all. See Default Services.

Coming from Effect

Effect TypeScriptVibeLang
class Users extends Context.Service<Users, { ... }>()("app/Users") {}service Users { static readonly key = "app/Users"; ... }
class Db extends Context.Service<Db, DbApi>()("app/Db") {}service Db: DbApi { static readonly key = "app/Db" }
yield* Userstry Users
Context.Service.Shape<typeof Users>Users.Shape

Older service class and static readonly default forms are retired; see Migrating retired forms.

Rules

See Services in the specification for service declaration rules and diagnostics.


The matching Effect page: effect.website/docs/v4/requirements-management/services