Conventions and best practices for writing maintainable TypeScript code based on the Google TypeScript Style Guide. Use this skill when writing new TypeScript modules or services, refactoring TypeScript code to improve readability and maintainability, enforcing consistent naming, typing, and formatting conventions, designing strongly typed APIs, interfaces, and classes, avoiding unsafe patterns such as `any`, prototype modification, or loose equality, structuring large TypeScript codebases with predictable patterns, and implementing safe and maintainable code in Node.js or browser TypeScript projects. Emphasizes strong typing, clear naming conventions, strict equality usage, minimal visibility exposure, avoiding dangerous JavaScript features, and consistent formatting. Reference: https://google.github.io/styleguide/tsguide.html
This skill follows the Google TypeScript Style Guide and provides conventions and best practices for writing consistent, maintainable, and type-safe TypeScript code.
const and let: Use const by default. Use let only if reassignment is required. Never use var.let a = 1, b = 2;.const x = 5;const s = 'foo';any: Never use any. Use unknown if the type is truly unknown.{}: Use Record<string, unknown> or object instead of the empty object type {}.[] and {} instead of new Array() or new Object().T[] for simple types and Array<T> for complex or nested types.for...of to iterate over arrays. Avoid forEach unless necessary for side effects in a chain.private, protected). Avoid ES private fields (#field).public explicitly (it is the default).class UserService {
constructor(private readonly db: Database) {}
}
readonly if they are not reassigned.interface: Use interface for object structures (classes/objects).type: Use type for unions, intersections, or primitive aliases.
interface User { id: string; }
type ID = string | number;
enum. Do not use const enum.function Foo() {} for named functions at the top level.{} for all control structures (if, else, for, while), even for single-line statements.=== and !==).obj == null (loose equality) specifically to check for both null and undefined.| Entity | Style | Example |
|---|---|---|
| Classes / Interfaces | UpperCamelCase | UserProfile |
| Types / Enums | UpperCamelCase | UserStatus |
| Decorators | UpperCamelCase | @Component |
| Variables / Params | lowerCamelCase | timeoutMs |
| Functions / Methods | lowerCamelCase | fetchData() |
| Constants | CONSTANT_CASE | MAX_RETRY |
_ or $ prefixes for private members or variables.' for ordinary string literals.` for complex concatenation or multi-line strings.import/export. Do not use require() or namespace.export default. Use named exports for better discoverability and refactoring.
export class Bar {} // Correct
export default Bar; // Incorrect
/** ... */ for documentation of classes, methods, and properties.// for internal notes.start(/* shouldHeat= */ true).strict: true in tsconfig.json.@ts-ignore. Use @ts-expect-error sparingly (primarily in tests) with a comment explaining why.any or loose assertions.