Skip to content

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.

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.
  • tsc checks it. The TypeScript compiler reads every file and reports type errors. With noEmit it writes nothing. Its only job is to say whether the program is consistent.

Here is Relay’s first config loader:

src/config.ts
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:

Terminal window
$ PORT=8080 bun run src/config.ts
{ port: 8080, queueUrl: undefined } # runs, boots with no queue
$ bunx tsc
src/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.

  1. Create the project.

    Terminal window
    mkdir relay && cd relay
    bun init -y
  2. Open package.json and delete the peerDependencies block. bun init writes "typescript": "^5" there, and that pin keeps you on TypeScript 5 even after you ask for the latest version.

  3. Install the compiler and check its version.

    Terminal window
    bun add -d typescript@latest
    bunx tsc --version # Version 7.0.2
  4. Replace tsconfig.json with the config below.

  5. Add the two scripts you’ll use all the time:

    package.json
    "scripts": {
    "dev": "bun --watch src/main.ts",
    "check": "tsc"
    }
tsconfig.json
{
"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
}
}

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.

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 string
const queueUrl: string = env.QUEUE_URL
// @ts-expect-error: parameter 'raw' implicitly has an 'any' type
export 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 | undefined
const first: string = argv[0]
const port = argv[0] ?? "8080" // fine: the fallback handles the missing case

exactOptionalPropertyTypes: 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 promised

noFallthroughCasesInSwitch 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 }))
}
}

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. tsc checks them. You need both, so add check to CI.
  • With noEmit, tsc is 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 @types by default: list "types": ["bun"].