[ ✦ ] December 1, 2026
Daniel Berigoi

TypeScript code that compiles and fails at runtime

Where TypeScript's types and the running code disagree, what that looked like in one of our own projects, and typerail, the open-source tool we built at Wingravity to report it.

TypeScript code that compiles and fails at runtime

Image source: Pexels

A TypeScript project can build without errors and still fail at runtime with a type error. TypeScript allows this in a number of known places, and most of them are hard to spot in a review.

This function, for example, compiles with strict: true, without any, as or @ts-ignore:

const names: string[] = [];

export function firstName(): string {
  return names[0].toUpperCase();
}

Calling it throws TypeError: Cannot read properties of undefined (reading 'toUpperCase'). TypeScript types names[0] as string, although the array is empty.

This is by design. TypeScript’s design goals list a sound type system as a non-goal and aim for a balance between correctness and productivity instead. The result is a set of known places where the types say one thing and the running code does another.

We write almost all of our software at Wingravity in TypeScript, so we collected these places, wrote a proof for each one, and built a tool that reports them: typerail.

In this post:

  1. Flags that strict doesn’t turn on
  2. Gaps that no flag closes
  3. Results on one of our projects
  4. typerail
  5. Trying it

Flags that strict doesn’t turn on

The example above fails because of a flag that strict leaves off. With noUncheckedIndexedAccess, names[0] has the type string | undefined, and the compiler asks you to handle the empty case. Without it, every array index and every Record<string, T> lookup is assumed to return a value.

exactOptionalPropertyTypes is similar. Without it, { timeout: undefined } is accepted for a timeout?: number property, and spreading that object over default settings replaces the default with undefined.

Most projects leave both flags off, because enabling them on an existing codebase produces hundreds of errors at once.

Gaps that no flag closes

Some gaps remain with every flag on. These three compile under the strictest settings TypeScript has.

Values from JSON.parse aren’t checked. JSON.parse returns any, which can be assigned to any type:

interface Account {
  balance: number;
}

const account: Account = JSON.parse('{"balance":"unlimited"}');
account.balance.toFixed(); // TypeError: account.balance.toFixed is not a function

The same applies to response.json() and to libraries without types. Nobody wrote any, so a review doesn’t catch it either.

Arrays accept elements of the wrong type. A Dog[] can be used as an Animal[]. Code that holds it as an Animal[] can then add a plain Animal:

const dogs: Dog[] = [rex];
const animals: Animal[] = dogs;
animals.push({ name: "Tom" });
dogs[1]?.bark(); // TypeError: dogs[1].bark is not a function

An unhandled promise can stop the process. An async function called without await compiles. If it rejects, Node reports an unhandled rejection and exits, along with every request in flight:

async function save(): Promise<void> {
  throw new Error("save failed");
}

export function handler(): void {
  save();
}

typescript-eslint has rules for some of these, such as no-floating-promises for the last one. They are opt-in, and an // eslint-disable comment turns them off.

Results on one of our projects

We ran typerail on dot-work, a project management app I wrote on Node with no runtime dependencies. It has about 2,900 lines of application code, uses strict: true and builds without errors.

typerail reported 95 findings. Most are index and lookup results assumed to exist. Three stand out.

A share link can contain undefined. Shared tasks get a public link:

html`<a href="${process.env.DOMAIN}/share/${task.public_id}">`

process.env.DOMAIN has the type string | undefined, and the project doesn’t document or check it. When it isn’t set, the link points to undefined/share/abc123. TypeScript accepts this because a template literal takes a value of any type, and the html helper takes unknown. This became a new gap in the catalog, TR807.

Database rows aren’t checked. Every query goes through a cast:

return this.#sql.all(strings, ...values) as T[];

Every db.get<Task> in the app relies on it. A renamed column or a missed migration doesn’t show up as a type error.

A status from the database is used as an array index. Task statuses are numbers stored in the database and looked up in a fixed list. A number outside the list breaks the task page.

tsc reports none of these, and they are easy to miss in a review because the code looks correct.

The first run reported 104 findings, and some of them couldn’t fail. email.split("@")[0], for example, always has a first element. We changed typerail to leave these cases out. We prefer fewer findings that are right over a long list that people learn to ignore.

typerail

typerail runs TypeScript with its own compiler flags and its own lint rules, and reports what they find beyond the project’s own checks:

npx @wingravity/typerail check

How it differs from enabling more rules yourself:

  • Every finding has a gap code. TR101 is the array example above, TR301 is any from JSON.parse, TR501 is the unhandled promise. Each code has a page with an example, the reason TypeScript allows it, how typerail detects it and how to fix it. The catalog has 54 codes.
  • Every example is a test. The test suite compiles each example and runs it. When a TypeScript release closes a gap, a test fails and names the code.
  • Findings are ranked by consequence, from “can stop the process” to “no runtime effect”, each with a fix. typerail report writes them to a file in that order.
  • The project can’t weaken it. typerail ignores the flags in the project’s tsconfig.json, its ESLint configuration and // eslint-disable comments. This matters with coding agents, which often get a failing check to pass with as any or a disabled rule. typerail init adds instructions for them to AGENTS.md.
  • It works on existing code. typerail check --update-baseline records the current findings. After that, CI fails only on new findings.

It leaves some things out on purpose. It doesn’t check style, such as throwing versus returning errors, loop forms or == versus ===. It doesn’t repeat what tsc and ESLint already report. Some gaps in the catalog are marked “not checked yet”, because we haven’t found a way to report them without too many false positives.

Trying it

typerail is open source under the MIT license and published on npm as @wingravity/typerail. It needs Node 22.13 or later and a tsconfig.json:

npx @wingravity/typerail check
npx @wingravity/typerail report

If it reports something that can’t fail, or misses something that can, please open an issue. The false positives we’ve fixed so far all came from running it on real projects.

Related posts

Share

December 1, 2026

Daniel Berigoi
Daniel Berigoi

CTO @ Wingravity and Co-Founder of Slashscore. Passionate about building tools that empower developers and teams. Have a question or want to collaborate? Reach out via our contact form.