|
| 1 | +--- |
| 2 | +description: Guidelines for using modern TypeScript features (v5.0-v5.8) |
| 3 | +applyTo: "**/*.{ts,tsx,mts,cts}" |
| 4 | +--- |
| 5 | + |
| 6 | +# TypeScript Coding Guidelines & Modern Features (v5.0 - v5.8) |
| 7 | + |
| 8 | +When writing TypeScript code, prioritize using modern features and best practices introduced in recent versions (up to 5.8). |
| 9 | + |
| 10 | +## Global Themes Across 5.x |
| 11 | + |
| 12 | +1. **Standard decorators are here; legacy decorators are legacy.** |
| 13 | + New TC39-compliant decorators landed in 5.0 and were extended in 5.2 (metadata). Old `experimentalDecorators`-style behavior is still supported but should be treated as legacy. |
| 14 | + |
| 15 | +2. **Type system is more precise and less noisy.** |
| 16 | + Major work went into narrowing, control flow analysis, error messages, and new helpers like `NoInfer`, inferred predicates, and better `undefined`/`never`/uninitialized checks. |
| 17 | + |
| 18 | +3. **Module / runtime interop has been modernized.** |
| 19 | + Options like `--moduleResolution bundler`, `--module nodenext`/`node18`, `--rewriteRelativeImportExtensions`, `--erasableSyntaxOnly`, and `--verbatimModuleSyntax` are about playing nicely with ESM, Node 18+/22+, direct TypeScript execution, and bundlers. |
| 20 | + |
| 21 | +4. **The standard library keeps tracking modern JS.** |
| 22 | + Support for new ES features (iterator helpers, `Object.groupBy`/`Map.groupBy`, new Set/ES2024 APIs) shows up as type declarations and sometimes extra checks (regex syntax checking, etc.). |
| 23 | + |
| 24 | +When generating or refactoring code, prefer these newer idioms, and avoid patterns that conflict with updated checks. |
| 25 | + |
| 26 | +## Modern Features to Utilize |
| 27 | + |
| 28 | +### Type System & Inference |
| 29 | +- **`const` Type Parameters (5.0)**: Use `const` type parameters for more precise literal inference. |
| 30 | + ```typescript |
| 31 | + declare function names<const T extends string[]>(...names: T): void; |
| 32 | + ``` |
| 33 | +- **`@satisfies` Operator (5.0)**: Use `satisfies` to validate types without widening them. |
| 34 | +- **Inferred Type Predicates (5.5)**: Allow TypeScript to infer type predicates for functions that filter arrays or check types, reducing the need for explicit `is` return types. |
| 35 | +- **`NoInfer` Utility (5.4)**: Use `NoInfer<T>` to block inference for specific type arguments when you want them to be determined by other arguments. |
| 36 | +- **Narrowing**: |
| 37 | + - **Switch(true) (5.3)**: Utilize narrowing in `switch(true)` blocks. |
| 38 | + - **Boolean Comparisons (5.3)**: Rely on narrowing from direct boolean comparisons. |
| 39 | + - **Closures (5.4)**: Trust preserved narrowing in closures when variables aren't modified after the check. |
| 40 | + - **Constant Indexed Access (5.5)**: Use constant indices to narrow object/array properties. |
| 41 | + |
| 42 | +### Syntax & Control Flow |
| 43 | +- **Decorators (5.0)**: Use standard ECMAScript decorators (Stage 3). |
| 44 | +- **`using` Declarations (5.2)**: Use `using` for explicit resource management (Disposable pattern) instead of manual cleanup. |
| 45 | + ```typescript |
| 46 | + using resource = new Resource(); |
| 47 | + ``` |
| 48 | +- **Import Attributes (5.3/5.8)**: Use `with { type: "json" }` for import attributes. Avoid the deprecated `assert` syntax. |
| 49 | +- **`switch` Exhaustiveness**: Rely on TypeScript's exhaustiveness checking in switch statements. |
| 50 | + |
| 51 | +### Modules & Imports |
| 52 | +- **`verbatimModuleSyntax` (5.0)**: Respect this flag by using `import type` explicitly when importing types to ensure they are erased during compilation. |
| 53 | +- **Type-Only Imports with Extensions (5.2)**: You can use `.ts`, `.mts`, `.cts` extensions in `import type` statements. |
| 54 | +- **`resolution-mode` (5.3)**: Use `import type { Type } from "mod" with { "resolution-mode": "import" }` if needed for specific module resolution contexts. |
| 55 | +- **JSDoc `@import` (5.5)**: Use `@import` tags in JSDoc for cleaner type imports in JS files if working in a mixed codebase. |
| 56 | + |
| 57 | +### Standard Library & Built-ins |
| 58 | +- **Iterator Helpers (5.6)**: Use new iterator methods (map, filter, etc.) if targeting modern environments. |
| 59 | +- **Set Methods (5.5)**: Utilize new `Set` methods like `union`, `intersection`, etc., when available. |
| 60 | +- **`Object.groupBy` / `Map.groupBy` (5.4)**: Use these standard methods for grouping instead of external libraries like Lodash when appropriate. |
| 61 | +- **`Promise.withResolvers` (5.7)**: Use `Promise.withResolvers()` for creating promises with exposed resolve/reject functions. |
| 62 | + |
| 63 | +### Configuration & Tooling |
| 64 | +- **`--moduleResolution bundler` (5.0)**: Assume this resolution strategy for modern web projects (Vite, Next.js, etc.). |
| 65 | +- **`--erasableSyntaxOnly` (5.8)**: Be aware of this flag; avoid TypeScript-specific syntax that cannot be simply erased (like `enum`s or `namespaces`) if the project aims for maximum compatibility with tools like Node.js's `--strip-types`. Prefer `const` objects or unions over `enum`s if requested. |
| 66 | + |
| 67 | +## Specific Coding Patterns |
| 68 | + |
| 69 | +### Arrays & Collections |
| 70 | +- Use **Copying Array Methods (5.2)** (`toSorted`, `toSpliced`, `with`) for immutable array operations. |
| 71 | +- **TypedArrays (5.7)**: Be aware that TypedArrays are now generic over `ArrayBufferLike`. |
| 72 | + |
| 73 | +### Classes |
| 74 | +- **Parameter Decorators (5.0/5.2)**: Use modern standard decorators. |
| 75 | +- **`super` Property Access (5.3)**: Avoid accessing instance fields via `super`. |
| 76 | + |
| 77 | +### Error Handling |
| 78 | +- **Checks for Never-Initialized Variables (5.7)**: Ensure variables are initialized before use to avoid new errors. |
| 79 | + |
| 80 | +## Deprecations to Avoid |
| 81 | +- Avoid `import ... assert` (use `with`). |
| 82 | +- Avoid implicit `any` returns in `undefined`-returning functions (though 5.1 makes this easier, explicit is better). |
| 83 | +- Avoid `enum`s if the project prefers erasable syntax (5.8). |
| 84 | + |
| 85 | +## Version-Specific Highlights |
| 86 | + |
| 87 | +### TypeScript 5.0 |
| 88 | +- **Decorators**: Use standard decorators unless `experimentalDecorators` is explicitly enabled. |
| 89 | +- **`const` Type Parameters**: Use for literal inference. |
| 90 | +- **Enums**: All enums are union enums. |
| 91 | +- **Modules**: `--moduleResolution bundler` and `--verbatimModuleSyntax` are key for modern bundlers. |
| 92 | + |
| 93 | +### TypeScript 5.1 |
| 94 | +- **Returns**: `undefined`-returning functions don't need explicit returns. |
| 95 | +- **Getters/Setters**: Can have unrelated types with explicit annotations. |
| 96 | + |
| 97 | +### TypeScript 5.2 |
| 98 | +- **Resource Management**: `using` declarations for `Symbol.dispose`. |
| 99 | +- **Decorator Metadata**: Use `context.metadata` for design-time metadata. |
| 100 | + |
| 101 | +### TypeScript 5.3 |
| 102 | +- **Import Attributes**: Use `with { type: "json" }`. |
| 103 | +- **Switch(true)**: Narrowing works in `switch(true)`. |
| 104 | + |
| 105 | +### TypeScript 5.4 |
| 106 | +- **Closures**: Narrowing preserved in closures if last assignment is before creation. |
| 107 | +- **`NoInfer`**: Block inference for specific arguments. |
| 108 | +- **Grouping**: `Object.groupBy` / `Map.groupBy`. |
| 109 | + |
| 110 | +### TypeScript 5.5 |
| 111 | +- **Inferred Predicates**: Functions checking types often don't need explicit `is` return types. |
| 112 | +- **Constant Index Access**: Better narrowing for constant keys. |
| 113 | +- **Regex**: Syntax checking for regex literals. |
| 114 | + |
| 115 | +### TypeScript 5.6 |
| 116 | +- **Truthiness Checks**: Errors on always-truthy/falsy conditions (e.g., `if (/regex/)`). |
| 117 | +- **Iterator Helpers**: `.map`, `.filter` on iterators. |
| 118 | + |
| 119 | +### TypeScript 5.7 |
| 120 | +- **Uninitialized Variables**: Stricter checks for never-initialized variables. |
| 121 | +- **Relative Imports**: `--rewriteRelativeImportExtensions` for `.ts` imports in output. |
| 122 | +- **ES2024**: Support for `Promise.withResolvers`, `Atomics.waitAsync`. |
| 123 | + |
| 124 | +### TypeScript 5.8 |
| 125 | +- **Return Checks**: Granular checks for conditional returns. |
| 126 | +- **Node Modules**: `--module node18` stable; `require()` of ESM allowed in `nodenext`. |
| 127 | +- **Erasable Syntax**: `--erasableSyntaxOnly` forbids enums, namespaces, etc. |
| 128 | + |
| 129 | +When generating code, always prefer the most modern, standard, and type-safe approach available in TypeScript 5.8. |
0 commit comments