<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://munzahshah.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 25 Sep 2026 12:52:26 GMT</lastBuildDate><atom:link href="https://munzahshah.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Common TypeScript Pitfalls Beginners Should Watch Out For]]></title><description><![CDATA[I've recently started diving deep into Typescript and it opened my eyes to some of the blunders I've been making in the past while using it. If you're new to Typescript, this article can be a lifesaver, as it highlights some typical mistakes I made w...]]></description><link>https://munzahshah.hashnode.dev/common-typescript-pitfalls-beginners-should-watch-out-for</link><guid isPermaLink="true">https://munzahshah.hashnode.dev/common-typescript-pitfalls-beginners-should-watch-out-for</guid><category><![CDATA[TypeScript]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Munzah Shah]]></dc:creator><pubDate>Mon, 04 Mar 2024 05:30:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1709486960043/e505ce1f-e37c-4068-a895-2933a83c1e00.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I've recently started diving deep into Typescript and it opened my eyes to some of the blunders I've been making in the past while using it. If you're new to Typescript, this article can be a lifesaver, as it highlights some typical mistakes I made when I first dipped my toes into Typescript coding.</p>
<ol>
<li><p><strong>Using the "any" type excessively and Not avoiding implicit "any" scenarios:</strong></p>
<p> When we do not know what to use, we use the <code>any</code> type, which is an extremely common mistake. When we do that, we are explicitly asking the typescript compiler to turn off the type checking, which removes the fundamental reason for using TS in any project.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709496876342/86cb1b31-1ccb-454a-a541-129402e7825a.png" alt class="image--center mx-auto" /></p>
<p> Or when we do not explicitly type our variables properly, Typescript implicitly gives them the <code>any</code> type.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709492086218/2c784793-0df0-41ca-a20e-12c24f0f16ee.png" alt class="image--center mx-auto" /></p>
<p> In both cases, we are eliminating the primary advantage of using TypeScript. An alternate solution is to use the <code>unknown</code> type. It is a type-safe alternative of <code>any</code> where we are telling the typescript compiler that <em>I don't know the type of the variable currently but I will know when I try to use this variable anywhere in my code</em>. In simpler words, we are not asking the Typescript compiler to silence itself, rather the compiler doesn't allow operations to be performed on a <code>unknown</code> type unless we narrow it down to a more specific type using some conditions. This allows us to safely type our variables even when we do not know their types in the present moment.</p>
</li>
<li><p><strong>Not using the strictNullChecks compiler option:</strong></p>
<p> There are times when we unconsciously try to access variables that might be <code>null</code> or <code>undefined</code>. Enabling the <code>strictNullChecks</code> compiler saves us from falling into this pitfall. Enabling this ensures that variables are not given null or undefined unless explicitly told (using a union type).</p>
<p> Here's an example that shows <code>myCircleArea</code> is used without it being defined and TS compiles this without any errors.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709493735067/563d3fad-7eef-44ef-a9e6-95a867048177.png" alt class="image--center mx-auto" /></p>
<p> However, if I enable the <code>strictNullChecks</code> or simply the <code>strict</code> flag in compiler options under my <code>tsconfig.json</code> file, the same code will throw me a compile error:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709493978746/1d22eb36-1689-49a1-9c61-d16600006233.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Overcomplicating the types instead of using discriminating unions:</strong></p>
<p> To understand this, let's consider the following example, where <code>UserState</code> is trying to represent three different states that the data can be in <code>Loading</code>, <code>Success</code>, or <code>Error</code>.</p>
<p> a) If we are in the <code>Loading</code> state, there can be no <code>error</code> messages as well as no user <code>information</code>.</p>
<p> b) If we are in the <code>Success</code> state, there can be no error messages.</p>
<p> c) If we are in the <code>Error</code> state, there can be no user information.</p>
<p> Currently, the following code suggests that we can be in the <code>Loading</code> state, but we can also have the user information and errors at the same time. Or we could be in the <code>Success</code> state but we can not have the user information, etc. Because of this, the TS compiler yells at me saying <code>'user.error' is possibly 'undefined'</code> or <code>'user.information' is possibly 'undefined'</code> because we have made those properties "optional" on the UserState.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709494723655/20b448de-6f3b-47bd-894b-691e92c9d0da.png" alt class="image--center mx-auto" /></p>
<p> To solve this problem, a better practice is to switch to discriminating unions by extracting one type into multiple types by recognizing the states your variable can be in. In our case, if we have three different states that it can be in, it's a good practice to create three different individual types for each one of them. Here's how we can do it:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709496163312/da572a8b-91a0-4893-9f06-a785ea6c6f45.png" alt class="image--center mx-auto" /></p>
<p> Each type <code>Loading</code>, <code>Success</code>, and <code>Error</code> contains only the properties they require without making them optional on the object, and each type contains a common key called <code>state</code> which contains different values that the <strong>user</strong> can be in. When we have one key (in our case <code>state</code>) that contains different values between multiple types (in our case <code>Loading</code>, <code>Success</code>, and <code>Error</code>), and you union them together (in our case <code>UserState</code>), then this is considered as a discriminating union in Typescript. Discriminating unions not only allows us to ensure type safety but also enhances auto-completion (in code editors) and code readability.</p>
</li>
<li><p><strong>Not knowing how to type an object properly:</strong></p>
<p> Another common mistake is not knowing how to type an Object properly. When you know the expected shape of an object, and instead of making it strictly typed, you type it as an <code>Object</code>, then the problem begins:<br /> Here is an example:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709489869242/4d80ca4d-ee1b-40cd-9421-b31c15f79662.png" alt class="image--center mx-auto" /></p>
<p> I was able to pass the <code>myDate</code> and <code>myFunction</code> as arguments to the <code>returnObject</code> function even though I typed <code>myObj</code> as an "Object". This is because dates and functions are treated as objects in JavaScript so TS didn't throw errors and allowed me to pass any object I want. To avoid this, one should use the Record type or an indexed type.</p>
<p> Here's a demonstration:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709491001796/c923d46c-3a9d-4321-ba0f-ef53e6b82ec3.png" alt class="image--center mx-auto" /></p>
<p> Now I'm telling it to only allow an Object that has a key with <code>string</code> type with an <code>unknown</code> value. So TypeScript recognizes the type and starts yelling at me for passing the date and function arguments.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709491052931/5cd6f492-92f5-4ee1-9d11-4e8c6dc2ac91.png" alt class="image--center mx-auto" /></p>
<p> Another way to achieve the same thing is to use <code>{[index: string]: unknown}</code> which is also telling the same thing to the compiler i.e., any object having a key with a type <code>string</code> and an <code>unknown</code> value will work perfectly fine for <code>myObj</code>.</p>
</li>
</ol>
<p>Hopefully reading this will equip you with the knowledge to steer clear of those common Typescript pitfalls I mentioned in this article. However, this isn't it, Typescript is a big world, and there's more to uncover! Might publish a Part 2 of the same topic if I manage to resist the urge to procrastinate :P Until then, happy coding!</p>
]]></content:encoded></item></channel></rss>