Skip to content

The Effect Type

In ordinary TypeScript, a function signature tells only one-third of the story. It names the success value, but not what can go wrong or what the function needs.

// TypeScript (.ts)
// What can go wrong? What database or API key does it need?
// TypeScript has no idea.
declare function convert(amount: number, currency: string): Promise<number>

When convert throws MissingRate or secretly reads a global Rates client, you find out at 2 a.m. in production.

VibeLang replaces that guesswork with a single compile-time contract: Effect<A, E, R>, written A!E!R.

Every Effect carries three compile-time channels: Success (A), Expected Errors (E), and Required Services (R)Every Effect carries three compile-time channels: Success (A), Expected Errors (E), and Required Services (R)
Every Effect carries three compile-time channels: Success (A), Expected Errors (E), and Required Services (R)

The three channels: A, E, and R

A function's type says what it returns, how it fails and what it needs

Watch all three channels appear automatically from a three-line function:

error MissingRate {
  currency: string
}
 
service Rates {
  lookup(currency: string): number!MissingRate
}
 
effect function convert(amount: number, currency: string) {
  const rates = try Rates
  return amount * (try rates.lookup(currency))
}
// Inferred: number!MissingRate!Rates
// Run in an effect function: try Console.log(try Effect.provide(convert(30, "EUR"), Layer.succeed(Rates, { lookup: effect (_currency) => 2 })))
// Output: 60

Read number!MissingRate!Rates left to right:

ChannelValue hereMeaningSpecial types
A (Success)numberWhat try convert(...) evaluates tovoid = no value; never = never succeeds
E (Error)MissingRateExpected failures callers can catchnever (empty slot) = no expected failures
R (Requirements)RatesServices that must be provided before runningnever (omitted) = ready to run anywhere

You did not write a return type on convert — the compiler inferred all three channels for you:

  • Calling try Rates added Rates to R.
  • Calling try rates.lookup(currency) added MissingRate to E.
  • Returning amount * ... set A to number.

When a function runs several Effects, their errors union together in E and their services union together in R. Catching an error subtracts it from E; providing a service subtracts it from R.

Writing contracts by hand (A!E!R)

The compiler infers the type as you write

Most of the time, omit the return annotation and let the compiler infer A!E!R. Write an explicit contract when you want the compiler to verify a promise — at module boundaries, on service methods, or when you want a guarantee that every error has been handled:

// Explicit full contract: checks all three channels
effect function rateOf(currency: string): number!MissingRate!Rates {
  const rates = try Rates
  return try rates.lookup(currency)
}
 
// Empty E slot means E = never: compiler verifies no expected error escapes
effect function safeRate(currency: string): number!!Rates {
  const rates = try Rates
  return try orDie(rates.lookup(currency))
}
 
// No annotation: compiler infers number!MissingRate!Rates
effect function anyRate(currency: string) {
  const rates = try Rates
  return try rates.lookup(currency)
}
// Run in an effect function: try Console.log(try Effect.provide(safeRate("EUR"), Layer.succeed(Rates, { lookup: effect (_currency) => 2 })))
// Output: 2

Reading the ! shorthand

An omitted or empty slot always means never — never "infer this slot":

ShorthandEquivalent Effect.Effect<A, E, R>Meaning
number!!Effect.Effect<number, never, never>Returns number, cannot fail, needs no services
number!MissingRateEffect.Effect<number, MissingRate, never>May fail with MissingRate, needs no services
number!!RatesEffect.Effect<number, never, Rates>Cannot fail, requires Rates
number!MissingRate!RatesEffect.Effect<number, MissingRate, Rates>All three channels populated

Parentheses rule (EK0301). Wrap compound types inside a slot in parentheses:

  • Union error: string!(MissingRate | Offline)!Rates
  • Optional Effect value: (number!!) | null
  • Nested Effect: (number!!)!! (arrays already delimit their elements: Array<number!!>!!)
  • Object requirement after !!: number!!({ readonly flag: boolean }) (so { is not mistaken for the function body)

Keep each ! and the start of its slot on the same line — a line break ends an Effect type.

There are no dumb questions: What if I write Effect.Effect<A, E, R> directly? It means the exact same thing and is accepted everywhere a type is expected. Inside .vibe files the compiler prefers A!E!R (EK4833) for readability, while generated .d.vibe.ts files emit Effect.Effect<A, E, R> so TypeScript callers see standard Effect types.

Description vs. execution (the #1 mental model)

This is the single most important difference between an async function and an effect function:

Calling an effect function builds a cold blueprint; only try or a runner executes itCalling an effect function builds a cold blueprint; only try or a runner executes it
Calling an effect function builds a cold blueprint; only try or a runner executes it

Calling convert(30, "EUR") does not look up any rate. It allocates a tiny, immutable description — a blueprint of work to do later.

const conversion = convert(30, "EUR") // Blueprint only — nothing runs yet
 
effect function displayedPrice() {
  const amount = try conversion       // Runs here, inside the current fiber
  return try orDie(Effect.try(() => amount.toFixed(2)))
}
// Run in an effect function: try Console.log(try Effect.provide(displayedPrice(), Layer.succeed(Rates, { lookup: effect (_currency) => 2 })))
// Output: 60.00

Wait — why is laziness a superpower? Because conversion is just a value until try runs it, you can pass it to retry, timeout, race, or run it twice without wrapping it in thunks:

// fragment
const resilient = conversion |> retry(%, Schedule.recurs(3)) |> timeout(%, "2 seconds")
const result = try resilient

A JavaScript Promise has already started running the moment you create it — you cannot retry a Promise you were handed. An Effect has not started yet, so combinators compose cleanly.

Plain vs. async vs. effect

The keyword on the function decides its kind — never its return annotation:

  • function f() — plain synchronous JavaScript. Can return an unrun Effect (for example ids.map((id) => convert(10, id)) builds an array of blueprints).
  • async function f() — standard JavaScript Promise function. Uses await for Promises only.
  • effect function f() — builds an Effect. Uses try to run Effects, throw for typed errors, and supports stack-safe recursion (try sumTo(100000) will not overflow the JS call stack).

Anything that implements Effect works with try: effect functions, effect { ... } blocks, service keys (try Rates), Config descriptors (try Config.String("PORT")), and built-in values like try Clock.currentTimeMillis.

Inspecting VibeLang types from TypeScript

Exported VibeLang functions compile to standard Effect functions in .d.vibe.ts. TypeScript consumers can inspect their channels with Effect's type helpers:

// convert.vibe
export effect function double(amount: number) {
  return amount * 2
}
// number!!
// Run in an effect function: try Console.log(try double(30))
// Output: 60
// TypeScript (.ts)
// inspect.ts: imports the emitted VibeLang module
import { Effect } from "effect"
import { double } from "./convert.js"
 
type Contract = ReturnType<typeof double>
type Value = Effect.Success<Contract>     // number
type Failure = Effect.Error<Contract>     // never
type Services = Effect.Services<Contract> // never

Coming from Effect

  • Effect is globally in scope without an import. Prefer A!E!R over Effect.Effect<A, E, R> (the long form draws advisory EK4833) and avoid the deprecated bare Effect<A, E, R> alias (EK4856).
  • Calling an effect function is like calling an Effect.fn — it returns an unrun Effect and evaluates parameter defaults at call time. try inside an effect function compiles to yield*.

Next: Creating Effects — build Effects from domain logic, wrap throwing .ts libraries, and bridge Promises and callbacks.


The matching Effect page: effect.website/docs/v4/getting-started/the-effect-type