Modules, and the whole loader
Relay’s config code has grown to a level parser, a flag parser, env helpers, exit codes and a
loader. This lesson splits it into modules and covers the rules TypeScript adds to JavaScript’s
import and export. Then the capstone puts every M1 lesson into one loader.
ES modules
Section titled “ES modules”A file with import or export is a module. Its top-level names are private unless exported.
With moduleDetection: "force" in the tsconfig from lesson 1, every file is a module even
without imports, so two files can’t clash over a top-level name.
export const LEVELS = ["debug", "info", "warn", "error"] as constexport type LogLevel = (typeof LEVELS)[number]
export function parseLevel(raw: string): LogLevel | undefined { return LEVELS.find((l) => l === raw.toLowerCase())}Prefer named exports to export default. A named export has the same name at every import, so
search and rename work across the codebase.
Values and types
Section titled “Values and types”An import can bring in values (functions, classes, constants) and types (interfaces, type
aliases). Types disappear when the code runs, so a runtime that strips them has to know which
imports were type-only.
verbatimModuleSyntax makes that explicit. Every import is kept exactly as written, and an import
used only as a type must say so with type:
// @ts-expect-error: 'BunFile' is a type and must be imported using a type-only importimport { BunFile } from "bun"
import type { Server } from "bun" // the whole import is erasedimport { file, type S3File } from "bun" // only S3File is erased
export function size(f: S3File) { return f.size}export const config = file("relay.json")Without the flag, the compiler decides which imports to drop. The result can differ between tsc
and a tool that compiles one file at a time, such as Bun, esbuild or Node’s type stripping. With
it, the file says what is erased, and every tool agrees.
Import paths
Section titled “Import paths”Bun and Node.js resolve the path you write. allowImportingTsExtensions lets you write the real
file name, ./level.ts, which works in both and in Deno. Directory imports (./config meaning
./config/index.ts) depend on the bundler, so spell out the file.
Running a file directly
Section titled “Running a file directly”import.meta.main is true only in the file Bun was started with. It lets one file be both a
module that tests can import and a program you can run:
export function main(argv: readonly string[]): number { console.log("relay starting with", argv) return 0}
if (import.meta.main) { process.exitCode = main(process.argv.slice(2))}Split Relay’s config
Section titled “Split Relay’s config”Do this in the Relay project from lesson 1.
Directorysrc/
Directoryconfig/
- cli.ts parseFlags, parseHostPort (lesson 3)
- env.ts readEnv, requireEnv (lesson 7)
- exit.ts ExitCode (lesson 9)
- file.ts parseConfigFile (lesson 10)
- level.ts LEVELS, LogLevel, parseLevel (lesson 2)
- config.ts loadConfig, Config, ConfigError
- main.ts the entry point
- tsconfig.json
- package.json
-
Move each lesson’s solution into its file and export what other files use.
-
Import types with
import typeor an inlinetype.bunx tscreports any you miss. -
In
main.ts, callloadConfig(process.env, process.argv.slice(2)). On aConfigError, print each problem and setprocess.exitCodeto itsexitCode. -
Check the whole project, then try it:
Terminal window bun run checkPORT=abc LOG_LEVEL=verbose bun src/main.ts; echo "exit $?"Both problems should print, and the exit code should be 78.
Capstone
Section titled “Capstone”The loader below type-checks and ignores almost every rule. Implement them all. The comment at the
top lists them, and each failing test names one. Every M1 lesson is in here: literal unions,
readonly tuples, optional keys without undefined, narrowing, find instead of as, and an
error that carries an exit code.
- Every file is a module (
moduleDetection: "force"). Prefer named exports. - With
verbatimModuleSyntax, type-only imports are marked withtype, and every tool erases the same imports. - Write real file names in imports,
./level.ts. import.meta.mainmakes a file both importable and runnable.
Further reading
Section titled “Further reading”- TSConfig Reference:
verbatimModuleSyntaxandmoduleoptions. - Bun docs: Module Resolution: how Bun resolves import paths.
- Node.js docs: Modules: TypeScript: what Node’s type stripping needs from imports.