All checklists

TypeScript code review checklist

The usual TypeScript code review trap is that types vanish at runtime. This checklist covers the mistakes Revix checks for in pull requests. Look for non-null assertions on unchecked values, request bodies cast to a type instead of validated, and DOM lookups cast instead of checked. Each rule shows the code to flag, the fix and its source.

Oscar Estudillo
Updated
  1. Validate runtime data explicitly
  2. Avoid unverified non-null assertions
  3. Do not rely on assertions for runtime validation
  4. Make discriminated-union switches exhaustive
  5. Use children for element-only traversal
  6. Do not add unused generic parameters
  7. Disallow angle-bracket type assertions in .tsx files
  8. Replace deprecated amd-dependency directives

8 rules to check in every TypeScript pull request

Revix reviews TypeScript pull requests against 310 rules. This page shows a short list we picked from them. Each rule has a bad example, a good example and a source. We check each rule against its source and update the list every month. Rules marked as flagged by Revix have come up in real reviews.

Validate runtime data explicitly

A TypeScript type describes what the compiler assumes, not what an incoming runtime value actually is.

Avoid

const user = req.body as User;
return user.email.toLowerCase();

Prefer

const user = UserSchema.parse(req.body);
return user.email.toLowerCase();

Flagged by Revix in real reviews · Security · All current versions · Source: TypeScript for the New Programmer

Avoid unverified non-null assertions

The handbook says the operator does not change runtime behavior and is safe only when the value cannot be null or undefined.

Avoid

function print(x?: number | null) {
  console.log(x!.toFixed());
}

Prefer

function print(x?: number | null) {
  if (x == null) return;
  console.log(x.toFixed());
}

Flagged by Revix in real reviews · Security · All current versions · Source: Everyday Types

Do not rely on assertions for runtime validation

The handbook explicitly warns that assertions do not affect runtime behavior and do not generate an exception or null when the assertion is wrong.

Avoid

const canvas = document.getElementById("main") as HTMLCanvasElement;
canvas.getContext("2d");

Prefer

const node = document.getElementById("main");
if (!(node instanceof HTMLCanvasElement)) throw new Error("Expected canvas");
node.getContext("2d");

Flagged by Revix in real reviews · Security · All current versions · Source: Everyday Types

Make discriminated-union switches exhaustive

The never assignment turns an unhandled union member into a compile-time error when the union is later expanded.

Avoid

switch (shape.kind) { case "circle": return area(shape); case "square": return area(shape); }

Prefer

default: { const unreachable: never = shape; return unreachable; }

Flagged by Revix in real reviews · Best practice · All current versions · Source: Narrowing

Use children for element-only traversal

Processing childNodes as though every entry were an HTMLElement can include text nodes and produce incorrect traversal or runtime behavior.

Avoid

for (const node of div.childNodes) {
  node.classList.add("item");
}

Prefer

for (const element of div.children) {
  element.classList.add("item");
}

Flagged by Revix in real reviews · API usage · All current versions · Source: DOM Manipulation — Difference between children and childNodes

Do not add unused generic parameters

The documentation states that type parameters should propagate type information and shows a generic length function whose single-use T is unnecessary.

Avoid

function length<T extends ArrayLike<unknown>>(t:T): number { return t.length; }

Prefer

function length(t: ArrayLike<unknown>): number { return t.length; }

Flagged by Revix in real reviews · API usage · All current versions · Source: TypeScript for Functional Programmers

Disallow angle-bracket type assertions in .tsx files

Using angle-bracket type assertions in .tsx files causes parsing ambiguities with JSX syntax, which could lead to compilation errors or unexpected behavior.

Avoid

const foo = <Foo>bar;

Prefer

const foo = bar as Foo;

Deprecation · All current versions · Source: TypeScript JSX Documentation

Replace deprecated amd-dependency directives

The directive is deprecated and can be replaced with the standard module dependency syntax.

Avoid

/// <amd-dependency path="legacy/moduleA" />

Prefer

import "legacy/moduleA";

Deprecation · All current versions · Source: Triple-Slash Directives

Deprecated TypeScript APIs to flag in review

These rules catch code that still works today but leans on an API that TypeScript has deprecated or replaced.

  1. Disallow angle-bracket type assertions in .tsx files
  2. Replace deprecated amd-dependency directives

How Revix applies these rules in pull requests

Revix matches these rules against the code each pull request changes. When a comment rests on one, it names the rule, so the author can read the reasoning and the source instead of taking the bot's word for it.

const user = req.body as User;
return user.email.toLowerCase();

revixappdevBot

issue: A TypeScript type describes what the compiler assumes, not what an incoming runtime value actually is.

📚 Rule (TypeScript): Validate runtime data explicitly

How the comment reads on a GitHub pull request when a change breaks the first rule on this page.

Reviews run natively on GitHub, GitLab and Bitbucket.

Revix also learns your team's own rules from the code you already merged. Start a free trial and see them on your next pull request.