Skip to content

Vibe in .ts files with // @vibe

A .ts, .tsx, .mts or .cts file whose first line is exactly // @vibe is a Vibe source file. The Vibe compiler reads it with the rules of a .vibe file, and every file without the directive stays TypeScript. Use the directive in a project that cannot rename its files. When you can rename a file, use the .vibe extension, which every Vibe tool and plugin recognizes by name. VIP-42 defines the directive.

// @vibe
// greet.ts
error Empty
 
export effect function greet(name: string) {
  if (name === "") throw new Empty()
  return `hello ${name}`
}
 
export effect function greetOrNobody(name: string) {
  return greet(name) catch { when Empty: "hello nobody" }
}
// TypeScript (.ts)
// caller.ts
import { Effect } from "effect"
import { greet, greetOrNobody } from "./greet.js"
 
console.log(await Effect.runPromise(greet("Ada")))
console.log(await Effect.runPromise(greetOrNobody("")))
hello Ada
hello nobody

greet.ts uses error, effect function and a tagged catch, which are Vibe forms. caller.ts has no directive, so it is ordinary TypeScript. It imports the Vibe module as it imports any .ts module, and it receives functions that return Effect values.

Place the directive

The directive is the whole first line of the file: // @vibe. The following rules decide whether a first line counts:

  • Spaces and tabs can follow // @vibe. Any other text on the line makes it an ordinary comment.
  • A byte order mark and a #! line can come before the directive.
  • A directive on a later line, such as after a license comment, is an ordinary comment.
  • /* @vibe */ and every other spelling are ordinary comments.
  • A declaration file (.d.ts), a JavaScript file and a JSON file have no directive.

No setting in tsconfig.json or vibe.config.json turns the directive on or off. The compiler decides from the file's name and its first line.

When the first line is not the directive, the file is TypeScript, and Vibe syntax in it is a syntax error:

// @vibes
// expect-error: TS1434
effect function greet(name: string) {
  return `hello ${name}`
}

Vibe syntax in a marked file

A marked .ts, .mts or .cts file follows the rules of a .vibe file. A marked .tsx file follows the rules of a .vibex file, so it can hold JSX. Every Vibe form is available: effect functions, try, catch, error, schema, data, service, match, provide, and the implicit Effect names such as Console and Schema. The compiler also applies the Vibe checks and reports them with EK codes on the .ts file:

// @vibe
// expect-error: EK0401
export function positive(n: number) {
  if (n < 0) throw new Error("negative")
  return n
}

A plain function in Vibe cannot throw, so the compiler reports EK0401, as it does in a .vibe file. In a file without the directive, the same code is valid TypeScript.

A .vibe file imports a marked file by its path:

import { greet } from "./greet.ts"
// Run in an effect function: try Console.log(try greet("Lin"))
// Output: hello Lin

Differences from a .vibe file

A marked file keeps its .ts name, so resolvers and TypeScript's own syntax rules treat it as a .ts file. The following table lists what differs from a .vibe file.

In a marked fileResult
module name { }TS1540. TypeScript reserves this syntax for namespaces, so a marked file has no Vibe module declarations. Put that code in a plain .ts file.
An import of ./plain.tsFollows TypeScript's checks: TS5097 unless allowImportingTsExtensions or rewriteRelativeImportExtensions is set. Write ./plain.js, or set one of the options.
Output namesgreet.ts emits greet.js, .mts emits .mjs, and .cts emits .cjs, as for TypeScript files.
Declaration sidecarNone. The compiler writes x.d.vibe.ts only beside x.vibe.
// @vibe
// expect-error: TS1540
module text {
  export const upper = (value: string) => value.toUpperCase()
}

Stock TypeScript and other tools

To every tool except the Vibe compiler and the Vibe plugins, the directive is a comment and the file is TypeScript. Stock tsc 5.9 reports syntax errors for greet.ts:

greet.ts(2,1): error TS1434: Unexpected keyword or identifier.
greet.ts(4,1): error TS1128: Declaration or statement expected.
greet.ts(4,8): error TS1434: Unexpected keyword or identifier.

A stock tool that accepts a marked file can still give it another meaning. Both compilers accept this file when the DOM library is enabled:

// @vibe
// request.ts
export const request = Request

The Vibe compiler binds Request to Effect's Request module and emits import * as Request from "effect/Request". Stock TypeScript binds it to the web Request constructor. Neither compiler reports a diagnostic.

The following table lists how to run each kind of tool on a marked file.

TaskTool
Check and emitvibe or vibelang, with your project's tsconfig.json.
Run a file in Nodevibe run src/main.ts, or node --import vibelang/register src/main.ts.
Bundlevibelang-vite for Vite and Vitest, vibelang-unplugin for webpack, Rspack, Rollup, Rolldown and esbuild.
Formatvibelang format. It leaves files without the directive unchanged.
Lintvibelang lint.
EditAn editor that runs the Vibe language server. See Developer tools after migration.

Prettier, ESLint, tsx, ts-node and Node's own type stripping read a marked file as TypeScript. Exclude marked files from those tools. bun build, Bun's test runner and Metro do not support marked files: use .vibe files there.

An existing tsc script reaches the Vibe compiler only when the package vibelang-typescript is installed under the name typescript. That package is a source-checkout preview and is not on npm. For its setup and tool compatibility, see Vibe in .ts files.

Rules and diagnostics

The following table lists the diagnostics that are specific to marked files. Every other Vibe diagnostic applies as in a .vibe file.

CodeCondition
TS1540A marked file holds a module name { } declaration.
TS5097A marked file imports a path that ends in .ts, and neither allowImportingTsExtensions nor rewriteRelativeImportExtensions is set.
TS1434, TS1128A file holds Vibe syntax, but its first line is not exactly // @vibe. Stock tsc reports the same codes for a marked file.
EK0401A plain function in a marked file throws. This check, like every EK check, is absent from files without the directive.