🚀 Why You Should Use @ts-expect-error Over @ts-ignore in TypeScript 🚀

Mohamed ZhiouaMohamed Zhioua
1 min read

As TypeScript developers, we often run into scenarios where the type definitions don't align with the actual data we receive. For example, you might receive a JSON object with a country field, but your type definition doesn't include it. This results in a TypeScript error when trying to access country, preventing the code from compiling.

A quick fix is to use @ts-ignore, but there's a better approach: using @ts-expect-error.

🔍 Why @ts-expect-error is superior:

  • @ts-expect-error suppresses the error like @ts-ignore, but with an important difference: once you fix the type issue, @ts-expect-error will throw its own error, reminding you to remove it.

  • This ensures that no unnecessary comments are left behind, leading to cleaner, more maintainable code.

💡 Best Practice: Always prefer @ts-expect-error when temporarily disabling errors. It helps keep your code clean, ensures type safety, and avoids hidden problems in the future.

#TypeScript #CleanCode #WebDevelopment #DevelopmentBestPractices #TypeSafety #JavaScript #SoftwareEngineering

0
Subscribe to my newsletter

Read articles from Mohamed Zhioua directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamed Zhioua
Mohamed Zhioua

Hеllo, I'm Mohamеd Zhioua, a dеdicatеd Full-Stack JavaScript Dеvеlopеr basеd in Tunis, Tunisia 📍. I'm on a mission to shapе thе futurе through codе.