Implementing TypeScript in Node.js Projects for Enhanced Type Safety
Guidelines for adopting TypeScript in Node.js applications to improve code quality and maintainability through static typing.
0 likes
174 views
Rule Content
title: Implementing TypeScript in Node.js Projects for Enhanced Type Safety
description: Guidelines for adopting TypeScript in Node.js applications to improve code quality and maintainability through static typing.
category: Node.js Cursor Rules
globs:
- "**/*.ts"
- "**/*.tsx"
rules:
- description: "Enable strict mode in TypeScript to catch more errors at compile time and enforce better coding practices."
action: "Ensure 'strict' is set to true in tsconfig.json."
trigger: "Presence of tsconfig.json without 'strict' set to true."
outcome: "Improved code reliability and maintainability through stricter type checking."
- description: "Use interfaces and types to define the shape of your data, improving code readability and catching type-related issues early."
action: "Define interfaces for objects and types for unions, intersections, and other complex structures."
trigger: "Usage of 'any' type where a more specific type can be defined."
outcome: "Enhanced code clarity and early detection of type-related errors."
- description: "Avoid using the 'any' type unless absolutely necessary to maintain type safety."
action: "Replace 'any' with specific types or use type inference where possible."
trigger: "Instances of 'any' type in the codebase."
outcome: "Stronger type safety and reduced runtime errors."
- description: "Follow consistent naming conventions for variables, functions, classes, and interfaces to improve code readability."
action: "Use camelCase for variables and functions, and PascalCase for classes and interfaces."
trigger: "Inconsistent naming conventions detected in the codebase."
outcome: "Improved code readability and maintainability."
- description: "Use a code formatter like Prettier to maintain a consistent code style across the project."
action: "Integrate Prettier into the development workflow and configure it to format TypeScript files on save."
trigger: "Unformatted code or inconsistent code style detected."
outcome: "Consistent code style and reduced code review overhead."
- description: "Separate database logic into its own layer to maintain a clean architecture."
action: "Organize code into distinct layers, keeping database interactions separate from business logic."
trigger: "Database logic found within service or controller layers."
outcome: "Improved code modularity and easier maintenance."
- description: "Use async/await for asynchronous code to make it more readable and maintainable."
action: "Refactor callback-based or promise-based asynchronous code to use async/await syntax."
trigger: "Presence of callback-based or promise-based asynchronous code."
outcome: "More readable and maintainable asynchronous code."
- description: "Implement proper error handling using try/catch blocks to build robust applications."
action: "Wrap asynchronous operations in try/catch blocks and handle errors appropriately."
trigger: "Asynchronous operations without error handling."
outcome: "Enhanced application stability and user experience."
- description: "Use generics to write reusable code that can work with multiple types."
action: "Define generic functions and classes where applicable to increase code reusability."
trigger: "Functions or classes that can benefit from generic types but are using specific types."
outcome: "More flexible and reusable code."
- description: "Set up ESLint with TypeScript-specific plugins to enforce coding standards and catch potential issues."
action: "Configure ESLint with '@typescript-eslint/parser' and '@typescript-eslint/eslint-plugin'."
trigger: "Absence of ESLint configuration or missing TypeScript-specific plugins."
outcome: "Consistent code quality and early detection of potential issues."