<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anmol Dalmia on Medium]]></title>
        <description><![CDATA[Stories by Anmol Dalmia on Medium]]></description>
        <link>https://medium.com/@dalmia.anmol?source=rss-8b16b8fd08d7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EbZewCX4TCgC81UO.jpg</url>
            <title>Stories by Anmol Dalmia on Medium</title>
            <link>https://medium.com/@dalmia.anmol?source=rss-8b16b8fd08d7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 20:24:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dalmia.anmol/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Nuance of “optional” in TypeScript]]></title>
            <link>https://medium.com/@dalmia.anmol/a-nuance-of-optional-in-typescript-968f6828bbf9?source=rss-8b16b8fd08d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/968f6828bbf9</guid>
            <category><![CDATA[function]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[arguments]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Anmol Dalmia]]></dc:creator>
            <pubDate>Fri, 13 Sep 2019 15:45:59 GMT</pubDate>
            <atom:updated>2019-09-13T15:45:59.244Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7meXcMo6YgYyHY1s" /><figcaption>Photo by <a href="https://unsplash.com/@fairfilter?utm_source=medium&amp;utm_medium=referral">Oliver Roos</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In a funny world of programming languages, it is common wisdom that JavaScript is a rising language. Voted the <a href="https://insights.stackoverflow.com/survey/2019#most-popular-technologies">most popular</a> technology today, one might say that <em>JavaScript is the new Java </em>perhaps<em>. </em>But not entirely so. One thing that characteristically demarcates JS from Java is <strong>dynamic typing</strong>.</p><h4>What are Types?</h4><p>In any computing system, not all variables in the system are the same. Some variables have different properties than others, by design. These differentiated properties create <strong>types </strong>of variables in a system. Same is true for programming languages where we may associate a type of any variable.</p><p>What do we mean by dynamic typing then? Well, a language is said to be <strong><em>dynamically typed</em></strong><em> </em>if the type of a variable is allowed to be variable itself during compilation. In other words, in a dynamically typed language, it is not possible to deterministically say the type of a variable without running the program itself. This is because during runtime, the variable can assume values of any type. <strong>JavaScript</strong> is such a language.</p><p>The opposite of that is <strong><em>static typing</em></strong> which dictates that during compilation, it is deterministically (aka statically) decided that a variable has a certain type. <strong>Java</strong> is such a language.</p><h4>Where’s a bridge?</h4><p><strong>Enter </strong><a href="https://www.typescriptlang.org/"><strong>TypeScript</strong></a><strong>.</strong> Informally, it is a language that creates an illusion of static typing in JavaScript. When you write code in TS, you can associate a type (or a limited set of possible types) to a variable and it is expected to be faithful to that type(s) throughout its life. During the process of compilation of TS to JS, the compiler performs static type check in your code based on these associated types to see if your variables are following this rule, which is equivalent to the static typing of Java. However, once the code is compiled to JS and is out there in the wild, the types associations are lost and these types lose their meanings.</p><p>But all this is public knowledge. What this story discusses is a very tiny part of TS — optional arguments.</p><h4>Optionality in TypeScript</h4><p>Optional arguments is a well known concept in TS where you could say that an argument is not required for a given function to execute some logic normally, but it would be taken for computation if you provided one. Something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1201a35d2ae5bb40b88b100a7298b752/href">https://medium.com/media/1201a35d2ae5bb40b88b100a7298b752/href</a></iframe><p>You can read the official docs about them <a href="https://www.typescriptlang.org/docs/handbook/functions.html#optional-and-default-parameters">here</a>.</p><p>In short, the <strong>?</strong> over there means that the parameter can be <em>undefined </em>or may not be passed in a call, which is equivalent to the following typing in TS:</p><pre>bar: string | undefined;</pre><p>Wow, this looks really simple!</p><h4>So what’s the catch?</h4><p>Does it mean whenever <strong>bar</strong><em> </em>can have <em>undefined</em> as a value, I can specify a <strong>?</strong> for it in the function prototype? Well, no, not really.</p><p>Take a look at this snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84c4021131bf36919d576507d8fac7c5/href">https://medium.com/media/84c4021131bf36919d576507d8fac7c5/href</a></iframe><p>Felt anything unusual? For starters, this function seems to throw an error for an <em>optional </em>argument. This contradicts the meaning of an argument being optional since the variable’s absence is actually causing the function to fail. Therefore, in this case, you cannot really say that optionality is the same as being able to assume <em>undefined </em>value.</p><p>However, one may ask a very intuitive question about this example. <strong>Why would you allow a variable to be <em>undefined </em>if it is not meant to be? </strong>Should it not be the responsibility of the caller to ensure that <strong>bar </strong>always has the type <em>string</em>?</p><p>This is because <strong>it is not possible to guarantee that the caller will always send in the right type</strong>. An example — JS and TS are versatile languages and a single project can contain libraries that are written in either of them. If a JS module had a caller that called this function in TS, there is no way to guarantee that <strong>bar </strong>is going to be a <em>string</em>. This is because JS itself is typeless. Another example is when this function is an entry point to an open interface where an agent is making an API call. It is very much possible that the agent is not honouring the function’s contract.</p><h4>The “right” way</h4><p>The program below shows the “right” code for this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adfc67dc1e0c4bfaf30c0ed749ff445b/href">https://medium.com/media/adfc67dc1e0c4bfaf30c0ed749ff445b/href</a></iframe><p>Wait. Did I just not expand the ‘<strong>?’ </strong>there? How does it make things right? Well, syntactically this is true; the <strong>? </strong>is just an unfolding of the “ | undefined” type clause. However, now, the function prototype explicitly states that <strong>bar </strong>can be <em>undefined</em> and that the function will use this value in its execution. It could throw an error over it, or log some metrics maybe. But for it, <em>undefined </em>becomes a legal type of the argument, and the prototype conveys the subtle intent to a caller that it may pass <em>undefined </em>to the function for whatever reasons.</p><h4>And in gist</h4><p>In summary, using the optionality and explicitly declaring the type of an argument as <em>undefined </em>are two different concepts. While they are identical in syntax and execution, they convey different semantic meanings and should be used accordingly. While this is not going to break your system, it definitely shows an intent of usage in your interfaces.</p><p>Hope you found this highly-specialized story informational! Please clap if you liked it. Also leave your comments on what you think about it. :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=968f6828bbf9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>