TypeScript inside a .vibe file with module declarations
A module name { … } declaration at the top level of a .vibe file holds a TypeScript module. Its body is checked, emitted and declared as a sibling .ts file is, and the rest of the file imports from it by name. Use it for the code that Vibe's own rules do not allow outside effect code: a function that throws, a plain try/catch, or a wrapper around a throwing API. VIP-40 defines the form.
module check {
/** @throws {RangeError} */
export function port(raw: string): number {
if (!/^\d+$/.test(raw)) throw new RangeError(`bad port: ${raw}`)
return Number(raw)
}
}
import { port } from check
export effect function loadPort(raw: string) {
return Effect.try({ try: () => port(raw), catch: () => -1 }) catch (fallback) => fallback
}
// Run in an effect function:
// try Console.log(try loadPort("8080"))
// try Console.log(try loadPort("http"))
// Output: 8080
// Output: -1The body of check is TypeScript, so its throw statement is allowed. The /** @throws {RangeError} */ comment tells the Vibe code what the function throws. loadPort is Vibe code: it calls port inside Effect.try, which turns the exception into a typed failure, and the catch handler maps that failure to a value.
Declare a module
Write the declaration at the top level of the file. The body is a complete TypeScript module: it has its own imports and exports, and it shares no variables with the rest of the file.
- The body follows TypeScript's rules, not Vibe's.
throw,try/catch/finallyand calls that throw are allowed. Vibe forms such aseffectandtryas an operator are not available in a body. - Document what a function throws. Write
/** @throws {T} */on every exported function that throws. A function with no@throwscomment is treated as throwingunknown. - A body cannot import from a sibling declaration of the same file. Such an import reports
EK4007.
Import from a module
Import with an identifier in place of a string: import { port } from check. A string specifier always names a file or a package, so from "check" imports the package check.
A module declaration is not a value. You cannot export it, pass it to a function, or read a member from it. To make a member available to other files, re-export it:
module text {
export const clean = (raw: string) => raw.trim().toLowerCase()
}
export { clean } from textChoose between a module declaration and a .ts file
Both hold the same code and compile to the same output. Use a module declaration to keep a small TypeScript helper next to the one Vibe function that uses it. Use a .ts file when another tool needs a file path, or when several files share the helper.
Rules and diagnostics
The following table lists the diagnostics that apply to module declarations.
| Code | Condition |
|---|---|
EK4001 | A module declaration that is not at the file's top level, such as one inside a function or a block |
EK4002 | export module name { } or export default module { } |
EK4003 | A module expression with no name: module { … } |
EK4004 | The module's name used as a value, such as import(text), text.clean or typeof text |
EK4005 | The module's name declared twice in the file, by another module, a variable or an import |
EK4006 | An identifier specifier that names no module declaration of the file, or that carries import attributes |
EK4007 | A body that imports from a sibling module declaration |
EK4008 | export = or import x = require() in a body |
EK4009 | import.meta in a body |
// expect-error: EK4002
export module shout {
export const loud = (raw: string) => raw.toUpperCase()
}Related pages
- Expected errors:
throwin effect code and wrapping functions that throw. - Creating Effects:
Effect.tryandtryPromisefor foreign code. - Vibe in .ts files with // @vibe: the opposite direction, Vibe syntax in a TypeScript file.