|
| 1 | +# TypeScript Style Guide |
| 2 | + |
| 3 | +[](https://tinyurl.com/2h9aktmd) [](https://tinyurl.com/d4xnrptz) [](https://tinyurl.com/mr22naua) [](https://tinyurl.com/e8ymxdw3) |
| 4 | + |
| 5 | +This guide provides a set of guidelines and best practices for writing TypeScript code. Adhering to these standards will improve code readability, maintainability, and collaboration within your project. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +1. [General Guidelines](#general-guidelines) |
| 10 | +2. [Naming Conventions](#naming-conventions) |
| 11 | +3. [Types](#types) |
| 12 | +4. [Interfaces](#interfaces) |
| 13 | +5. [Classes](#classes) |
| 14 | +6. [Functions](#functions) |
| 15 | +7. [Variables](#variables) |
| 16 | +8. [Modules](#modules) |
| 17 | +9. [Error Handling](#error-handling) |
| 18 | +10. [Formatting](#formatting) |
| 19 | + |
| 20 | +## General Guidelines |
| 21 | + |
| 22 | +- Always use strict mode by enabling the `strict` compiler option. |
| 23 | +- Use meaningful and descriptive names for variables, functions, classes, etc. |
| 24 | +- Keep lines of code short and concise. Aim for a maximum of 80 characters per line. |
| 25 | +- Comment your code to explain complex logic or any non-obvious behavior. |
| 26 | + |
| 27 | +## Naming Conventions |
| 28 | + |
| 29 | +- Use camelCase for variables, functions, and class members. |
| 30 | +- Use PascalCase for classes, interfaces, and type names. |
| 31 | +- Use uppercase snake case for constants and enum values. |
| 32 | +- Prefix interfaces with `I`. |
| 33 | +- Use clear and descriptive names, even if it results in longer names. |
| 34 | + |
| 35 | +## Types |
| 36 | + |
| 37 | +- Prefer TypeScript's built-in types (`string`, `number`, `boolean`, etc.) over their JavaScript counterparts (`String`, `Number`, `Boolean`, etc.). |
| 38 | +- Use union types (`|`) and intersection types (`&`) when appropriate. |
| 39 | +- Avoid using the `any` type whenever possible. Instead, be explicit about the types. |
| 40 | +- Use type inference when the type is obvious and doesn't harm readability. |
| 41 | + |
| 42 | +## Interfaces |
| 43 | + |
| 44 | +- Define interfaces for objects or classes that share a common structure. |
| 45 | +- Name interfaces using PascalCase. |
| 46 | +- Use optional properties (`?`) or default values sparingly. |
| 47 | +- Prefer readonly properties where applicable. |
| 48 | + |
| 49 | +## Classes |
| 50 | + |
| 51 | +- Use classes to represent complex data structures or encapsulate behavior. |
| 52 | +- Use the `public` modifier explicitly for clarity, as it's the default access modifier. |
| 53 | +- Keep the number of properties and methods in a class to a minimum for better cohesion. |
| 54 | +- Prefer composition over inheritance, unless there's a strong need for inheritance. |
| 55 | + |
| 56 | +## Functions |
| 57 | + |
| 58 | +- Use arrow functions (`() => {}`) for short, concise functions and when lexical scoping is required. |
| 59 | +- Use regular functions (`function () {}`) for methods and when dynamic scoping is needed. |
| 60 | +- Prefer explicit return types for functions to enhance readability. |
| 61 | +- Use default parameters to provide optional arguments when it makes sense. |
| 62 | + |
| 63 | +## Variables |
| 64 | + |
| 65 | +- Use `const` for values that should not be reassigned. |
| 66 | +- Use `let` for variables that can be reassigned. |
| 67 | +- Avoid using `var` unless you have a specific reason to use it. |
| 68 | + |
| 69 | +## Modules |
| 70 | + |
| 71 | +- Use ES modules (`import`/`export`) for managing dependencies. |
| 72 | +- Prefer named exports over default exports to encourage explicit imports. |
| 73 | +- Organize imports in alphabetical order and group them by third-party dependencies and local modules. |
| 74 | + |
| 75 | +## Error Handling |
| 76 | + |
| 77 | +- Use proper error handling techniques, such as `try-catch` blocks, for handling expected errors. |
| 78 | +- Throw specific error types instead of generic `Error` objects. |
| 79 | +- Avoid swallowing errors by using empty `catch` blocks. |
| 80 | + |
| 81 | +## Formatting |
| 82 | + |
| 83 | +- Use two spaces for indentation. Do not use tabs. |
| 84 | +- Use single quotes (`'`) for string literals, unless escaping is necessary. |
| 85 | +- Use semicolons at the end of each statement. |
| 86 | +- Put spaces around operators (`+, -, *, /, =, ===, etc.`) for improved readability. |
| 87 | +- Break long statements or expressions into multiple lines for better readability. |
0 commit comments