|
| 1 | +--- |
| 2 | +applyTo: "**/*.ts, **/*.mts, **/*.cts" |
| 3 | +--- |
| 4 | + |
| 5 | +# TypeScript Coding Standards for This Project |
| 6 | + |
| 7 | +These guidelines complement the existing **JavaScript** rules and are **binding** for all TypeScript source files. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 1 Compiler Baseline (`tsconfig.json`) |
| 12 | + |
| 13 | +The project’s `tsconfig.json` is **authoritative**. Do **not** change it. |
| 14 | + |
| 15 | +```json |
| 16 | +{ |
| 17 | + "compilerOptions": { |
| 18 | + "target": "es2022", |
| 19 | + "module": "NodeNext", |
| 20 | + "esModuleInterop": true, |
| 21 | + "allowJs": true, |
| 22 | + "resolveJsonModule": true, |
| 23 | + "isolatedModules": true, |
| 24 | + "verbatimModuleSyntax": true, |
| 25 | + |
| 26 | + "strict": true, |
| 27 | + "strictNullChecks": true, |
| 28 | + "noImplicitOverride": true, |
| 29 | + "noImplicitThis": true, |
| 30 | + "noImplicitReturns": true, |
| 31 | + "noUnusedLocals": true, |
| 32 | + |
| 33 | + "skipLibCheck": true, |
| 34 | + "forceConsistentCasingInFileNames": true, |
| 35 | + |
| 36 | + "sourceMap": true, |
| 37 | + "declaration": true, |
| 38 | + "declarationMap": true, |
| 39 | + "experimentalDecorators": true, |
| 40 | + "lib": ["es2022"] |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +Key implications: |
| 46 | + |
| 47 | +* **ESM only** (`module: "NodeNext"`, `verbatimModuleSyntax: true`). Use `import`/`export`, never CommonJS `require`. |
| 48 | +* **Target ES2022** → you may use class fields, `at()` on arrays, `Error.cause`, `RegExp matchIndices`, etc. |
| 49 | +* **`strict` mode** with additional checks: treat all compile‑time warnings as **errors**. |
| 50 | +* **`allowJs`: true** means you *may* import legacy `.js` files; new code **must** be `.ts`/`.tsx`. |
| 51 | +* **Decorators** are enabled (`experimentalDecorators`). Use them sparingly and document intent. |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## 2 General Style |
| 56 | + |
| 57 | +(The JavaScript standards apply; this section covers TS‑specifics.) |
| 58 | + |
| 59 | +| Topic | Rule | |
| 60 | +| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 61 | +| **Types vs Interfaces** | Prefer **`type` aliases** for unions, mapped & conditional types. <br>Use **`interface`** for plain object shapes intended for extension. | |
| 62 | +| **Explicit typing** | Always annotate **public APIs** (exported functions, class members, constants) with explicit types.<br>Local variables may rely on inference if trivial. | |
| 63 | +| **`any` / `unknown`** | `any` is allowed by config but **should be avoided**; use `unknown` with proper narrowing. | |
| 64 | +| **Nullish values** | Handle `null` / `undefined` explicitly (`strictNullChecks`). Use the *nullish coalescing* operator (`??`) or early returns. | |
| 65 | +| **Enums** | Avoid `enum`; prefer `as const` objects or union literal types for safer exhaustiveness. | |
| 66 | +| **Tuples & readonly** | Mark tuples as `readonly` when contents shouldn’t mutate (`readonly [T1, T2]`). | |
| 67 | +| **Generics** | Provide descriptive parameter names (`TData`, `TError`). Supply default type parameters when helpful. | |
| 68 | +| **Decorators** | Keep decorator logic *pure*; avoid hidden side‑effects. Document with JSDoc above the decorator. | |
| 69 | +| **Assertions & type casts** | Prefer `as` casting; avoid the angle‑bracket form. Keep casts to the narrowest scope possible. | |
| 70 | +| **Error handling** | Use custom `class`es extending `Error` with a descriptive **PascalCase** name and optional `cause`. | |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## 3 Imports & Module Resolution |
| 75 | + |
| 76 | +1. **Order** exactly as in the JavaScript guide: core → third‑party → internal. |
| 77 | + Always include file extensions (e.g. `"./util.js"`). |
| 78 | +2. **JSON modules** can be imported directly (`resolveJsonModule: true`). |
| 79 | + |
| 80 | + ```ts |
| 81 | + import packageJson from "../package.json" assert { type: "json" }; |
| 82 | + ``` |
| 83 | +3. **Type‑only imports**: use the `import type { … } from "…";` form to avoid runtime overhead. |
| 84 | +4. Keep each import statement **on its own line**. |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## 4 Naming Conventions |
| 89 | + |
| 90 | +Follow the same table as the JavaScript guide, with additions: |
| 91 | + |
| 92 | +* **Type parameters**: `T`, `TValue`, `TOptions` (PascalCase prefixed with `T`). |
| 93 | +* **Namespace imports** (rare): camelCase (e.g. `import * as fs from "node:fs/promises";`). |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## 5 Testing |
| 98 | + |
| 99 | +* Write tests in **TypeScript** (preferred extension `.spec.ts`). |
| 100 | +* Use **Node.js core test runner** (`node:test`). Configure the runner to load `tsx` or `ts-node/register` for runtime transpilation. |
0 commit comments