Frontend Development 5 min read

9 TypeScript Bad Habits to Break for Cleaner, Safer Code

This article lists nine common TypeScript pitfalls—such as disabling strict mode, misusing the || operator, overusing any, and vague generic names—and explains the correct practices and reasons to adopt them, helping developers write more maintainable and type‑safe code.

Code Mala Tang
Code Mala Tang
Code Mala Tang
9 TypeScript Bad Habits to Break for Cleaner, Safer Code

To improve your TypeScript skills and avoid common bad habits, here are nine habits to break, helping you write more efficient and standardized code.

1. Not Using Strict Mode

Incorrect practice: Not enabling strict mode in tsconfig.json . Correct practice: Enable strict mode. Reason: Stricter rules aid future code maintenance and pay off in reduced debugging time.

2. Using || for Default Values

Incorrect practice: Using || to handle optional values. Correct practice: Use the ?? operator or define default values at the parameter level. Reason: ?? only falls back for null or undefined , providing more precise behavior.

3. Using any as a Type

Incorrect practice: Using any for uncertain data structures. Correct practice: Use unknown instead. Reason: any disables type checking, making errors harder to catch.

4. Using val as SomeType

Incorrect practice: Forcing the compiler to infer a type. Correct practice: Use type guards. Reason: Type guards ensure explicit checks, reducing potential errors.

5. Using as any in Tests

Incorrect practice: Creating incomplete test doubles with as any . Correct practice: Move mock logic to reusable locations. Reason: Avoid repeated property changes across tests and keep code tidy.

6. Optional Properties

Incorrect practice: Defining properties as optional. Correct practice: Explicitly express property combinations. Reason: More precise types catch errors at compile time.

7. Single‑Letter Generics

Incorrect practice: Using single‑letter generic names. Correct practice: Use descriptive type names. Reason: Descriptive names improve readability and understanding.

8. Non‑Boolean Checks

Incorrect practice: Passing a value directly to an if statement. Correct practice: Explicitly check the condition. Reason: Makes code logic clearer and avoids misunderstandings.

9. Double‑Bang ( !! ) Operator

Incorrect practice: Using !! to coerce non‑boolean values to boolean. Correct practice: Explicitly check the condition. Reason: Improves code readability and avoids confusion.

TypeScriptfrontend developmentprogrammingcode qualityBad Practices
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.