Naming Conventions
In this page:
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
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
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
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
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
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: