Introduction: why code review is needed in TypeScript
Code review is the process of checking written code by other developers. For beginner TypeScript developers, this is not just a formality, but a powerful learning tool. Proper review helps identify errors early, improve architecture, and maintain a consistent style within the team.
In this article, we will break down what specifically to pay attention to during code review in TypeScript, how to give feedback so that it is useful, and provide code examples. The material is aimed at beginners who are just starting to work with TypeScript and want to improve their review skills.
1. Typing: the foundation of TypeScript
The first thing to check in TypeScript is the correctness of types. TypeScript adds static typing, which reduces the number of bugs, but only if types are used correctly.
What to check:
- Explicit types for parameters and return values. Avoid
anywherever possible. - Using interfaces and types instead of primitives for complex structures.
- Correct use of Union and Intersection types.
- Absence of unnecessary type assertions.
// Bad: implicit type and anyfunction processData(data: any) { return data.name;}
// Good: explicit interfaceinterface User { name: string; age: number;}
function processData(data: User): string { return data.name;}During review, pay attention to places where the any type could be replaced with a more specific one. This immediately improves code readability and safety.
2. Checking for null and undefined
TypeScript helps avoid errors with null and undefined through strict null checks. But developers often forget to handle optional values.
What to check:
- Using
?for optional properties and parameters. - Checking for
nullbefore accessing properties. - Using the
??and?.operators.
// Bad: no protection against undefinedfunction getLength(str: string | undefined): number { return str.length; // Error: str could be undefined}
// Good: using optional chaining and nullish coalescingfunction getLength(str: string | undefined): number { return str?.length ?? 0;}In code review, check that all potentially empty values are handled. This is a common cause of bugs in production.
3. Code cleanliness and readability
TypeScript is a superset of JavaScript, so the principles of clean code remain relevant. A good code review includes checking style and structure.
What to check:
- Naming of variables and functions. Names should be meaningful.
- Function length. If a function does more than one thing, break it up.
- Comments. They should explain "why", not "what".
- Absence of magic numbers. Extract constants into separate variables.
// Bad: magic numbers and unclear namefunction calc(a: number) { return a * 1.2;}
// Good: constant and clear nameconst TAX_RATE = 0.2;
function calculateTotalWithTax(amount: number): number { return amount * (1 + TAX_RATE);}During review, ask the question: "Could a new developer understand this code without explanation?" If not, ask for improvements.
4. How to give feedback: gently and constructively
Code review is not an exam, but a dialogue. For beginners, it is especially important to receive feedback without personal criticism. Here are a few rules that will help give useful comments.