Types that prevent bugs
All of these techniques share one goal: turn a mistake you could make at run time into something that doesn't compile.
You'll recognise most of these individually. What's worth noticing is how they combine: by the time a value reaches the logic, most wrong programs can't even be written.
type Claim = {
status: string
verifiedAt?: Date
expiredAt?: Date
}
// Verified, but when? And also expired?
const c: Claim = {
status: "verified",
expiredAt: new Date(),
}
type ClaimState =
| { status: "verified"; verifiedAt: Date; lastSeenAt: Date }
| { status: "expired"; expiredAt: Date }
const c: ClaimState = {
status: "verified",
expiredAt: new Date(),
}
One source for every closed set
Two doors for a domain
An AsciiDomain can only come from two places, and the difference matters.
Trust stops at the edges
Types disappear at run time, so anything from outside gets checked as it comes in.
| What arrives | What checks it |
|---|---|
| A request body | Zod schema in lib/contract.ts |
| A DoH response | Zod schema in packages/dns/src/doh.ts |
| Domain Connect settings | Zod schema, and HTTPS only |
| A domain someone typed | normalizeDomain |
| A claim id in a URL | parseClaimId |
| A database row | claimFromRow and the to* helpers |
Which JavaScript version we check against
The engine's is es2025, set in packages/tsconfig/strict.json. That's the newest numbered version TypeScript 7 accepts. Beyond it there's only esnext, which is a moving target rather than a stable version. The frozen experiments stay on es2023.
| Where | TypeScript | Knows es2025? | Result |
|---|---|---|---|
bun run typecheck | 7.0.2 | Yes | No errors |
| TypeScript 6 | 6.0.3 | Yes | No errors |
| Zed's default server, vtsls | 5.9.3 | No | Errors that aren't real |
Testing that code doesn't compile
// @ts-expect-error verifiedAt is required
const bad: ClaimState = { status: "verified", lastSeenAt: at }
This is an assertion about the type system. If that line ever starts compiling, the directive itself becomes an error and bun run typecheck fails.