Clean TypeScript Guidelines
Follow these rules when writing or reviewing TypeScript code.
1. Type Safety
- No type casts or assertions: Never use
as Type,<Type>value, or the non-null assertion operator (!). These bypass the compiler and defeat static typing. - No
any: Useunknownfor values whose shape is not yet verified, then narrow them before use. - Use type guards and narrowing: Write user-defined type guards (
value is Type), or useinchecks,typeof,instanceof, and discriminated unions so the compiler proves types naturally. - Validate at boundaries: Parse external inputs (JSON responses, environment variables, form data) with runtime checks or schema validators at the system boundary.
- Prefer string union types over
enum: Usetype Status = "open" | "closed"instead ofenumfor simpler runtime behavior and cleaner narrowing. - Exceptions: Only use a type assertion when interfacing with a broken third-party type definition where no runtime check is possible. Isolate the assertion inside a single small helper function and add a comment explaining why it cannot be avoided.
// Avoid
function getUserName(data: unknown): string {
return (data as { name: string }).name;
}
// Prefer
function isUserRecord(value: unknown): value is { name: string } {
return typeof value === "object" && value !== null && "name" in value && typeof value.name === "string";
}
function getUserName(data: unknown): string {
if (!isUserRecord(data)) {
throw new Error("Invalid user record");
}
return data.name;
}
2. Functions
- Use
functiondeclarations: Always use thefunctionkeyword for named functions instead of assigning arrow functions toconstvariables. - Avoid inline functions: Do not pass multi-line or complex inline callbacks to methods like
.map(),.filter(),.reduce(), or event listeners. Extract them into namedfunctiondeclarations. - When inline functions are acceptable: Short, single-expression callbacks that depend directly on local closure variables (such as
items.filter((item) => item.groupId === currentGroupId)) are acceptable when extracting them would obscure intent. Otherwise, pass a named function reference (such asitems.filter(isActive)).
// Avoid
const getActiveUserEmails = (users: User[]): string[] => {
return users
.filter((user) => {
return user.isActive && !user.isSuspended;
})
.map((user) => user.email);
};
// Prefer
function isEligibleUser(user: User): boolean {
return user.isActive && !user.isSuspended;
}
function getUserEmail(user: User): string {
return user.email;
}
function getActiveUserEmails(users: User[]): string[] {
return users.filter(isEligibleUser).map(getUserEmail);
}
3. File and Module Structure
- Balanced granularity: Group related types, constants, and functions into cohesive modules organized by feature or domain.
- Avoid extremes:
- Do not put the entire application into one giant file.
- Do not create a separate file for every tiny function or single-property type.
- Practical boundaries:
- Split a file when it handles more than one distinct responsibility or grows past roughly 200–300 lines.
- Merge files that contain only 10–20 lines and are always imported together by the same caller.
- Explicit exports: Export only the public API of each module. Keep internal helper functions unexported at the bottom of the file.
4. Readability and Naming
- Keep code readable: Favor clear, step-by-step logic over clever one-liners, nested ternaries, or dense method chains. Assign intermediate steps to named variables.
- Use plain words: Name variables, functions, types, and files with simple, everyday words that describe what they hold or do.
- Avoid unnecessary adjectives: Do not use vague, promotional, or filler adjectives in names, comments, or documentation (avoid words like
smart,fast,simple,advanced,clean,helper, ormanager). Only use an adjective when it states a concrete domain fact (such asactiveUsersoremptyCart).