name: code-review-ts description: TypeScript-specific code review guidelines focusing on type safety and TypeScript idioms. Use when: (1) reviewing TypeScript pull requests, (2) auditing type-safety regressions, (3) checking TS API and error-handling patterns.
category: audit disable-model-invocation: false user-invocable: true allowed-tools: Read, Grep, Glob, Bash
TypeScript Code Review Guidelines
This guide defines how the reviewer evaluates a TypeScript pull request.
Baseline Assumptions
- The code compiles.
- All tests pass.
Normative Words
- MUST: Mandatory. Not following this is a violation of the guide.
- MUST NOT: Forbidden.
- SHOULD: Recommended in almost all cases; exceptions need a strong reason.
- SHOULD NOT: Generally discouraged; only do it with clear justification.
- MAY: Optional; use judgment.
Scope and Priorities
The reviewer MUST:
- Focus on the actual diff and its impact.
- Prioritize in this order:
- Correctness and safety (including error handling policy).
- Public API and external behavior.
- Concurrency and performance issues with real impact.
- Readability, idioms, maintainability.
The reviewer MUST NOT:
- Invent business logic or protocol rules not implied by the code or docs.
- Demand large unrelated refactors unless there is a clear correctness or safety concern.
Review Process
- Read the PR description and understand the intent
- Review the diff file by file
- For each change, consider:
- Does this introduce bugs or security issues?
- Is the API appropriate?
- Are edge cases handled?
- Is error handling adequate?
- Provide actionable, specific feedback
- Distinguish blocking issues from suggestions
Feedback Format
Use clear prefixes:
- MUST FIX: Blocking issue that needs resolution
- SHOULD FIX: Strong recommendation
- CONSIDER: Optional improvement
- QUESTION: Clarification needed
TypeScript-Specific Rules
Prefer Strong Types; Avoid Type Inspection on Known Types
The reviewer MUST:
- Avoid runtime type inspection (
typeof,instanceof) when the type is known or enforced by TypeScript. - Restrict runtime checks to untyped inputs or boundary validation (e.g., API payloads).
Discouraged:
function labelCount(count: number) {
if (typeof count === "number") return `${count} items`;
return "n/a";
}
Avoid any and unknown When Possible
The reviewer MUST:
- Reject
anyunless it is a last-resort boundary with clear justification. - Require immediate narrowing of
unknownwith explicit type guards.
The reviewer SHOULD:
- Prefer generics,
satisfies, and well-scoped interfaces overany.
Type Casting Must Be Justified
The reviewer MUST:
- Require a comment or invariant when using
as, non-null assertions, or unsafe casts. - Prefer
satisfiesor type guards before asserting a type.
Acceptable with proof:
const payload = parse(input) as Payload; // validated by parse schema
Avoid Closures That Capture Large Scopes
The reviewer MUST:
- Flag arrow functions or closures that capture
this,init,options, or request bodies when passed to long-lived event listeners. - Prefer
bind()so only the necessary reference is retained.
An arrow function such as () => controller.abort() captures the surrounding scope, which can include request bodies and other large objects. If a long-lived AbortSignal is used, the event listener prevents those objects from being garbage-collected for the lifetime of the signal.
Discouraged:
signal.addEventListener("abort", () => controller.abort());
Preferred:
signal.addEventListener("abort", controller.abort.bind(controller));
Circular Dependency Detection
The reviewer MUST:
- Flag import cycles when errors like "Cannot access 'X' before initialization" appear
- Recommend
madge --circular --extensions ts,tsx src/for cycle detection
Common resolution strategies:
- Extract shared dependencies to separate modules
- Use dependency injection instead of direct imports
- Use
import typefor type-only imports (erased at runtime) - Restructure barrel files (
index.ts) to avoid re-export cycles
The reviewer SHOULD:
- Check for barrel file re-export cycles (common source of issues)
- Verify Jest/Vitest module resolution matches bundler behavior
Additional TypeScript Guidelines
The reviewer SHOULD:
- Encourage use of strict TypeScript compiler options
- Prefer
readonlyfor immutable data - Use discriminated unions over type assertions
- Prefer
unknownoveranyfor external data - Use template literal types where appropriate