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.
The three channels: A, E, and R
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: 60Read number!MissingRate!Rates left to right:
| Channel | Value here | Meaning | Special types |
|---|---|---|---|
A (Success) | number | What try convert(...) evaluates to | void = no value; never = never succeeds |
E (Error) | MissingRate | Expected failures callers can catch | never (empty slot) = no expected failures |
R (Requirements) | Rates | Services that must be provided before running | never (omitted) = ready to run anywhere |
You did not write a return type on convert — the compiler inferred all three channels for you:
- Calling
try RatesaddedRatestoR. - Calling
try rates.lookup(currency)addedMissingRatetoE. - Returning
amount * ...setAtonumber.
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)
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: 2Reading the ! shorthand
An omitted or empty slot always means never — never "infer this slot":
| Shorthand | Equivalent Effect.Effect<A, E, R> | Meaning |
|---|---|---|
number!! | Effect.Effect<number, never, never> | Returns number, cannot fail, needs no services |
number!MissingRate | Effect.Effect<number, MissingRate, never> | May fail with MissingRate, needs no services |
number!!Rates | Effect.Effect<number, never, Rates> | Cannot fail, requires Rates |
number!MissingRate!Rates | Effect.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.vibefiles the compiler prefersA!E!R(EK4833) for readability, while generated.d.vibe.tsfiles emitEffect.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 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.00Wait — why is laziness a superpower? Because
conversionis just a value untiltryruns it, you can pass it toretry,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 resilientA JavaScript
Promisehas 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 exampleids.map((id) => convert(10, id))builds an array of blueprints).async function f()— standard JavaScript Promise function. Usesawaitfor Promises only.effect function f()— builds an Effect. Usestryto run Effects,throwfor 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> // neverComing from Effect
Effectis globally in scope without an import. PreferA!E!RoverEffect.Effect<A, E, R>(the long form draws advisoryEK4833) and avoid the deprecated bareEffect<A, E, R>alias (EK4856).- Calling an
effect functionis like calling anEffect.fn— it returns an unrunEffectand evaluates parameter defaults at call time.tryinside aneffectfunction compiles toyield*.
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