<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Really Good</title>
    <description>The latest articles on DEV Community by Really Good (@reallygood).</description>
    <link>https://dev.to/reallygood</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F9561%2F56840706-3960-4a76-9bd3-1ed46f9141dc.jpg</url>
      <title>DEV Community: Really Good</title>
      <link>https://dev.to/reallygood</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/reallygood"/>
    <language>en</language>
    <item>
      <title>Friendly TypeScript Errors: Custom Messages for Easier Debugging</title>
      <dc:creator>nunibaranes</dc:creator>
      <pubDate>Thu, 19 Sep 2024 11:53:22 +0000</pubDate>
      <link>https://dev.to/reallygood/making-typescript-errors-friendly-custom-messages-for-easier-debugging-5672</link>
      <guid>https://dev.to/reallygood/making-typescript-errors-friendly-custom-messages-for-easier-debugging-5672</guid>
      <description>&lt;p&gt;Ever struggled with cryptic &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt; errors that slow down your debugging process? In large projects, unclear error messages can turn simple bugs into major roadblocks.&lt;/p&gt;

&lt;p&gt;Take, for example, a common TypeScript error when props don’t match the expected types. Here’s how TypeScript usually presents these errors:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhvyc9sqtpggg55j11w7u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhvyc9sqtpggg55j11w7u.png" alt="Screenshot of the default TypeScript error message showing incompatible prop types for the MyComponent component. 'is not assignable to type 'IntrinsicAttributes &amp;amp; Props' It highlights that 'a' is a string and 'b' is a number, causing a type mismatch." width="" height=""&gt;&lt;/a&gt;&lt;br&gt;
While the information is there, it's not immediately easy to understand. 🤔&lt;/p&gt;

&lt;p&gt;Wouldn’t it be much easier to get a clearer, more specific message, like this? 🤩&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fumo5ahj86xkwvp8ocjk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fumo5ahj86xkwvp8ocjk6.png" alt="Screenshot of a TypeScript error message showing custom error handling in MyComponent. The error highlights that 'a' and 'b' must be the same type." width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it’s clear that the props must to get the same type and why 🥳&lt;/p&gt;

&lt;p&gt;In large projects, customizing TypeScript errors makes debugging faster and more intuitive.&lt;/p&gt;

&lt;p&gt;In this article, I’ll show you how to customize TypeScript’s error messages to make them clearer and more helpful for developers. We’ll use &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types" rel="noopener noreferrer"&gt;type guards&lt;/a&gt; and &lt;a href="https://www.typescriptlang.org/docs/handbook/2/generics.html" rel="noopener noreferrer"&gt;generic types&lt;/a&gt; to create developer-friendly errors that make your code safer and easier to maintain.&lt;/p&gt;

&lt;p&gt;Let’s dive into making your error messages both functional and easy to understand.&lt;/p&gt;
&lt;h2&gt;
  
  
  Making Error Messages More Developer-Friendly
&lt;/h2&gt;

&lt;p&gt;We’ll use generic types and type guards to create custom TypeScript errors in our code:&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Define Expected and Invalid Types
&lt;/h3&gt;

&lt;p&gt;First, define the expected and invalid prop types to trigger TypeScript errors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Define the base props type where both `a` and `b` can be either a number or a string.&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// Define the invalid props type when either `a` or `b` is not provided.&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;InvalidRequireProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// Define the invalid props type when `a` and `b` have different types.&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;InvalidTypeProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Enforce Strict Type Compatibility
&lt;/h3&gt;

&lt;p&gt;Next, we’ll create a generic React component that enforces strict type compatibility between the two props using generic type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Ensure a and b are provided and have the same type.&lt;/span&gt;
&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;TypeGuardValidateProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
 &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;InvalidRequireProps&lt;/span&gt;
   &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🚨 MyComponent's Props `a` and `b` are required 🤦🏻‍♀️ - without them, this component is as useful as a bicycle with no wheels! 😝&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;InvalidTypeProps&lt;/span&gt;
 &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🚨 MyComponent's Props `a` and `b` must to get the same type 🙏🏼 - either both numbers for calculation or both strings for concatenation. Mixing them? That's like trying to ride a bicycle with square wheels! 😜&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
 &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Define a generic React component that applies type validation.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TypeGuardValidateProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you try to render the component without the required props, TypeScript will provide a clear error message&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr38qc4fxj1rvjac4nze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr38qc4fxj1rvjac4nze.png" alt="TypeScript error showing that a and b props are required, with a custom error message humorously comparing the component to a bicycle with no wheels if the props are missing." width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, if you pass a and b with different types, TypeScript will generate the following error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5me5skggddpltmu0agxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5me5skggddpltmu0agxa.png" alt="TypeScript error showing a type mismatch between props a and b, with a custom error message stating that both props must be the same type - either both numbers for calculation or both strings for concatenation. The message humorously compares mixing types to riding a bicycle with square wheels." width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  👏👏👏 Conclusion
&lt;/h2&gt;

&lt;p&gt;We explored custom TypeScript error messages. You can extend this approach to handle more cases, making your code clearer, readable, and safer.&lt;/p&gt;

&lt;p&gt;In a recent project, design limitations required type guards to block certain options, but it wasn’t clear why. This led me to create custom TypeScript error messages for clearer feedback.&lt;/p&gt;

&lt;p&gt;These custom error messages not only help individual developers debug faster but can also be invaluable for larger teams where clear communication through errors prevents misunderstandings.&lt;/p&gt;

&lt;p&gt;Here’s my CodeSandbox with examples, showcasing custom error messages and a forwardRef workaround to address type loss in forwarded ref components.&lt;/p&gt;

&lt;p&gt;Mastering these techniques will make your React components more robust and developer-friendly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try implementing these techniques in your own projects and let me know how it improves your debugging process! 💪 Good luck!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
    </item>
  </channel>
</rss>
