← Back to TypeScript Course | Chapter 23: Performance and Best Practices | Lesson 6 of 7

Naming Conventions

Consistent names make TypeScript code easier to read and maintain. Good naming usually communicates the purpose of variables, types, functions, classes, and generic parameters without requiring extra comments.

Variables and Functions

Variables and functions should read as nouns and verbs respectively in camelCase, so userCount and calculateTotal communicate their role without needing a comment.

Example: Variables and Functions

typescript
const userCount: number = 5;
function calculateTotal(price: number, qty: number): number {
  return price * qty;
}
console.log(userCount, calculateTotal(10, 2));

Types and Interfaces

Types and interfaces are conventionally PascalCase, which visually distinguishes a type reference like User from a value reference like user at the exact same spot in code.

Example: Types and Interfaces

typescript
interface User {
  name: string;
}
const user: User = { name: "Ravi" };
console.log(user);

Generic Type Parameters

Generic type parameters traditionally start with T (and U, K, V for additional ones), but naming a parameter TItem or TResponse when its role isn't obvious saves readers from tracing usage to figure out what it represents.

Example: Generic Type Parameters

typescript
function wrapResponse<TResponse>(data: TResponse): { data: TResponse } {
  return { data };
}
console.log(wrapResponse({ id: 1 }));

Boolean Names

Boolean variables and functions read best prefixed with is, has, or can (like isLoading or hasPermission), since that phrasing makes the true/false meaning obvious at the call site.

Example: Boolean Names

typescript
const isLoading: boolean = false;
const hasPermission: boolean = true;
function canEdit(user: { role: string }): boolean {
  return user.role === "admin";
}
console.log(isLoading, hasPermission, canEdit({ role: "admin" }));

Names That Explain Intent

A name should describe intent, not implementation — activeUsers tells a reader what the value represents, while a name like arr2 forces them to go read the surrounding code to find out.

Example: Names That Explain Intent

typescript
const activeUsers: string[] = ["Ravi", "Priya"];
// Avoid vague names like arr2 or data1
console.log(activeUsers);
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.