Best practices for TypeScript Strict Mode Standards
.cursorrules in your project root# TypeScript Strict Mode Standards
=====================================
## Core Principles
-------------------
* **Type Safety**: Ensure that all variables, function parameters, and return types are properly typed to prevent type-related errors.
* **Code Quality**: Enforce strict coding standards to maintain high-quality, readable, and maintainable code.
* **Error Prevention**: Use TypeScript's strict mode features to catch errors at compile-time rather than runtime.
## Code Style Guidelines
------------------------
### Naming Conventions
* **Variable Names**: Use camelCase for variable names, with the first letter in lowercase (e.g., `userName`).
* **Function Names**: Use camelCase for function names, with the first letter in lowercase (e.g., `getUserName`).
* **Class Names**: Use PascalCase for class names, with the first letter in uppercase (e.g., `UserName`).
* **Interface Names**: Use PascalCase for interface names, with the first letter in uppercase (e.g., `IUserName`).
### Typing
* **Type Annotations**: Use type annotations for all variables, function parameters, and return types.
* **Type Inference**: Use type inference for variables and function return types when possible.
* **No Implicit Any**: Avoid using the `any` type; instead, use specific types or interfaces.
### Code Organization
* **File Structure**: Organize code into logical folders and files, with a clear and consistent naming convention.
* **Import Statements**: Use ES6-style import statements, with imports at the top of the file.
## Best Practices
------------------
* **Use Strict Mode**: Enable strict mode in the `tsconfig.json` file to enable all strict type checking options.
* **Use Interfaces**: Use interfaces to define the shape of objects and functions.
* **Use Type Guards**: Use type guards to narrow the type of a value within a specific scope.
* **Use Enums**: Use enums to define a set of named values.
* **Avoid Null and Undefined**: Avoid using null and undefined as types; instead, use optional types (e.g., `string | undefined`).
## Common Pitfalls to Avoid
---------------------------
* **Implicit Any**: Avoid using the `any` type, as it can lead to type-related errors.
* **Unused Variables**: Avoid declaring unused variables, as they can lead to confusion and errors.
* **Unreachable Code**: Avoid writing unreachable code, as it can lead to confusion and errors.
* **Type Assertions**: Avoid using type assertions (e.g., `as any`) unless absolutely necessary, as they can lead to type-related errors.
## Example Use Cases
---------------------
### Variable Declaration
```typescript
// Good
let userName: string = 'John Doe';
// Bad
let userName = 'John Doe'; // implicit any
```
### Function Declaration
```typescript
// Good
function getUserName(): string {
return 'John Doe';
}
// Bad
function getUserName() { // implicit any return type
return 'John Doe';
}
```
### Interface Declaration
```typescript
// Good
interface User {
name: string;
age: number;
}
// Bad
interface User {
name: any; // implicit any
age: number;
}
```Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install TypeScript Strict Mode Standards.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.