TypeScript Code Smells
Common code smells in TypeScript and how to fix them.
Using any
1// ❌ Bad
2function process(data: any) {
3 return data.value;
4}
5
6// ✅ Good
7interface Data {
8 value: string;
9}
10function process(data: Data) {
11 return data.value;
12}
Not Using Optional Chaining
1// ❌ Bad
2const city = user && user.address && user.address.city;
3
4// ✅ Good
5const city = user?.address?.city;
Not Using Nullish Coalescing
1// ❌ Bad
2const value = input || 'default'; // 0, false, '' are replaced
3
4// ✅ Good
5const value = input ?? 'default'; // Only null/undefined replaced
Mutation of Function Parameters
1// ❌ Bad
2function addItem(arr: Item[], item: Item) {
3 arr.push(item);
4 return arr;
5}
6
7// ✅ Good
8function addItem(arr: readonly Item[], item: Item): Item[] {
9 return [...arr, item];
10}
Not Using Union Types
1// ❌ Bad
2function handleResponse(success: boolean, data?: any, error?: any) {
3 if (success) {
4 return data;
5 }
6 throw error;
7}
8
9// ✅ Good
10type Success = { success: true; data: Data };
11type Failure = { success: false; error: Error };
12type Result = Success | Failure;
13
14function handleResponse(result: Result) {
15 if (result.success) {
16 return result.data;
17 }
18 throw result.error;
19}
Related Snippets
- C/C++ Code Smells
Common code smells in C/C++ and how to fix them. Memory Leaks 1// ❌ Bad 2void … - C/C++ Secure Coding
Secure coding practices for C/C++ applications. Buffer Overflow Prevention 1// ❌ … - C/C++ Vulnerability Checks
Tools for checking vulnerabilities in C/C++ code. Valgrind 1# Install 2sudo apt … - Common Antipatterns
Common software antipatterns to avoid across all languages and architectures. … - Common Code Smells
Common code smells to watch for during code reviews with examples and fixes. … - Developer Pre-Submission Checklist
Comprehensive checklist for developers before submitting a pull request. Code … - Go Code Smells
Common code smells in Go and how to fix them. Ignoring Errors 1// ❌ Bad 2result, … - Go Secure Coding
Secure coding practices for Go applications. SQL Injection Prevention 1// ❌ … - Go Vulnerability Checks
Tools for checking vulnerabilities in Go code. Govulncheck 1# Install 2go … - Haskell Code Smells
Common code smells in Haskell and how to fix them. Partial Functions 1-- ❌ Bad: … - Haskell Secure Coding
Secure coding practices for Haskell applications. SQL Injection Prevention 1-- ❌ … - Haskell Vulnerability Checks
Tools for checking vulnerabilities in Haskell code. Cabal Outdated 1# Check … - Python Code Smells
Common code smells in Python and how to fix them. Mutable Default Arguments 1# ❌ … - Python Secure Coding
Secure coding practices for Python applications. SQL Injection Prevention 1# ❌ … - Python Vulnerability Checks
Tools for checking vulnerabilities in Python code. Safety - Dependency Scanner … - Reviewer Checklist
Comprehensive checklist for code reviewers to ensure thorough and constructive … - Rust Code Smells
Common code smells in Rust and how to fix them. Unwrap/Expect Abuse 1// ❌ Bad … - Rust Secure Coding
Secure coding practices for Rust applications. SQL Injection Prevention 1// ❌ … - Rust Vulnerability Checks
Tools for checking vulnerabilities in Rust code. Cargo Audit 1# Install 2cargo … - TypeScript Secure Coding
Secure coding practices for TypeScript applications. XSS Prevention 1// ❌ … - TypeScript Vulnerability Checks
Tools for checking vulnerabilities in TypeScript/JavaScript code. npm audit 1# …