TypeScript5

TypeScript coding guidance covering compiler settings, type definitions, and common patterns for safer application and library code.

In plain words
What is it for?
Use it when configuring tsconfig.json, choosing TypeScript types, designing functions and objects, handling modules or JSX, and preparing libraries to generate declaration files.
Why use it?
It helps avoid weak types and unclear compiler settings that can let errors pass unnoticed or make code harder to maintain.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/thesethrose/devrules/typescript5
Clone the repo
git clone --depth 1 https://github.com/TheSethRose/DevRules

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 932 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00932
Opus 5 $0.00000 $0.00466
Sonnet 5 $0.00000 $0.00186
Haiku 4.5 $0.00000 $0.00093

Measured yesterday against content hash 304d45225e9d, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

TypeScript5 scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/languages/TypeScript5.mdc · 62 lines

How it starts

The opening of the file, as written. The whole thing — 62 lines — stays where its author put it; the contents beside it link to each section on GitHub.

TypeScript Best Practices

tsconfig.json Configuration

  • Enable strict mode (or individual strict flags like strictNullChecks, noImplicitAny, strictFunctionTypes, etc.). This is the most crucial step for leveraging TypeScript's safety features.
  • Configure target and module appropriately for your runtime environment (e.g., ES2022 or higher for modern Node/browsers, appropriate module system like NodeNext or ESNext).
  • Set up baseUrl and paths for non-relative module resolution if needed, especially in larger projects or monorepos.
  • Understand compiler options related to JSX (jsx, jsxFactory, etc.) if using React/JSX.
  • Configure declaration file generation (declaration, declarationMap, outDir) if building a library.

Type System & Definitions

  • Avoid any. Use unknown when the type is truly unknown and perform type checking/narrowing before use.
  • Use explicit types for function parameters, return values, and variable declarations where inference is not sufficient or clear.
  • Prefer specific types over overly broad ones (e.g., string over any, "success" | "error" over string).
  • Use interface for defining the shape of objects or implementing classes.
  • Use type for creating aliases for unions, intersections, tuples, primitives, and using utility types.
  • Leverage built-in Utility Types (Partial, Required, Readonly, Pick, Omit, Record, etc.) to manipulate existing types effectively.
  • Use Generics (<T>) to create reusable components, functions, and types that can work over a variety of types.
  • Use readonly modifiers for properties that should not be reassigned after object creation.
  • Utilize Discriminated Unions (tagged unions) with literal types to create type-safe state machines or variant types.

Type Narrowing & Guards

  • Rely on TypeScript's control flow analysis for automatic type narrowing (e.g., within if checks, switch statements, instanceof checks).
  • Use typeof checks for primitives.
  • Use instanceof checks for classes.
  • Use the in operator to check for property existence.
  • Create custom Type Guard functions (value is Type) for complex validation logic.

Read the full file on GitHub · 62 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 62 lines · 932 tokens per session scan A 304d45225e9d

Subscribe to this mod's changes

TypeScript5 is a cursor rule published in the GitHub repository TheSethRose/DevRules (25 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 932 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.