Tooling: run vs check
Relay is a service that receives webhooks and turns them into jobs. Before it can do anything it
has to read its configuration: a port, a queue URL, a timeout. That loader is where this module
starts, because it is exactly where untyped JavaScript goes wrong. A missing variable becomes
undefined, Number(undefined) becomes NaN, and the service boots with a broken config.
Two programs, two jobs
Section titled “Two programs, two jobs”TypeScript code passes through two separate tools, and they don’t do the same thing.
- Bun runs it. Bun strips the type annotations and executes what’s left. It never checks the types.
tscchecks it. The TypeScript compiler reads every file and reports type errors. WithnoEmitit writes nothing. Its only job is to say whether the program is consistent.
Here is Relay’s first config loader:
export interface Config { port: number queueUrl: string}
export function loadConfig(env: Record<string, string | undefined>): Config { return { port: Number(env.PORT), // @ts-expect-error: QUEUE_URL may be missing, so this is string | undefined queueUrl: env.QUEUE_URL }}Without the @ts-expect-error line, this is what each tool says about it:
$ PORT=8080 bun run src/config.ts{ port: 8080, queueUrl: undefined } # runs, boots with no queue
$ bunx tscsrc/config.ts(11,5): error TS2322: Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.(Without a terminal, or with --pretty false, tsc prints errors in this file(line,col) form.)
Bun runs the file happily. tsc refuses it, and the error points at the exact line. A type error
is a bug report that arrives before the bug does, but only if something runs the checker. The
editor runs it as you type, and the check script you’re about to add runs it in CI.
Set up Relay
Section titled “Set up Relay”-
Create the project.
Terminal window mkdir relay && cd relaybun init -y -
Open
package.jsonand delete thepeerDependenciesblock.bun initwrites"typescript": "^5"there, and that pin keeps you on TypeScript 5 even after you ask for the latest version. -
Install the compiler and check its version.
Terminal window bun add -d typescript@latestbunx tsc --version # Version 7.0.2 -
Replace
tsconfig.jsonwith the config below. -
Add the two scripts you’ll use all the time:
package.json "scripts": {"dev": "bun --watch src/main.ts","check": "tsc"}
{ "compilerOptions": { // What Bun runs: modern JS, no compile step "target": "esnext", "module": "preserve", "moduleResolution": "bundler", "moduleDetection": "force", "allowImportingTsExtensions": true, "noEmit": true, "verbatimModuleSyntax": true, "skipLibCheck": true,
// Bun's globals (Bun, process, fetch...). TypeScript 6.0 and later load no @types by default. "types": ["bun"],
// The checks "strict": true, "noUncheckedIndexedAccess": true, "exactOptionalPropertyTypes": true, "noImplicitOverride": true, "noFallthroughCasesInSwitch": true }}The checks that matter
Section titled “The checks that matter”strict is a bundle of flags, and the flags after it are ones strict leaves off. Each one below
is shown catching a real Relay bug. Every @ts-expect-error line marks an error that tsc reports.
strict: null checks and no implicit any
Section titled “strict: null checks and no implicit any”strictNullChecks (part of strict) keeps undefined and null out of a type unless you say so.
noImplicitAny (also part of strict) makes you write a type where TypeScript can’t infer one.
const env: Record<string, string | undefined> = { PORT: "8080" }
// @ts-expect-error: string | undefined is not assignable to stringconst queueUrl: string = env.QUEUE_URL
// @ts-expect-error: parameter 'raw' implicitly has an 'any' typeexport function parsePort(raw) { return Number(raw)}noUncheckedIndexedAccess: indexes can miss
Section titled “noUncheckedIndexedAccess: indexes can miss”Without it, argv[0] is typed string even when the array is empty. With it, every index access
includes undefined, which is what really happens at runtime.
const argv: string[] = []
// @ts-expect-error: argv[0] is string | undefinedconst first: string = argv[0]
const port = argv[0] ?? "8080" // fine: the fallback handles the missing caseexactOptionalPropertyTypes: missing is not undefined
Section titled “exactOptionalPropertyTypes: missing is not undefined”timeoutMs?: number means “the key may be absent”. Without this flag TypeScript also lets you
write timeoutMs: undefined, a present key holding undefined. Code that checks
"timeoutMs" in config or spreads the config over defaults then behaves differently.
interface Config { timeoutMs?: number}
// @ts-expect-error: undefined is not a number, and the key is not "missing"const bad: Config = { timeoutMs: undefined }
const good: Config = {} // absent, as the type promisednoFallthroughCasesInSwitch and noImplicitOverride
Section titled “noFallthroughCasesInSwitch and noImplicitOverride”The first catches a case that runs into the next one because a break is missing. The second
makes a subclass say override when it replaces a parent method, so renaming the parent method
can’t silently leave a stale copy behind.
export function levelName(n: number): string { let name = "unknown" switch (n) { // @ts-expect-error: fallthrough case in switch case 0: name = "debug" case 1: name = "info" break } return name}
class Logger { write(line: string) { console.log(line) }}
class JsonLogger extends Logger { // @ts-expect-error: must have an 'override' modifier write(line: string) { console.log(JSON.stringify({ line })) }}Exercise
Section titled “Exercise”The loader below runs, but tsc rejects it: queueUrl may be undefined. Fix that and it
reports the next problem in the same object, the timeoutMs: undefined from this lesson. A third
bug doesn’t show up as a type error at all: an argument like "abc" becomes NaN. Fix all three. Hover env.QUEUE_URL and
argv[0] to see the types the compiler is working with.
- Bun runs TypeScript and ignores the types.
tscchecks them. You need both, so addcheckto CI. - With
noEmit,tscis a pure checker: it never writes files. - The flags worth turning on:
strict,noUncheckedIndexedAccess,exactOptionalPropertyTypes,noImplicitOverride,noFallthroughCasesInSwitch. - TypeScript 6.0 and later (including 7) load no
@typesby default: list"types": ["bun"].
Further reading
Section titled “Further reading”- Bun docs: TypeScript: running TypeScript in Bun and the recommended compiler options.
- TSConfig Reference:
strictand the other flags.