<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>javascript Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/javascript/</link>
	<description>Become a better .NET full stack web developer</description>
	<lastBuildDate>Sun, 26 Jan 2025 17:36:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>javascript Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/javascript/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>What is Mutation Testing and Why Should You Use It?</title>
		<link>https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/</link>
					<comments>https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Tue, 01 Oct 2024 18:22:58 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mutation testing]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[typescript]]></category>
		<category><![CDATA[unit tests]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4825</guid>

					<description><![CDATA[<p>Do you know what is a better testing metric than code coverage? Does 100% code coverage mean your code is well-tested? How to generate unit test cases for free, without using AI? The answer to all these questions is mutation testing. Interested? Read on 😉 Code Coverage &#8211; Our Old Friend You probably know what&#8230;</p>
<p>The post <a href="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/">What is Mutation Testing and Why Should You Use It?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Do you know what is a better testing metric than code coverage? Does 100% code coverage mean your code is well-tested? How to generate unit test cases for free, without using AI?</p>



<p>The answer to all these questions is <strong>mutation testing</strong>. Interested? Read on <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4825"></span>



<h2 class="wp-block-heading">Code Coverage &#8211; Our Old Friend</h2>



<p>You probably know what <a href="https://en.wikipedia.org/wiki/Code_coverage">code coverage</a> is. Traditionally, many teams tend to measure how good their tests&#8217; suite is by percentage line / statements / functions coverage. Some even require this metric to be 100%. Let&#8217;s start with this well-known approach first.</p>



<p>We are working with <code>TicketsPriceCalculator</code> class today. It contains some simple business logic:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist133062269" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-ticketspricecalculator-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="ticketsPriceCalculator.ts content, created by dsibinski on 04:08PM on October 01, 2024."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="ticketsPriceCalculator.ts">
        <tr>
          <td id="file-ticketspricecalculator-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-ticketspricecalculator-ts-LC1" class="blob-code blob-code-inner js-file-line">export class TicketsPriceCalculator {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-ticketspricecalculator-ts-LC2" class="blob-code blob-code-inner js-file-line">  calculateTotalTicketsPrice(tickets: Ticket[]): number {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-ticketspricecalculator-ts-LC3" class="blob-code blob-code-inner js-file-line">    if (tickets.length === 0) {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-ticketspricecalculator-ts-LC4" class="blob-code blob-code-inner js-file-line">      return 0;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-ticketspricecalculator-ts-LC5" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-ticketspricecalculator-ts-LC6" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-ticketspricecalculator-ts-LC7" class="blob-code blob-code-inner js-file-line">    let totalPrice = tickets.reduce((sum, ticket) =&gt; sum + ticket.price, 0);</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-ticketspricecalculator-ts-LC8" class="blob-code blob-code-inner js-file-line">    const businessClassTickets = tickets.filter(</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-ticketspricecalculator-ts-LC9" class="blob-code blob-code-inner js-file-line">      (ticket) =&gt; ticket.isBusinessClass</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-ticketspricecalculator-ts-LC10" class="blob-code blob-code-inner js-file-line">    ).length;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-ticketspricecalculator-ts-LC11" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-ticketspricecalculator-ts-LC12" class="blob-code blob-code-inner js-file-line">    let totalDiscountToApply = 0;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-ticketspricecalculator-ts-LC13" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-ticketspricecalculator-ts-LC14" class="blob-code blob-code-inner js-file-line">    // Apply discounts based on total price</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-ticketspricecalculator-ts-LC15" class="blob-code blob-code-inner js-file-line">    if (totalPrice &gt; 2000) {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-ticketspricecalculator-ts-LC16" class="blob-code blob-code-inner js-file-line">      totalDiscountToApply += 0.25;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-ticketspricecalculator-ts-LC17" class="blob-code blob-code-inner js-file-line">    } else if (totalPrice &gt; 1000 &amp;&amp; totalPrice &lt;= 2000) {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-ticketspricecalculator-ts-LC18" class="blob-code blob-code-inner js-file-line">      totalDiscountToApply += 0.2;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-ticketspricecalculator-ts-LC19" class="blob-code blob-code-inner js-file-line">    } else if (totalPrice &gt; 500 &amp;&amp; totalPrice &lt;= 1000) {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-ticketspricecalculator-ts-LC20" class="blob-code blob-code-inner js-file-line">      totalDiscountToApply += 0.15;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-ticketspricecalculator-ts-LC21" class="blob-code blob-code-inner js-file-line">    } else if (totalPrice &gt;= 200 &amp;&amp; totalPrice &lt;= 500) {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-ticketspricecalculator-ts-LC22" class="blob-code blob-code-inner js-file-line">      totalDiscountToApply += 0.1;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-ticketspricecalculator-ts-LC23" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-ticketspricecalculator-ts-LC24" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-ticketspricecalculator-ts-LC25" class="blob-code blob-code-inner js-file-line">    // Apply additional discount based on number of tickets</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-ticketspricecalculator-ts-LC26" class="blob-code blob-code-inner js-file-line">    if (tickets.length &gt;= 10) {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-ticketspricecalculator-ts-LC27" class="blob-code blob-code-inner js-file-line">      totalDiscountToApply += 0.05;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L28" class="blob-num js-line-number js-blob-rnum" data-line-number="28"></td>
          <td id="file-ticketspricecalculator-ts-LC28" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L29" class="blob-num js-line-number js-blob-rnum" data-line-number="29"></td>
          <td id="file-ticketspricecalculator-ts-LC29" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L30" class="blob-num js-line-number js-blob-rnum" data-line-number="30"></td>
          <td id="file-ticketspricecalculator-ts-LC30" class="blob-code blob-code-inner js-file-line">    // Apply business class discounts</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L31" class="blob-num js-line-number js-blob-rnum" data-line-number="31"></td>
          <td id="file-ticketspricecalculator-ts-LC31" class="blob-code blob-code-inner js-file-line">    if (businessClassTickets &gt;= 10) {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L32" class="blob-num js-line-number js-blob-rnum" data-line-number="32"></td>
          <td id="file-ticketspricecalculator-ts-LC32" class="blob-code blob-code-inner js-file-line">      totalDiscountToApply += 0.2;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L33" class="blob-num js-line-number js-blob-rnum" data-line-number="33"></td>
          <td id="file-ticketspricecalculator-ts-LC33" class="blob-code blob-code-inner js-file-line">    } else if (businessClassTickets &gt;= 5) {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L34" class="blob-num js-line-number js-blob-rnum" data-line-number="34"></td>
          <td id="file-ticketspricecalculator-ts-LC34" class="blob-code blob-code-inner js-file-line">      totalDiscountToApply += 0.1;</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L35" class="blob-num js-line-number js-blob-rnum" data-line-number="35"></td>
          <td id="file-ticketspricecalculator-ts-LC35" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L36" class="blob-num js-line-number js-blob-rnum" data-line-number="36"></td>
          <td id="file-ticketspricecalculator-ts-LC36" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L37" class="blob-num js-line-number js-blob-rnum" data-line-number="37"></td>
          <td id="file-ticketspricecalculator-ts-LC37" class="blob-code blob-code-inner js-file-line">    return totalPrice * (1 &#8211; totalDiscountToApply);</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L38" class="blob-num js-line-number js-blob-rnum" data-line-number="38"></td>
          <td id="file-ticketspricecalculator-ts-LC38" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator-ts-L39" class="blob-num js-line-number js-blob-rnum" data-line-number="39"></td>
          <td id="file-ticketspricecalculator-ts-LC39" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/28087f239e57c7dab06d3df2e7532e6d/raw/d388fec49bd951d520a8d0af93c07907c122ce27/ticketsPriceCalculator.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/28087f239e57c7dab06d3df2e7532e6d#file-ticketspricecalculator-ts" class="Link--inTextBlock">
          ticketsPriceCalculator.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>As you can see, the class has one function whith returns the total price to be paid for a set of tickets. On the way, it applies various discounts based on some business rules. Fair enough.</p>



<p>It&#8217;s a critical piece of business logic for the app we&#8217;re working on, but it has never had any tests. We were asked to write unit tests for this class. Our manager strictly requires the code coverage to be 100%. Let&#8217;s do it then. We will go line by line, analyze the business logic and add the tests. Below, you can see the results of running those tests (you can find the tests code <a href="https://gist.github.com/dsibinski/cfc8d36cf7fe45bb9114b950fa898766">here</a>):</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="839" height="427" data-attachment-id="4840" data-permalink="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/1-unit-tests-initial/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/1-unit-tests-initial.jpg?fit=839%2C427&amp;ssl=1" data-orig-size="839,427" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1-unit-tests-initial" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/1-unit-tests-initial.jpg?fit=839%2C427&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/1-unit-tests-initial.jpg?fit=839%2C427&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/1-unit-tests-initial.jpg?resize=839%2C427&#038;ssl=1" alt="Unit tests before using mutation testing. We have 8 unit tests, all passing. Our code coverage (line, functions, statements, branches) is 100%" class="wp-image-4840" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/1-unit-tests-initial.jpg?w=839&amp;ssl=1 839w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/1-unit-tests-initial.jpg?resize=768%2C391&amp;ssl=1 768w" sizes="(max-width: 839px) 100vw, 839px" /></figure>



<p></p>



<p>Wow, that&#8217;s awesome! We did a great job. We have <strong>8 unit tests which provide 100% statements, branch, functions and lines coverage</strong>!</p>



<p>So now we are really safe, aren&#8217;t we? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Yeah&#8230; until you know what mutation testing is <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">What Is Mutation Testing?</h2>



<p>The idea of mutation testing is to <em>mutate</em> (change) our code. Mutation testing tools introduce changes into our production code. These changes can be really anything &#8211; from reverting a condition of an <code>if</code> statement, to changing some operators or changing a <code>string</code> value. For instance, here are some logical mutations <a href="https://stryker-mutator.io/">Stryker Mutator</a> is able to introduce (you can find the list of all mutations supported by Stryker <a href="https://stryker-mutator.io/docs/mutation-testing-elements/supported-mutators/">here</a>):</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" decoding="async" width="445" height="320" data-attachment-id="4845" data-permalink="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/2-stryker-logical-mutations/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/2-stryker-logical-mutations.jpg?fit=445%2C320&amp;ssl=1" data-orig-size="445,320" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2-stryker-logical-mutations" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/2-stryker-logical-mutations.jpg?fit=445%2C320&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/2-stryker-logical-mutations.jpg?fit=445%2C320&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/2-stryker-logical-mutations.jpg?resize=445%2C320&#038;ssl=1" alt="Logical operator mutations supported by Stryker Mutator" class="wp-image-4845"/></figure>
</div>


<p></p>



<p>In effect, they generate modified versions of our code, which are called <em>mutants</em>. Makes sense <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Each mutant contains a single modification in our source code. What happens next is crucial. Mutation testing tool takes this mutant (it is used as the code under tests now) and executes all of our unit tests against it. The result may be twofold:</p>



<ul class="wp-block-list">
<li>all tests have passed. In other words, the <em>mutant has</em> <em>survived</em>. <strong>This is a bad situation</strong>. It implies that the modification of our source code was not detected by our tests</li>



<li>at least one test has failed. In other words, the <em>mutant has been killed</em>. <strong>This is a good scenario</strong>. It means that our tests detect changes in the production code and efficiently cover this particular scenario.</li>
</ul>



<p>The flow is more or less like this <a href="https://peterevans.dev/posts/mutation-testing/">schema from Peter Evans&#8217; blog</a> presents:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img data-recalc-dims="1" decoding="async" width="353" height="580" data-attachment-id="4847" data-permalink="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/3-mutation-test-schema/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/3-mutation-test-schema.png?fit=353%2C580&amp;ssl=1" data-orig-size="353,580" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3-mutation-test-schema" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/3-mutation-test-schema.png?fit=353%2C580&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/3-mutation-test-schema.png?fit=353%2C580&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/3-mutation-test-schema.png?resize=353%2C580&#038;ssl=1" alt="Schema of mutation testing" class="wp-image-4847" style="width:353px;height:auto"/></figure>
</div>


<p></p>



<p>But why would anyone change the production code on purpose by introducing some stupid changes? It may seem counterintuitive for now, but let&#8217;s see how we can use it to our leverage.</p>



<h2 class="wp-block-heading">Mutation Testing with Stryker Mutator</h2>



<p>Let&#8217;s clearly state what we are starting with:</p>



<p><strong>8 unit tests, 100% code coverage</strong></p>



<p>Let&#8217;s now run mutation testing on our code. We are using TypeScript here, so let&#8217;s go with <a href="https://stryker-mutator.io/">Stryker Mutator</a>.</p>



<p>Here&#8217;s the result of running Stryker with <code>npx stryker run</code> on our code:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="958" height="163" data-attachment-id="4854" data-permalink="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/4-stryker-mutator-1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/4-stryker-mutator-1.jpg?fit=958%2C163&amp;ssl=1" data-orig-size="958,163" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4-stryker-mutator-1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/4-stryker-mutator-1.jpg?fit=958%2C163&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/4-stryker-mutator-1.jpg?fit=958%2C163&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/4-stryker-mutator-1.jpg?resize=958%2C163&#038;ssl=1" alt="Mutation test result - first run. Mutation score 78%, survived mutants 15" class="wp-image-4854" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/4-stryker-mutator-1.jpg?w=958&amp;ssl=1 958w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/4-stryker-mutator-1.jpg?resize=768%2C131&amp;ssl=1 768w" sizes="auto, (max-width: 958px) 100vw, 958px" /></figure>
</div>


<p></p>



<p>As you can see, 53 mutants were killed, but 15 of them survived. This gives us a mutation score of 78%. Let&#8217;s call it <em>mutation coverage</em> from now on. So to make it clear, this is our starting situation:</p>



<p><strong>8 unit tests, 100% code coverage, 78% mutation coverage</strong></p>



<p>Let&#8217;s now see how to kill those remaining mutants.</p>



<h2 class="wp-block-heading">Killing First Mutants</h2>



<p>In order to kill the mutants and improve our mutation score, we need to write more unit tests. Sounds logical, doesn&#8217;t it?</p>



<p>If still not, let&#8217;s take a deeper look at mutation tests results in VS Code. Stryker lists details of every survived mutant, for example this one:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="864" height="198" data-attachment-id="4855" data-permalink="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/5-stryker-mutant-1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/5-stryker-mutant-1.jpg?fit=864%2C198&amp;ssl=1" data-orig-size="864,198" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="5-stryker-mutant-1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/5-stryker-mutant-1.jpg?fit=864%2C198&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/5-stryker-mutant-1.jpg?fit=864%2C198&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/5-stryker-mutant-1.jpg?resize=864%2C198&#038;ssl=1" alt="First mutant generated by Stryker" class="wp-image-4855" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/5-stryker-mutant-1.jpg?w=864&amp;ssl=1 864w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/5-stryker-mutant-1.jpg?resize=768%2C176&amp;ssl=1 768w" sizes="auto, (max-width: 864px) 100vw, 864px" /></figure>
</div>


<p></p>



<p>Stryker changed the greater or equal (<code>>=</code>) operator to greater operator (<code>></code>). This is a very common and very useful mutation. It usually means that our tests don&#8217;t cover the edge case associated with this particular condition.</p>



<p>In this case, the edge case value seems to be <code>5</code>. More precisely, what happens if the number of business class tickets is <em>exactly 5</em>? We don&#8217;t know, because there is no test for this particular edge case!</p>



<p>Let&#8217;s add it:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist133063837" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-ticketspricecalculator_edgecase-test-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="ticketsPriceCalculator_edgeCase.test.ts content, created by dsibinski on 05:23PM on October 01, 2024."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="ticketsPriceCalculator_edgeCase.test.ts">
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC1" class="blob-code blob-code-inner js-file-line">  test(&quot;should apply 10% discount if there are exactly 5 business class tickets&quot;, () =&gt; {</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC2" class="blob-code blob-code-inner js-file-line">    const tickets = Array.from({ length: 5 }, () =&gt; ({</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC3" class="blob-code blob-code-inner js-file-line">      price: 10,</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC4" class="blob-code blob-code-inner js-file-line">      isBusinessClass: true,</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC5" class="blob-code blob-code-inner js-file-line">    }));</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC6" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC7" class="blob-code blob-code-inner js-file-line">    const totalPrice = calculator.calculateTotalTicketsPrice(tickets);</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC8" class="blob-code blob-code-inner js-file-line">    expect(totalPrice).toBe(45); // 10% discount</td>
        </tr>
        <tr>
          <td id="file-ticketspricecalculator_edgecase-test-ts-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-ticketspricecalculator_edgecase-test-ts-LC9" class="blob-code blob-code-inner js-file-line">  });</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/ee04a5c91df633a66935ed6a663f3ae1/raw/836811b2f5ba7f306346a9c11f4fe7de42ce2b89/ticketsPriceCalculator_edgeCase.test.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/ee04a5c91df633a66935ed6a663f3ae1#file-ticketspricecalculator_edgecase-test-ts" class="Link--inTextBlock">
          ticketsPriceCalculator_edgeCase.test.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>The test passes. Let&#8217;s run mutation tests again:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="961" height="166" data-attachment-id="4856" data-permalink="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/6-stryker-mutator-2/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/6-stryker-mutator-2.jpg?fit=961%2C166&amp;ssl=1" data-orig-size="961,166" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="6-stryker-mutator-2" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/6-stryker-mutator-2.jpg?fit=961%2C166&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/6-stryker-mutator-2.jpg?fit=961%2C166&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/6-stryker-mutator-2.jpg?resize=961%2C166&#038;ssl=1" alt="Struker Mutator second run - already 14 mutants survived and almost 80% mutation coverage" class="wp-image-4856" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/6-stryker-mutator-2.jpg?w=961&amp;ssl=1 961w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/6-stryker-mutator-2.jpg?resize=768%2C133&amp;ssl=1 768w" sizes="auto, (max-width: 961px) 100vw, 961px" /></figure>
</div>


<p></p>



<p>That&#8217;s it! We have a new unit test and 1 more mutant killed <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f389.png" alt="🎉" class="wp-smiley" style="height: 1em; max-height: 1em;" />  Current state:</p>



<p><strong>9 (+1) unit tests, 100% code coverage, 79% mutation coverage</strong></p>



<p>I think you&#8217;re now getting what this is all about. We should be adding unit tests to cover the edge cases detected by mutations until all (or most) mutants are killed.</p>



<h2 class="wp-block-heading">Mutation Testing &#8211; Free Unit Tests Generator</h2>



<p>After adding 1 more test, the results look as follows:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="960" height="164" data-attachment-id="4859" data-permalink="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/7-stryker-mutator-3/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/7-stryker-mutator-3.jpg?fit=960%2C164&amp;ssl=1" data-orig-size="960,164" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="7-stryker-mutator-3" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/7-stryker-mutator-3.jpg?fit=960%2C164&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/7-stryker-mutator-3.jpg?fit=960%2C164&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/7-stryker-mutator-3.jpg?resize=960%2C164&#038;ssl=1" alt="Struker mutator with 10 tests - 86% mutation coverage" class="wp-image-4859" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/7-stryker-mutator-3.jpg?w=960&amp;ssl=1 960w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/7-stryker-mutator-3.jpg?resize=768%2C131&amp;ssl=1 768w" sizes="auto, (max-width: 960px) 100vw, 960px" /></figure>
</div>


<p></p>



<p><strong>10 (+2) unit tests, 100% code coverage, 86% mutation coverage</strong></p>



<p>Finally, this is what we managed to get after a few minutes of work:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="959" height="165" data-attachment-id="4860" data-permalink="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/8-stryker-mutator-4/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/8-stryker-mutator-4.jpg?fit=959%2C165&amp;ssl=1" data-orig-size="959,165" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="8-stryker-mutator-4" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/8-stryker-mutator-4.jpg?fit=959%2C165&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/8-stryker-mutator-4.jpg?fit=959%2C165&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/8-stryker-mutator-4.jpg?resize=959%2C165&#038;ssl=1" alt="" class="wp-image-4860" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/8-stryker-mutator-4.jpg?w=959&amp;ssl=1 959w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2024/10/8-stryker-mutator-4.jpg?resize=768%2C132&amp;ssl=1 768w" sizes="auto, (max-width: 959px) 100vw, 959px" /></figure>
</div>


<p></p>



<p>And we now have 17 unit tests in our suite! The final score looks like that:</p>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f38a.png" alt="🎊" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>17 (+9) unit tests, 100% code coverage, 96% mutation coverage</strong> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f38a.png" alt="🎊" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Notice what happened here. <strong>We started with 8 unit tests and finished with 17! This is awesome! That&#8217;s how mutation testing is a free unit tests generator</strong>. And you don&#8217;t even need AI for that <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>What&#8217;s more, the generated mutations even forced me to make a little tweak to the production code. It exposed some things that didn&#8217;t make sense. I could make this change to the production code, because my tests&#8217; suite was already quite substantial.</p>



<p>Note that I didn&#8217;t push to gain 100% mutation coverage. The 3 mutants that survived don&#8217;t make much sense in our case. I encourage you to <a href="https://github.com/dsibinski/codejourney/tree/main/mutation-testing">get the code</a> and execute <code>npx stryker run</code> in the application folder to see for yourself <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>The percentage value doesn&#8217;t matter in that case. What matters is that we got 9 new unit tests and our tests&#8217; suite is more robust. Now we can go and tell our manager that the business-critical code is well tested <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60a.png" alt="😊" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">So What Now?</h2>



<p>I hope you see how valuable mutation testing is. It&#8217;s not a perfect tool that suits all cases. For example, it&#8217;s not easy to use mutation testing for complex legacy code. However, if your logic is nicely isolated, it could be a great tool. Especially if your production code heavily depends on calculations and many conditions. You may be surprised how many edge cases mutation testing can find.</p>



<p>I also love using mutation testing with TDD. These two techniques nicely complement each other.</p>



<p>I encourage you to go and try mutation testing. There are different mutation tools for various programming languages:</p>



<ul class="wp-block-list">
<li>C#, JavaScript, TypeScript, Scala: <a href="https://stryker-mutator.io/">Stryker Mutator</a></li>



<li>Ruby: <a href="https://github.com/mbj/mutant">mutant</a></li>



<li>Java: <a href="https://pitest.org/">pitest</a></li>



<li>PHP: <a href="https://infection.github.io/">Infection</a></li>



<li>C++: <a href="https://github.com/nlohmann/mutate_cpp">Mutate++</a></li>
</ul>



<p>&#8230;and probably many more. Just do some research for the language you work with and give it a try <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Because <a href="https://www.codejourney.net/improve-your-tests-with-assert-object-pattern/">testing</a> is cool, isn&#8217;t it? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>You can find the full source code used in this article <a href="https://github.com/dsibinski/codejourney/tree/main/mutation-testing">here</a>.</p>
<p>The post <a href="https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/">What is Mutation Testing and Why Should You Use It?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/what-is-mutation-testing-and-why-you-should-use-it/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4825</post-id>	</item>
		<item>
		<title>React State Management Basics</title>
		<link>https://www.codejourney.net/react-state-management-basics/</link>
					<comments>https://www.codejourney.net/react-state-management-basics/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sun, 29 Oct 2023 04:39:10 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[road-to-dotnet-full-stack]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4591</guid>

					<description><![CDATA[<p>If I were to point out a single concept that is critical to understand in React development, it would be state management. So what is React state management? How is it different from managing data in C# or Java? Let&#8217;s find out! 🙂 State If you are a backend or desktop developer, you may have&#8230;</p>
<p>The post <a href="https://www.codejourney.net/react-state-management-basics/">React State Management Basics</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If I were to point out a single concept that is critical to understand in React development, it would be state management.</p>



<p>So what is React state management? How is it different from managing data in C# or Java? Let&#8217;s find out! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4591"></span>



<h2 class="wp-block-heading">State</h2>



<p>If you are a backend or desktop developer, you may have never come across the term of state. At least, I never used it before learning frontend web development. Surely not in the context it is used in frameworks like React. The term more familiar to me at that time was <em>data binding</em> known from <a href="https://learn.microsoft.com/en-us/dotnet/desktop/wpf/overview">WPF</a> or <a href="https://learn.microsoft.com/en-us/dotnet/desktop/winforms/overview">WinForms</a>.</p>



<p>This is what <a href="https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data">WPF documentation</a> says:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>
<cite>Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically</cite></blockquote>



<p>In case of WPF, the crucial part to me is <em><strong>If the binding has the correct settings and the data provides the proper notifications</strong></em> <strong><em>(&#8230;)</em></strong>. If you have ever worked with WPF, you know how frustrating can these <em>notifications</em> be (any <code>INotifyPropertyChanged</code> fans here?).</p>



<p>WPF&#8217;s state management it not that bad. However, React has something better <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>React introduces us to a concept of <em>state</em>. In React components, you do not set anything directly, like calling some code to update an input&#8217;s value (this is possible, we&#8217;re still in <a href="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/">JavaScript world</a>, but it&#8217;s not a <em>React way</em> of doing stuff). You don&#8217;t directly bind your UI controls with data like you would in WPF or WinForms. Instead, you keep the data in memory, which is a current <em>state </em>of the UI (the whole app or a piece of it). </p>



<p><strong>When the state changes, your UI is updated by React</strong>. In other words &#8211; <strong>in React, UI is a function of state</strong>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-markup code-embed-pre"  data-start="1" data-line-offset="0"><code class="language-markup code-embed-code">UI = fn(state)</code></pre> <div class="code-embed-infos"> </div> </div></div>



<p>It means that your React app (or its individual pieces called <em>components</em>) take some state and output the actual, rendered UI.</p>



<p>This is React state management in a nutshell. Let&#8217;s explore it a bit more <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p><em>Small disclaimer:</em> <em>I will focus on state in React functional componets. I consider <a href="https://react.dev/reference/react/Component">class components somewhat legacy</a>. I think this meme summarizes it best <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" />:</em></p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://www.freecodecamp.org/news/functional-setstate-is-the-future-of-react-374f30401b6b/"><img data-recalc-dims="1" loading="lazy" decoding="async" width="500" height="381" data-attachment-id="4669" data-permalink="https://www.codejourney.net/react-state-management-basics/react-setstate-meme/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/react-setstate-meme.jpg?fit=500%2C381&amp;ssl=1" data-orig-size="500,381" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="react-setstate-meme" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/react-setstate-meme.jpg?fit=500%2C381&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/react-setstate-meme.jpg?fit=500%2C381&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/react-setstate-meme.jpg?resize=500%2C381&#038;ssl=1" alt="" class="wp-image-4669"/></a></figure>
</div>


<h3 class="wp-block-heading">The Naive Way</h3>



<p>Let&#8217;s try to approach React state management as C# developer. In React, the basic piece of UI we create is a component. Let&#8217;s add one:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">import Button from &quot;react-bootstrap/Button&quot;;<br/><br/>export const NaiveComponent = () =&gt; {<br/>  return (<br/>    &lt;&gt;<br/>      &lt;div&gt;I am a naive component&lt;/div&gt;<br/>      &lt;Button variant=&quot;success&quot;&gt;Click me!&lt;/Button&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">NaiveComponent.tsx</span> </div> </div></div>



<p>For now, we render some text and a button which does nothing. What we want is to have some data displayed, like a number, which is incremented on every button click. Let&#8217;s try to implement that naively:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const NaiveComponent = () =&gt; {<br/>  let myCount = 0;<br/><br/>  return (<br/>    &lt;&gt;<br/>      &lt;div&gt;I am a naive component&lt;/div&gt;<br/>      &lt;div&gt;Current count: {myCount}&lt;/div&gt;<br/>      &lt;Button<br/>        variant=&quot;success&quot;<br/>        onClick={() =&gt; {<br/>          myCount＋＋;<br/>        }}<br/>      &gt;<br/>        Click me!<br/>      &lt;/Button&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">NaiveComponent.tsx &#8211; the naive counter incrementing</span> </div> </div></div>



<p>Now, let&#8217;s check if it works:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="536" height="285" data-attachment-id="4602" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_wkzlxfcktf/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_wkZlXfCKTf.gif?fit=536%2C285&amp;ssl=1" data-orig-size="536,285" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_wkZlXfCKTf" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_wkZlXfCKTf.gif?fit=536%2C285&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_wkZlXfCKTf.gif?fit=536%2C285&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_wkZlXfCKTf.gif?resize=536%2C285&#038;ssl=1" alt="" class="wp-image-4602"/></figure>
</div>


<p></p>



<p>It looks nothing happens <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f979.png" alt="🥹" class="wp-smiley" style="height: 1em; max-height: 1em;" /> To investigate that, we can add a <code>console.log</code> (the most common debugging technique in JavaScript world <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f605.png" alt="😅" class="wp-smiley" style="height: 1em; max-height: 1em;" />) to the <code>onClick</code> event:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">onClick={() =&gt; {<br/>          myCount＋＋;<br/>          console.log(myCount);<br/>        }}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">onClick with console.log</span> </div> </div></div>



<p>How does that look in the console now?</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1071" height="591" data-attachment-id="4603" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_g62ao8n7fw/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?fit=1071%2C591&amp;ssl=1" data-orig-size="1071,591" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_g62ao8n7FW" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?fit=1071%2C591&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?fit=1071%2C591&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?resize=1071%2C591&#038;ssl=1" alt="React state management - naive way (like C# dev), not working" class="wp-image-4603" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?w=1071&amp;ssl=1 1071w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_g62ao8n7FW.gif?resize=768%2C424&amp;ssl=1 768w" sizes="auto, (max-width: 1071px) 100vw, 1071px" /></figure>
</div>


<p></p>



<p>Woooow, something is really going wrong here! The data is updated in memory, but not reflected in the UI <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Why does that happen? Because <code>myCount</code> is not a state. It&#8217;s just some variable we created hoping that it will work fine as part of the UI. Remember what we have discussed in the previous section &#8211; we need to start <em>thinking in React</em>. This is <em>The</em> <em>React way</em>, not <em>Java way </em>or <em>C# way </em>anymore.</p>



<h3 class="wp-block-heading">The React Way</h3>



<p>So, how to use React state management properly in our example? We need to make <code>myCount</code> be a part of component&#8217;s state. The simplest way to do that looks as follows:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="2,11" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const TheReactWayComponent = () =&gt; {<br/>  const [myCount, setMyCount] = useState(0);<br/><br/>  return (<br/>    &lt;&gt;<br/>      &lt;div&gt;I am a React Way component&lt;/div&gt;<br/>      &lt;div&gt;Current count: {myCount}&lt;/div&gt;<br/>      &lt;Button<br/>        variant=&quot;success&quot;<br/>        onClick={() =&gt; {<br/>          setMyCount(myCount ＋ 1);<br/>        }}<br/>      &gt;<br/>        Click me!<br/>      &lt;/Button&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">TheReactWayComponent.tsx &#8211; The React Way</span> </div> </div></div>



<p>First, instead of declaring a &#8220;normal&#8221; variable for storing our UI-related data, we used the <code>useState</code> <a href="https://react.dev/reference/react#state-hooks">hook</a>. As you can see, it returns an array with two things: our state variable (<code>myCount) </code>and a function to update its value<code> </code>(<code>setMyCount</code>). We also set the initial value to <code>0</code>. A good practice is to always use a construct like <code>[myCount, setMyCount]</code> to destructure what <code>useState</code> returns into named, separate objects.</p>



<p>Now we are managing our component&#8217;s state <em>The React Way</em>. Does it work? Let&#8217;s see:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="464" height="215" data-attachment-id="4624" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_ojmhjudmmd/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_OJMhjuDMmd.gif?fit=464%2C215&amp;ssl=1" data-orig-size="464,215" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_OJMhjuDMmd" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_OJMhjuDMmd.gif?fit=464%2C215&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_OJMhjuDMmd.gif?fit=464%2C215&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_OJMhjuDMmd.gif?resize=464%2C215&#038;ssl=1" alt="React state management - The React Way" class="wp-image-4624"/></figure>
</div>


<p></p>



<p>Great! We have just learned how the basic form of state management works in React. That was easy, wasn&#8217;t it? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>



<p>Now, let us discuss one more aspect related to the state itself.</p>



<h3 class="wp-block-heading">Reference vs value</h3>



<p>We know the React Way for state management now. We feel confident. However, we are also used to how data is managed in C# or Java, especially in terms of <a href="https://www.codejourney.net/net-internals-02-stack-and-heap-net-data-structures/">handling reference and value types</a>. This knowledge will come very handy now <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Let&#8217;s try to create a bit more complex component:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">type Person = {<br/>  id: number;<br/>  name: string;<br/>  age: number;<br/>};<br/><br/>export const ObjectHolderComponent = () =&gt; {<br/>  const johnInitialValue: Person = {<br/>    id: 1,<br/>    name: &quot;John Doe&quot;,<br/>    age: 30,<br/>  };<br/><br/>  const [john, setJohn] = useState&lt;Person&gt;(johnInitialValue);<br/><br/>  return (<br/>    &lt;&gt;<br/>      &lt;div&gt;ID: {john.id}&lt;/div&gt;<br/>      &lt;div&gt;Name: {john.name}&lt;/div&gt;<br/>      &lt;div&gt;Age: {john.age}&lt;/div&gt;<br/>      &lt;Button<br/>        variant=&quot;success&quot;<br/>        onClick={() =&gt; {<br/>          const currentJohnObject = john;<br/>          currentJohnObject.age = currentJohnObject.age ＋ 1;<br/>          console.log(currentJohnObject);<br/>          setJohn(currentJohnObject);<br/>        }}<br/>      &gt;<br/>        Make John older<br/>      &lt;/Button&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">ObjectHolderComponent.tsx</span> </div> </div></div>



<p>Now, instead of storing a single value in our component&#8217;s state, we store a whole object of type <code>Person</code>. The object has a few properties: <code>id</code>, <code>name</code> and <code>age</code>.</p>



<p>There is also a button. We want this button to take the current <code>john</code> object, increment the value of <code>age</code> on it and update the state. We do it The React Way using <code>setJohn</code> function returned by <code>useState</code>. Just to be sure, we also have the <code>console.log(john)</code> just before updating the state. Does that work as we expected?</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="873" height="656" data-attachment-id="4626" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_hlgfththbt/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?fit=873%2C656&amp;ssl=1" data-orig-size="873,656" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_hlgFthtHBT" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?fit=873%2C656&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?fit=873%2C656&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?resize=873%2C656&#038;ssl=1" alt="Changing an object's (reference) internal variable does not trigger a re-render, even we use useState" class="wp-image-4626" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?w=873&amp;ssl=1 873w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_hlgFthtHBT.gif?resize=768%2C577&amp;ssl=1 768w" sizes="auto, (max-width: 873px) 100vw, 873px" /></figure>
</div>


<p></p>



<p>Eh, what&#8217;s happening here this time? We can clearly see that <code>age</code> is updated on the object on each click, but why isn&#8217;t the UI re-rendered? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>As I mentioned before, it&#8217;s critical to understand the difference between reference and value types here. If you don&#8217;t know it, <a href="https://www.codejourney.net/net-internals-02-stack-and-heap-net-data-structures/">go and make up for it now</a>.</p>



<p>The reason it does not work is that we haven&#8217;t updated the actual content of <code>john</code> state variable by calling <code>setJohn(currentJohnObject)</code>. <strong>What is really being hold in <code>john</code> variable is the reference (pointer, address in memory) to the actual object</strong>. And this is what React tracks. We didn&#8217;t really update it, because we assigned the same instance of the object to the state. It doesn&#8217;t matter that we updated its internal property <code>age</code> &#8211; the reference itself wasn&#8217;t updated.</p>



<p>How do we solve that? Well, in current solution we&#8217;d need to create a copy of <code>john</code> object before using <code>setJohn</code> to alter the state. In effect, we&#8217;ll get a new instance of the object in memory and <code>john</code> variable will hold a new reference (address) to it.</p>



<p>So, if we only change our <code>onClick</code> implementation to this one:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">onClick={() =&gt; {<br/>          const johnCopy = { ...john };<br/>          johnCopy.age = john.age ＋ 1;<br/>          setJohn(johnCopy);<br/>        }}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">ObjectHolderComponent.tsx &#8211; onClick implementation with copying the original object</span> </div> </div></div>



<p>Everything starts to work as expected:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="333" height="236" data-attachment-id="4627" data-permalink="https://www.codejourney.net/react-state-management-basics/chrome_yfmhz6z0xn/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_yfmHz6z0Xn.gif?fit=333%2C236&amp;ssl=1" data-orig-size="333,236" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="chrome_yfmHz6z0Xn" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_yfmHz6z0Xn.gif?fit=333%2C236&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_yfmHz6z0Xn.gif?fit=333%2C236&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/chrome_yfmHz6z0Xn.gif?resize=333%2C236&#038;ssl=1" alt="Changing a reference state by creating a copy of it works fine in React" class="wp-image-4627"/></figure>
</div>


<p></p>



<p>I think you now understand how React state management works. I also hope that you see the issues managing state in such a way can bring. Even in this trivial example, we create a new instance of an object on every button click. Apart from memory usage considerations, this just looks ugly. Imagine manually creating copies of much more complex objects, like classes storing arrays or dictionaries. This gets pretty wild <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92a.png" alt="🤪" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>We will see how this issue can be addressed by the end of this article. But first, let&#8217;s discuss <code>props</code> &#8211; another concept critical to really understand React state management.</p>



<h2 class="wp-block-heading">Props</h2>



<p>Apart from <code>state</code>, we also use <code>props</code> to control the state of our React components. The name is a short for <em>properties</em>, which quite well describes its purpose. <code>Props</code> are the data input provided to a component from outside (from a parent component). They are immutable and cannot be changed inside the component which receives them. Let&#8217;s see some examples.</p>



<h3 class="wp-block-heading">Sharing state between components</h3>



<p>The most common usage for <code>props</code> is sharing state between components. Imagine that you have an input, where the user manually enters some data. You want to use the current value of this input in two other components. This is where you need to share your component&#8217;s state (the value of the input) with these two other components. We often call it <em>lifting the state up</em> &#8211; you keep your state higher in the components tree, so the child components can use it.</p>



<p>Let&#8217;s try to see an example of connecting <code>state</code> with <code>props</code>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="4,9,27,28" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const PersonData = () =&gt; {<br/>  const minimumAge = 5;<br/>  const maximumAge = 100;<br/>  const [age, setAge] = useState&lt;number&gt;(minimumAge);<br/><br/>  const handleAgeChange = (event: React.ChangeEvent&lt;HTMLInputElement&gt;) =&gt; {<br/>    const enteredAge = parseInt(event.target.value);<br/>    if (enteredAge &gt;= minimumAge &amp;&amp; enteredAge &lt;= maximumAge) {<br/>      setAge(enteredAge);<br/>    }<br/>  };<br/><br/>  return (<br/>    &lt;&gt;<br/>      &lt;Form&gt;<br/>        &lt;Form.Group controlId=&quot;age&quot;&gt;<br/>          &lt;Form.Label&gt;Your age&lt;/Form.Label&gt;<br/>          &lt;Form.Control<br/>            type=&quot;number&quot;<br/>            value={age}<br/>            onChange={handleAgeChange}<br/>            min={minimumAge}<br/>            max={maximumAge}<br/>          /&gt;<br/>        &lt;/Form.Group&gt;<br/>      &lt;/Form&gt;<br/>      &lt;DaysLiving currentAge={age} /&gt;<br/>      &lt;YearsUntilCentenarian currentAge={age} /&gt;<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">PersonData.tsx &#8211; state and props in practice</span> </div> </div></div>



<p>As you can see, at lines 4 and 9 we manage the state variable <code>age</code>. This is the old stuff we have already seen.</p>



<p>However, at lines 27 and 28 we are passing the <code>age</code> state variable into <code>DaysLiving</code> and <code>YearsUntilCentenarian</code> children components as <code>currentAge</code> prop. This is how the <code>DaysLiving</code> component is implemented:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">type DaysLivingProps = {<br/>  currentAge: number;<br/>};<br/><br/>export const DaysLiving = (props: DaysLivingProps) =&gt; {<br/>  const daysLived = props.currentAge * 365;<br/><br/>  return (<br/>    &lt;div&gt;<br/>      &lt;p&gt;You have lived for {daysLived} days.&lt;/p&gt;<br/>    &lt;/div&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">DaysLiving.tsx</span> </div> </div></div>



<p>Notice that <code>props</code> is simply a typed object you pass to a function component. To make things easier, you can also use object destructuring and accept the props in the following way:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const DaysLiving = ({currentAge}: DaysLivingProps) =&gt; {<br/>  const daysLived = currentAge * 365;<br/>  /// ... rest of the code</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">DaysLiving.tsx &#8211; props with object destructuring</span> </div> </div></div>



<p>That way, you don&#8217;t need to write <code>props.currentAge</code> every time you want to use this property inside the component.</p>



<h3 class="wp-block-heading">Does changing props always trigger a re-render?</h3>



<p>Well, generally the answer to this question is <em>yes</em>. Every time a prop of a component changes, this component will get re-rendered. However, this is not <em>entirely</em> true <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92a.png" alt="🤪" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>To make this belief true, we need to assume that this prop is changed <em>in a React way</em>. So, to take things literally, consider this component we saw before:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const DaysLiving = ({currentAge}: DaysLivingProps) =&gt; {<br/>  const daysLived = currentAge * 365;<br/><br/>  return (<br/>    &lt;div&gt;<br/>      &lt;p&gt;You have lived for {daysLived} days.&lt;/p&gt;<br/>    &lt;/div&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">DaysLiving.tsx</span> </div> </div></div>



<p>From its perspective only, it accepts a <code>currentAge</code> property. For this component to re-render, it&#8217;s not enough that the <code>currentAge</code> property changes in any way. <strong>Its value must be changed by mutating the state in the parent component</strong>.</p>



<p>So, if the value provided as <code>currentAge</code> to <code>DaysLiving</code> component changes in the parent component by properly mutating the state (with <code>setAge</code> mutation callback, in our case):</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="7,14" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const PersonData = () =&gt; {<br/>  // ...<br/>  const [age, setAge] = useState&lt;number&gt;(minimumAge);<br/><br/>  const handleAgeChange = (event: React.ChangeEvent&lt;HTMLInputElement&gt;) =&gt; {<br/>      // ...<br/>      setAge(enteredAge);<br/>    }<br/>  };<br/><br/>  return (<br/>    &lt;&gt;<br/>      // ...<br/>      &lt;DaysLiving currentAge={age} /&gt;<br/>      // ...<br/>    &lt;/&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">PersonData.tsx &#8211; mutating the state for children components</span> </div> </div></div>



<p>the child component (<code>DaysLiving</code>) will get re-rendered.</p>



<p>This is because when a parent component changes, React by default re-renders the parent component itself and all of its children.</p>



<p>If you modified the <code>age</code> variable in the parent compont directly, without using <code>setAge</code> (which we already know it&#8217;s incorrect), the <code>currentAge</code> prop would technically change, but the <code>DaysLiving</code> component will not get re-rendered.</p>



<h3 class="wp-block-heading">state vs props</h3>



<p>This is a question that people oftern ask: what&#8217;s the difference between <code>props</code> and <code>state</code> in React? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>I would start with addressing this differently: what do <code>props</code> and <code>state</code> have in common?</p>



<p>As you already know, both are used for React state management. Both of them influence when the components will be re-rendered by React. Props and state are the absolute foundations of React.</p>



<p>The main difference is that <code>state</code> is used internally in a component, while <code>props</code> are used to pass information from parent to children components. <code>State</code> is also naturally mutable within a component where it&#8217;s defined, but <code>props</code> are immutable and cannot be changed in a component that receives and uses them.</p>



<h2 class="wp-block-heading">React state management libraries</h2>



<p>That&#8217;s basically everything you need to know to be able to comfortably work with React state management. However, I mentioned before that managing state using only built-in React mechanisms actually sucks <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92a.png" alt="🤪" class="wp-smiley" style="height: 1em; max-height: 1em;" /> That&#8217;s why I&#8217;d like to make a short point on state management libraries.</p>



<p>Maybe you heard about <a href="https://redux.js.org/">Redux</a>, <a href="https://github.com/pmndrs/zustand">Zustand</a> or <a href="https://mobx.js.org/README.html">MobX</a>. All of them are state management libraires for React. But why would you even need one? And should you learn one now?</p>



<p>In my opinion, you can easily start working with React using the built-in state management mechanisms we discussed today. It&#8217;s good to know how it works in practice. However, the truth is that as your application grows and you want to keep it scalable and maintenable, you will need a state management library at some point. As soon as your components tree gets bigger, sharing data and state will not be as easy as &#8220;passing props to a children component&#8221;. You will need to share state between multiple components in the tree, sometimes not only between parent and children, but also with another components defined on the same level or even in a totally different place of the tree.</p>



<p>This is where React state management libraries come very handy. They solve many of the React&#8217;s issues that come to the surface as your state gets complex. But even if you use one, you will still always need <code>state</code> and <code>props</code>. That&#8217;s why it&#8217;s critical to understand these two basic concepts first and play with them for some time.</p>



<p>If you&#8217;re starting out, it&#8217;s good to know that state management libraries exist and that one day you will have to get your hands on one (or more <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" />) of them. I hope to share my thoughts on state management libraries in a separate article one day.</p>



<h2 class="wp-block-heading">Summary</h2>



<p>That&#8217;s it! Now you should know how React manages state of its components. Of course, we didn&#8217;t cover everything related to state management in React, but I hope you got the basics <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>You can find all of the source code used in this article <a href="https://github.com/dsibinski/codejourney/tree/main/react-state-management">here</a>.</p>



<p><strong>How do you manage state in your React apps? Do you use a state management library? If yes, which one and why? Let me know in the comments below!</strong></p>
<p>The post <a href="https://www.codejourney.net/react-state-management-basics/">React State Management Basics</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/react-state-management-basics/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4591</post-id>	</item>
		<item>
		<title>TypeScript Compiler Explained</title>
		<link>https://www.codejourney.net/typescript-compiler-explained/</link>
					<comments>https://www.codejourney.net/typescript-compiler-explained/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Tue, 17 Oct 2023 04:08:05 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[road-to-dotnet-full-stack]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4530</guid>

					<description><![CDATA[<p>As a frontend developer, one of the things you should know is how TypeScript compiler works. Sooner or later you will work with this language (which I sincerely wish you!), so it&#8217;s good to know your stuff 😉 In this article, I will explain TypeScript compiler to you in simple terms. We will avoid complex&#8230;</p>
<p>The post <a href="https://www.codejourney.net/typescript-compiler-explained/">TypeScript Compiler Explained</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>As a frontend developer, one of the things you should know is how TypeScript compiler works. Sooner or later you will work with this language (which I sincerely wish you!), so it&#8217;s good to know your stuff <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>



<p>In this article, I will explain TypeScript compiler to you in simple terms. We will avoid complex stuff &#8211; only what you need for your everyday frontend developer&#8217;s work. We will not explore the inner workings of the TypeScript compiler Instead, we&#8217;ll see some practical implications of its workings for TypeScript developer. Let&#8217;s dive in <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4530"></span>



<h2 class="wp-block-heading">Few basic facts about TypeScript</h2>



<p>For starters, let&#8217;s establish some quick facts about TypeScript as a language.</p>



<h3 class="wp-block-heading">TypeScript is a superset of JavaScript</h3>



<p>You may have heard that TypeScript is a superset of JavaScript. In other words, every JavaScript code is a valid TypeScript code. In  case of TypeScript, being a &#8220;superset&#8221; means adding types information on top of perfectly legit JavaScript code.</p>



<p>Why is TypeScript only adding its types on top of JavaScript? In order to be able to remove them easily <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> But why would it do that?!</p>



<h3 class="wp-block-heading">Browser doesn&#8217;t understand TypeScript</h3>



<p>The answer is that TypeScript cannot be run directly by a web browser (or NodeJS).</p>



<p>Let&#8217;s take this trivial TypeScript code:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">let age : number = 27;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">Trivial TypeScript code</span> </div> </div></div>



<p>and try to paste it into Google Chrome&#8217;s console in order to execute it:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="438" height="145" data-attachment-id="4535" data-permalink="https://www.codejourney.net/typescript-compiler-explained/typescript-in-chrome-console/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/TypeScript-in-chrome-console.png?fit=438%2C145&amp;ssl=1" data-orig-size="438,145" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TypeScript-in-chrome-console" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/TypeScript-in-chrome-console.png?fit=438%2C145&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/TypeScript-in-chrome-console.png?fit=438%2C145&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/TypeScript-in-chrome-console.png?resize=438%2C145&#038;ssl=1" alt="TypeScript code cannot be understood by a web browser" class="wp-image-4535"/></figure>
</div>


<p></p>



<p>As you can see, it fails. The <code>number</code> type annotation is unknown to the browser. It means that <strong>before any TypeScript code is run in the browser, the information about types must be stripped out</strong>. In the end, browsers and NodeJS environments can only execute JavaScript. This is in short what TypeScript compiler does, and we&#8217;ll get into the details soon. For now, this leads us to another conclusion, that&#8230;</p>



<h3 class="wp-block-heading">TypeScript is for developers</h3>



<p>As we have just discovered, types must be removed by the TypeScript compiler before the code can be executed by the browser. In fact, this line of TypeScript:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">let age : number = 27;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">TypeScript code before compilation</span> </div> </div></div>



<p>produces the following JavaScript output when processed by the TypeScript compiler:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">var age = 27;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">JavaScript code after TypeScript compilation</span> </div> </div></div>



<p>This is effectively the same code we wrote in TypeScript, but without type annotations.</p>



<p>Wrong data types assignments will only be detected at compile time:</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="576" height="241" data-attachment-id="4549" data-permalink="https://www.codejourney.net/typescript-compiler-explained/tsc-compilation-error/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/tsc-compilation-error.png?fit=576%2C241&amp;ssl=1" data-orig-size="576,241" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="tsc-compilation-error" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/tsc-compilation-error.png?fit=576%2C241&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/tsc-compilation-error.png?fit=576%2C241&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/tsc-compilation-error.png?resize=576%2C241&#038;ssl=1" alt="" class="wp-image-4549"/></figure>



<p></p>



<p>but as soon as it&#8217;s compiled, this protection is not there anymore <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f937-200d-2642-fe0f.png" alt="🤷‍♂️" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>



<p>This discovery means that <strong>TypeScript is for developers</strong>. Its job is to make our lives easier. Of course, in the end it also improves the end users&#8217; experience, because <a href="https://www.codejourney.net/is-it-worth-migrating-to-typescript/">programming in TypeScript is much better than using pure JS</a>. But you can already see that TypeScript it not present at runtime at all. It only protects us until the compilation step by static types checking.</p>



<p>The philosophy is more or less as follows <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" />:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://anjulashanaka.medium.com/say-goodbye-to-runtime-errors-with-typescript-7afbae84e75f"><img data-recalc-dims="1" loading="lazy" decoding="async" width="602" height="333" data-attachment-id="4551" data-permalink="https://www.codejourney.net/typescript-compiler-explained/typescript-no-runtime-errors-if-code-does-not-compile-meme/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/typescript-no-runtime-errors-if-code-does-not-compile-meme.jpg?fit=602%2C333&amp;ssl=1" data-orig-size="602,333" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}" data-image-title="typescript-no-runtime-errors-if-code-does-not-compile-meme" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/typescript-no-runtime-errors-if-code-does-not-compile-meme.jpg?fit=602%2C333&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/typescript-no-runtime-errors-if-code-does-not-compile-meme.jpg?fit=602%2C333&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/typescript-no-runtime-errors-if-code-does-not-compile-meme.jpg?resize=602%2C333&#038;ssl=1" alt="TypeScript compiler only protects you at runtime (during compilation)" class="wp-image-4551"/></a></figure>
</div>


<p></p>



<p>What&#8217;s worth mentioning, as the <a href="https://www.typescriptlang.org/docs/handbook/typescript-from-scratch.html">language&#8217;s specification</a> says, &#8220;TypeScript never changes the runtime behavior of JavaScript code&#8221;.</p>



<h2 class="wp-block-heading">TypeScript compiler in practice</h2>



<h3 class="wp-block-heading">tsc </h3>



<p>TypeScript compiler is actually a CLI tool called <code>tsc</code>. It can be installed globally on your machine and used directly or built into your IDE. Refer to <a href="https://www.typescriptlang.org/download">official documentation</a> for installation details.</p>



<p>The compiler can also be installed locally in your project. It can then be set up as part of the build pipeline, which is supported by many <a href="https://www.codejourney.net/what-is-a-javascript-bundler/">bundlers</a>. That&#8217;s why, when working on a TypeScript project, you may never encounter any direct usages of the <code>tsc</code> command. </p>



<h3 class="wp-block-heading">tsconfig.json</h3>



<p>In order to make working with TypeScript compiler easier, you should use a <code>tsconfig.json</code> file. It&#8217;s a JSON file containing all compilation settings. This is quite important, because, in opposite to C# or Java compilers,  the TypeScript&#8217;s one allows for quite extensive (and flexible) configuration. </p>



<p>When you use the <code>tsc</code> command, it looks for <code>tsconfig.json</code> file in the current directory or any of the parent directories until it finds one. You can also provide a custom <code>tsconfig.json</code> location via <code>--project</code> parameter.</p>



<p>The simplest <code>tsconfig.json</code> file you get after initializing a new TypeScript project with <code>tsc --init</code> looks as follows:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">{<br/>  &quot;compilerOptions&quot;: {<br/>    &quot;target&quot;: &quot;es2016&quot;,<br/>    &quot;module&quot;: &quot;commonjs&quot;,<br/>    &quot;forceConsistentCasingInFileNames&quot;: true,<br/>    &quot;strict&quot;: true,<br/>    &quot;skipLibCheck&quot;: true<br/>  }<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">Default tsconfig.json</span> </div> </div></div>



<p>We will not explore all the individual settings in this article. The <a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html">official documentation does a great job</a>.</p>



<h3 class="wp-block-heading">TypeScript compiler &#8211; example</h3>



<p>Let&#8217;s now see how it works.</p>



<p>Having a bit more complex TypeScript file as an example:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">class Person {<br/>  private name: string;<br/>  private age: number;<br/><br/>  constructor(name: string, age: number) {<br/>    this.name = name;<br/>    this.age = age;<br/>  }<br/><br/>  public getName(): string {<br/>    return this.name;<br/>  }<br/><br/>  public getAge(): number {<br/>    return this.age;<br/>  }<br/>}<br/><br/>const person = new Person(&quot;John&quot;, 27);<br/>console.log(person.getName()); // Output: John<br/>console.log(person.getAge()); // Output: 27</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">test.ts</span> </div> </div></div>



<p>Notice the <code>.ts</code> extension. This tells the compiler that this file contains TypeScript code.</p>



<p>Having <code>tsc</code> installed globally, we can compile it by executing the following command:</p>



<pre class="wp-block-code"><code>tsc test.ts</code></pre>



<p>For the file given above, the output of this command is the following JavaScript file:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">var Person = /** @class */ (function () {<br/>  function Person(name, age) {<br/>    this.name = name;<br/>    this.age = age;<br/>  }<br/>  Person.prototype.getName = function () {<br/>    return this.name;<br/>  };<br/>  Person.prototype.getAge = function () {<br/>    return this.age;<br/>  };<br/>  return Person;<br/>})();<br/>var person = new Person(&quot;John&quot;, 27);<br/>console.log(person.getName()); // Output: John<br/>console.log(person.getAge()); // Output: 27</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">test.js</span> </div> </div></div>



<p>Few things to note:</p>



<ul class="wp-block-list">
<li>there is no information about types in the <code>.js</code> file. As we discussed before, this is intentional &#8211; only pure JavaScript code can be understood by the browser</li>



<li>notice there is no <code>class</code> keyword used in the compiled JS code, even though classes were introduced in ES2015. This is one of TypeScript compiler&#8217;s features &#8211; to produce code which is supported in most of the browsers and JS engines, without having to wait for support of new JavaScript versions</li>



<li>comments are left untouched</li>
</ul>



<p>This explains how the TypeScript compiler works. It also confirms what we stated before, that TypeScript is for programmers. As soon as your code is deployed to production, the types you added in <code>.ts</code> files are absent. The way this process is built gives a lot of advantages, but it may also be a source of confusion &#8211; let&#8217;s see how exactly.</p>



<h2 class="wp-block-heading">TypeScript compilation flaws</h2>



<p>Finally, knowing how TS -&gt; JS compilation works, let&#8217;s quickly explore some of its common flaws.</p>



<h3 class="wp-block-heading">No runtime protection</h3>



<p>As you now know, TypeScript stripes out the types during compilation. It means that the information about types is not present at runtime (when our code executes). Because of that,<strong> we are not protected by TypeScript at runtime</strong>. Consequently, if an input comes from a user and the inputs themselves are not well validated, we may still get runtime type errors. Imagine that your code expects a <code>number</code>, but the user enters a <code>string</code> because of lack of proper input validation. TypeScript will not protect you here.</p>



<p>The same applies to validating API responses. Most data from HTTP APIs comes in a form of JSON. In TypeScript, we can represent such data as <code>type</code> or <code>interface</code>. Based on those expected shapes of data, we use objects of a given type and assume that given properties are present on them or not. However, APIs may change, and TypeScript will again not protect us at runtime. We still need to <a href="https://www.codejourney.net/typing-api-responses-with-zod/">resort to alternative solutions for runtime types validation</a>.</p>



<p>To be completely clear &#8211; I don&#8217;t think that no runtime protection is something TypeScript lacks. On the contrary &#8211; I love the flexibility of TypeScript which this approach gives. It&#8217;s just how the language has been designed, and we should be aware of that <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Debugging complexity</h3>



<p>When working on a TypeScript project, the code you see on production is different from the one in your IDE. Sometimes not only types are stripped out by TypeScript compiler, but as you saw earlier it may also convert some constructs to the other (like changing a <code>class</code> to a <code>function</code>). It makes debugging, especially directly on production, more complex.</p>



<p>In order to place breakpoints and actually have them hit in your TypeScript code, you need <a href="https://code.visualstudio.com/docs/typescript/typescript-debugging#_javascript-source-map-support">source maps</a>. It does the job in most cases, but adds to the complexity at the same time, not always working seamlessly.</p>



<h3 class="wp-block-heading">TypeScript is sometimes too strict</h3>



<p>If you decide to <a href="https://www.codejourney.net/is-it-worth-migrating-to-typescript/">migrate your JS project to TypeScript</a>, you will quickly get frustrated by how strict this new language can be. Especially if you are coming from JavaScript world where <a href="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/">everything is allowed</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f605.png" alt="😅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>There are countless memes about that, but here is the one I like:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://devs.lol/meme/for-any-emergency-in-typescript-222"><img data-recalc-dims="1" loading="lazy" decoding="async" width="576" height="505" data-attachment-id="4554" data-permalink="https://www.codejourney.net/typescript-compiler-explained/typescript-any-type-meme/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/typescript-any-type-meme.jpg?fit=576%2C505&amp;ssl=1" data-orig-size="576,505" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="typescript-any-type-meme" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/typescript-any-type-meme.jpg?fit=576%2C505&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/typescript-any-type-meme.jpg?fit=576%2C505&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/10/typescript-any-type-meme.jpg?resize=576%2C505&#038;ssl=1" alt="TypeScript compiler will often let you go with any type" class="wp-image-4554"/></a></figure>
</div>


<p></p>



<p>Joking apart, most things TypeScript complaints about can be configured/turned off in <code>tsconfig.json</code> file. This is actually what I like about TypeScript. It brings static typing into JavaScript world, but lets you control how strict you want it to be. So, if you get frustrated with TypeScript compiler&#8217;s complaints, remember about <a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html">configuration options</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>



<h2 class="wp-block-heading">Summary</h2>



<p>I hope that now you feel more comfortable working with TypeScript compiler <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>If you&#8217;re a .NET developer, and you enjoyed this article, I think you may also find my free guide useful:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://mailchi.mp/055b94b02391/net-full-stack-web-developer-guide" target="_blank" rel=" noreferrer noopener"><img data-recalc-dims="1" loading="lazy" decoding="async" width="300" height="300" data-attachment-id="4950" data-permalink="https://www.codejourney.net/how-to-become-dotnet-full-stack-in-2025-300-300/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?fit=300%2C300&amp;ssl=1" data-orig-size="300,300" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}" data-image-title="How-to-become-dotnet-full-stack-in-2025-300-300" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?fit=300%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?fit=300%2C300&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?resize=300%2C300&#038;ssl=1" alt="16 Steps to become .NET full stack developer in 2025 - download a free guide" class="wp-image-4950" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?w=300&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?resize=50%2C50&amp;ssl=1 50w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></figure>
</div><p>The post <a href="https://www.codejourney.net/typescript-compiler-explained/">TypeScript Compiler Explained</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/typescript-compiler-explained/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4530</post-id>	</item>
		<item>
		<title>What Is A JavaScript Bundler?</title>
		<link>https://www.codejourney.net/what-is-a-javascript-bundler/</link>
					<comments>https://www.codejourney.net/what-is-a-javascript-bundler/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sat, 02 Sep 2023 08:16:11 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[road-to-dotnet-full-stack]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4453</guid>

					<description><![CDATA[<p>If you asked me 5 years ago what a JavaScript bundler is, I&#8217;d probably tell you it&#8217;s something people fight with for hours, just to get a simple web app set up 🤪 While this might have been closer to the truth in 2018, a lot has changed in JavaScript ecosystem until today. If you&#8217;re&#8230;</p>
<p>The post <a href="https://www.codejourney.net/what-is-a-javascript-bundler/">What Is A JavaScript Bundler?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you asked me 5 years ago what a JavaScript bundler is, I&#8217;d probably tell you it&#8217;s something people fight with for hours, just to get a simple web app set up <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92a.png" alt="🤪" class="wp-smiley" style="height: 1em; max-height: 1em;" /> While this might have been closer to the truth in 2018, a lot has changed in JavaScript ecosystem until today.</p>



<p>If you&#8217;re starting your web development journey, or maybe have already dived into it, but are not really sure what JS bundlers are and what&#8217;s their role, you&#8217;re reading a proper piece of explanation <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4453"></span>



<h2 class="wp-block-heading">Chaos and complexity &#8211; the issues</h2>



<p>Let&#8217;s consider a very simple JavaScript app. We have just one page <code>index.html</code>. Our HTML is hydrated with <code>index.js</code> file, which adds some interactivity to it. In order to use it, we need to import <code>index.js</code> script in <code>index.html</code> file. We also decided to use an external library (Bootstrap), so we should also link the necessary CSS.</p>



<p>We are linking the <code>index.js</code> with a <code>script</code> tag in <code>head</code> section:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-markup code-embed-pre line-numbers"  data-line="2" data-start="1" data-line-offset="0"><code class="language-markup code-embed-code">&lt;head&gt;<br/>    &lt;script type=&quot;module&quot; src=&quot;index.js&quot;&gt;&lt;/script&gt;<br/>    &lt;link<br/>      href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot;<br/>      rel=&quot;stylesheet&quot;<br/>      integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot;<br/>      crossorigin=&quot;anonymous&quot;<br/>    /&gt;<br/>    &lt;meta charset=&quot;UTF-8&quot; /&gt;<br/>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;<br/>    &lt;title&gt;My WebPage&lt;/title&gt;<br/>  &lt;/head&gt;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">Linking internal scripts and external library files in HTML page</span> </div> </div></div>



<p>Of course, as we are building a web application, we won&#8217;t put all the JavaScript code in <code>index.js</code>. That&#8217;s why we created two separate files with some functionalities: <code>my-utils.js</code> and <code>navigation.js</code>. </p>



<p>For instance, the <code>my-utils.js</code> exports a few functions:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">export function doSomethingNice() {<br/>  alert(&quot;Nice!&quot;);<br/>}<br/><br/>export function notUsedFunctionA() {<br/>  alert(&quot;Not used function A&quot;);<br/>}<br/><br/>export function notUsedFunctionB() {<br/>  alert(&quot;Not used function B&quot;);<br/>}<br/><br/>export function notUsedFunctionC() {<br/>  alert(&quot;Not used function C&quot;);<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">Functions exported from a JavaScript module file</span> </div> </div></div>



<p>Notice the not used functions there &#8211; we&#8217;ll get to the point of them soon.</p>



<p>Next, our <code>index.js</code> file imports what it needs and adds some interactivity to the HTML elements:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">import { doSomethingNice } from &quot;./my-utils.js&quot;;<br/>import { reloadThePage } from &quot;./navigation.js&quot;;<br/><br/>document<br/>  .getElementById(&quot;reload-page-btn&quot;)<br/>  .addEventListener(&quot;click&quot;, reloadThePage);<br/><br/>document<br/>  .getElementById(&quot;say-sth-nice-btn&quot;)<br/>  .addEventListener(&quot;click&quot;, doSomethingNice);</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">index.js &#8211; adding interactivity to index.html</span> </div> </div></div>



<p>If we now open our webpage, we can see the network activity when the page is loaded:</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1131" height="330" data-attachment-id="4464" data-permalink="https://www.codejourney.net/what-is-a-javascript-bundler/html-loading-javascript-1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-loading-javascript-1.png?fit=1131%2C330&amp;ssl=1" data-orig-size="1131,330" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="html-loading-javascript-1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-loading-javascript-1.png?fit=1131%2C330&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-loading-javascript-1.png?fit=1131%2C330&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-loading-javascript-1.png?resize=1131%2C330&#038;ssl=1" alt="" class="wp-image-4464" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-loading-javascript-1.png?w=1131&amp;ssl=1 1131w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-loading-javascript-1.png?resize=768%2C224&amp;ssl=1 768w" sizes="auto, (max-width: 1131px) 100vw, 1131px" /></figure>



<p></p>



<p>Notice that all 3 JavaScript files are fetched separately: <code>index.js</code>, <code>my-utils.js</code> and <code>navigation.js</code>.</p>



<p>What&#8217;s more, if you check the contents of <code>my-utils.js</code>, it obviously loads the whole script, containing even the not used functions:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="558" height="300" data-attachment-id="4465" data-permalink="https://www.codejourney.net/what-is-a-javascript-bundler/html-utils-fetching-content/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-utils-fetching-content.png?fit=558%2C300&amp;ssl=1" data-orig-size="558,300" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="html-utils-fetching-content" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-utils-fetching-content.png?fit=558%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-utils-fetching-content.png?fit=558%2C300&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/html-utils-fetching-content.png?resize=558%2C300&#038;ssl=1" alt="" class="wp-image-4465"/></figure>
</div>


<p></p>



<p>I think you might already feel that this can get a little problematic <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Imagine managing this when you have hundreds of your own JavaScript files plus dozens of external libraries. Not only will your app be making hundreds of network requests to fetch every single file, but it will always fetch everything &#8211; even the stuff your page doesn&#8217;t use.</p>



<p>Here&#8217;s where a JavaScript bundler comes into the field. </p>



<p>You can find full source code of this JavaScript app <a href="https://github.com/dsibinski/codejourney/tree/main/javascript-bundlers/no-bundler-app">here</a>.</p>



<h2 class="wp-block-heading">What does a JavaScript bundler do?</h2>



<p>JavaScript bundler produces <em>bundles</em> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> But what is a bundle? It&#8217;s basically a static file, optimized to be served to the client (in our case: a web browser). The main goal is to produce a single bundle file from multiple dependencies. In effect, the user&#8217;s browser doesn&#8217;t need to fetch so many files separately. But how is such a bundle created?</p>



<p>First, a JavaScript bundler needs to know the <em>entry point</em>. It&#8217;s a kind of root in which our &#8220;code tree&#8221; begins. In the case of our sample JavaScript application from the previous paragraph, a perfect candidate for an entry point would be the <code>index.js</code> file. Modern bundlers support multiple entry points. It can be very useful if you&#8217;re building a multipage application, so each page can have its own entry point.</p>



<p>The next step is to create a dependency graph. The JavaScript bundler is basically starting from the root (entry point) and traverses all dependencies referenced from it, as well as all of these dependencies&#8217; dependencies. Having the dependency graph, bundler knows what is exactly used by what and in which place of our app. </p>



<p>This is how you can imagine the graph of dependencies:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><a href="https://paulallies.medium.com/webpack-managing-javascript-and-css-dependencies-3b4913f49c58"><img data-recalc-dims="1" loading="lazy" decoding="async" width="786" height="344" data-attachment-id="4469" data-permalink="https://www.codejourney.net/what-is-a-javascript-bundler/webpack-dependency-graph/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-dependency-graph.webp?fit=786%2C344&amp;ssl=1" data-orig-size="786,344" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="webpack-dependency-graph" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-dependency-graph.webp?fit=786%2C344&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-dependency-graph.webp?fit=786%2C344&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-dependency-graph.webp?resize=786%2C344&#038;ssl=1" alt="" class="wp-image-4469" style="width:858px;height:348px" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-dependency-graph.webp?w=786&amp;ssl=1 786w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-dependency-graph.webp?resize=768%2C336&amp;ssl=1 768w" sizes="auto, (max-width: 786px) 100vw, 786px" /></a></figure>
</div>


<p></p>



<p>Module bundlers like <a href="https://webpack.js.org/">webpack</a> not only know the dependencies of your entry point. These tools even check what each dependency uses. Thanks to building such a detailed dependency graph, the bundler can introduce multiple optimizations into the bundle file.</p>



<p>Finally, JavaScript bundler outputs the actual bundles. As I mentioned, bundles can be highly optimized by applying techniques like <a href="https://webpack.js.org/guides/code-splitting/">code splitting</a>, <a href="https://vitejs.dev/guide/features.html#css">CSS inlining</a>, <a href="https://parceljs.org/features/development/#hot-reloading">HMR</a> and various performance optimizations.</p>



<p>JavaScript bundler might also ensure the backwards-compatibility with older web browsers by converting some newer JavaScript constructs used in your code to their equivalents from previous JS versions.</p>



<p>What&#8217;s interesting, modern bundlers can handle not only JS files, but also CSS, HTML and even images.</p>



<h2 class="wp-block-heading">JavaScript bundler in action</h2>



<p>Currently, the most popular JavaScript bundles is (still) <a href="https://webpack.js.org/">webpack</a>. It has been the first really advanced bundler created for both the browser and NodeJS. </p>



<p>You might have heard unpleasant things about webpack and in fact, its configuration can get really <em>spaghetti</em> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92a.png" alt="🤪" class="wp-smiley" style="height: 1em; max-height: 1em;" /> There are simpler alternatives out there, but let&#8217;s start with what&#8217;s the most common tool used in web development today. </p>



<h3 class="wp-block-heading">Adding webpack to a JavaScript app</h3>



<p>Our sample app already has a <code>package.json</code> file (we used JavaScript modules), so we can install webpack with this command:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
npm install --save-dev webpack webpack-cli
</pre></div>


<p>Now we should reorganize the files a bit. Let&#8217;s put all scripts in <code>src</code> folder and move <code>index.html</code> to <code>dist</code> folder. Finally, it looks like that:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="241" height="217" data-attachment-id="4475" data-permalink="https://www.codejourney.net/what-is-a-javascript-bundler/webpack-app-structure/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-structure.png?fit=241%2C217&amp;ssl=1" data-orig-size="241,217" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="webpack-app-structure" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-structure.png?fit=241%2C217&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-structure.png?fit=241%2C217&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-structure.png?resize=241%2C217&#038;ssl=1" alt="" class="wp-image-4475"/></figure>
</div>


<p></p>



<p>Using the folder named <code>dist</code> is very common. It stands for <em>distributable</em>, which means the code ready to be distributed = served to the public. This is where our bundles will be located. As the <code>index.html</code> is directly served to the public, we also place it there.</p>



<p>With such a folders structure, there&#8217;s nothing more we need to configure for webpack to work. By default, webpack looks for <code>src/index.js</code> file which we already have and outputs bundles into the <code>dist</code> folder. To use more advanced features or multiple entry points, a <a href="https://dev.to/typescripttv/6-ways-to-configure-webpack-5a33">configuration file should be added</a>.</p>



<p>In our case, let&#8217;s go with the zero-config approach. The only thing we need is to add a <code>script</code> entry to <code>package.json</code>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-javascript code-embed-pre line-numbers"  data-line="2" data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">  &quot;scripts&quot;: {<br/>    &quot;build&quot;: &quot;webpack&quot;,<br/>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br/>  },</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">package.json with webpack build script</span> </div> </div></div>



<p>Let&#8217;s now run <code>npm run build</code> and see what happens:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="483" height="154" data-attachment-id="4476" data-permalink="https://www.codejourney.net/what-is-a-javascript-bundler/webpack-build-output/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-build-output.png?fit=483%2C154&amp;ssl=1" data-orig-size="483,154" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="webpack-build-output" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-build-output.png?fit=483%2C154&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-build-output.png?fit=483%2C154&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-build-output.png?resize=483%2C154&#038;ssl=1" alt="" class="wp-image-4476"/></figure>
</div>


<p></p>



<p>It seems that webpack created the <code>main.js</code> asset (bundle):</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="351" height="245" data-attachment-id="4477" data-permalink="https://www.codejourney.net/what-is-a-javascript-bundler/webpack-main-bundle/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-main-bundle.png?fit=351%2C245&amp;ssl=1" data-orig-size="351,245" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="webpack-main-bundle" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-main-bundle.png?fit=351%2C245&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-main-bundle.png?fit=351%2C245&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-main-bundle.png?resize=351%2C245&#038;ssl=1" alt="" class="wp-image-4477"/></figure>
</div>


<p></p>



<p>Let&#8217;s make sure that our <code>index.html</code> page links this bundle file properly:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-markup code-embed-pre line-numbers"  data-line="4" data-start="1" data-line-offset="0"><code class="language-markup code-embed-code">&lt;!DOCTYPE html&gt;<br/>&lt;html lang=&quot;en&quot;&gt;<br/>  &lt;head&gt;<br/>    &lt;script type=&quot;module&quot; src=&quot;main.js&quot;&gt;&lt;/script&gt;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">index.html &#8211; linking main.js bundle</span> </div> </div></div>



<p>Now, let&#8217;s open our <code>index.html</code> page and see how the network requests look like now:</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1138" height="283" data-attachment-id="4478" data-permalink="https://www.codejourney.net/what-is-a-javascript-bundler/webpack-app-bundle-fetching-network/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-bundle-fetching-network.png?fit=1138%2C283&amp;ssl=1" data-orig-size="1138,283" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="webpack-app-bundle-fetching-network" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-bundle-fetching-network.png?fit=1138%2C283&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-bundle-fetching-network.png?fit=1138%2C283&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-bundle-fetching-network.png?resize=1138%2C283&#038;ssl=1" alt="" class="wp-image-4478" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-bundle-fetching-network.png?w=1138&amp;ssl=1 1138w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-bundle-fetching-network.png?resize=768%2C191&amp;ssl=1 768w" sizes="auto, (max-width: 1138px) 100vw, 1138px" /></figure>



<p></p>



<p>Wow, that&#8217;s something different from what we had previously! Notice that now only 1 script, the <code>main.js</code> file, is fetched by the webpage. There were 3 before: <code>index.js</code>, <code>my-utils.js</code> and <code>navigation.js</code>. Of course, the web page works exactly as before.</p>



<p>Let us also examine the contents of the fetched <code>main.js</code> file:</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="942" height="248" data-attachment-id="4479" data-permalink="https://www.codejourney.net/what-is-a-javascript-bundler/webpack-app-main-bundle-content/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-main-bundle-content.png?fit=942%2C248&amp;ssl=1" data-orig-size="942,248" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="webpack-app-main-bundle-content" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-main-bundle-content.png?fit=942%2C248&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-main-bundle-content.png?fit=942%2C248&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-main-bundle-content.png?resize=942%2C248&#038;ssl=1" alt="" class="wp-image-4479" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-main-bundle-content.png?w=942&amp;ssl=1 942w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/08/webpack-app-main-bundle-content.png?resize=768%2C202&amp;ssl=1 768w" sizes="auto, (max-width: 942px) 100vw, 942px" /></figure>



<p></p>



<p>Pay attention what happened here. Webpack has nicely inlined all the external code we used directly into the <code>main.js</code> bundle. What&#8217;s more, the exported, but not used functions from <code>my-utils.js</code> are not present here at all! When building the dependency graph, webpack noticed that those functions are not used, so it didn&#8217;t include them<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>This is basically how webpack and most JavaScript bundlers work. Their role is to deliver the smallest and the most optimized bundles possible. This ultimately saves a lot of bandwidth by avoiding unnecessary network requests the client browser would otherwise need to make. Cutting out the unused code makes the bundles smaller, again reducing the network usage.</p>



<p>You can find full source code of the webpack app <a href="https://github.com/dsibinski/codejourney/tree/main/javascript-bundlers/webpack-app">here</a>.</p>



<h3 class="wp-block-heading">Webpack has a lot more to offer</h3>



<p>From all the JavaScript bundlers used today, webpack is the most advanced one. It&#8217;s the most mature one as well. It offers dozens of customizations, <a href="https://webpack.js.org/concepts/#plugins">plugins</a> and <a href="https://webpack.js.org/concepts/#loaders">loaders</a>. You can also easily add <a href="https://www.codejourney.net/is-it-worth-migrating-to-typescript/">TypeScript</a> support. </p>



<p>You can do really crazy stuff with webpack <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> However, if you are starting your web development journey, don&#8217;t get overwhelmed by learning it all. Just be aware that webpack can do almost everything you might need. It&#8217;s just a matter of <a href="https://stackoverflow.com/questions/tagged/webpack">searching StackOverflow</a> or asking <a href="https://github.com/features/copilot">Copilot</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Alternatives to webpack</h3>



<p>In recent years, developers created a few other amazing JavaScript bundlers. I recommend you to pay attention to them, because webpack doesn&#8217;t have to be your first choice today. Webpack is still <a href="https://create-react-app.dev/">used in CRA</a>, which is believed to be a default way of creating new React applications. This is what makes it very widely used, but you don&#8217;t need to follow this path <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>I recommend checking <a href="https://parceljs.org/">Parcel</a>, <a href="https://esbuild.github.io/">esbuild</a> and <a href="https://vitejs.dev/">ViteJS</a>. These are modern bundlers created to, among others, eliminate some of webpack&#8217;s burdens. If you&#8217;re starting out and don&#8217;t want to get yourself busy with bundling configuration, I especially recommend creating your next app with Parcel.</p>



<h2 class="wp-block-heading">Summary</h2>



<p>That&#8217;s all that you need to know about JavaScript bundlers to be able to work with them and understand what&#8217;s happening when executing this magic <code>npm run build</code> script <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>If you&#8217;re a .NET developer, and you enjoyed this article, I think you may also find my free guide useful:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://mailchi.mp/055b94b02391/net-full-stack-web-developer-guide" target="_blank" rel=" noreferrer noopener"><img data-recalc-dims="1" loading="lazy" decoding="async" width="300" height="300" data-attachment-id="4950" data-permalink="https://www.codejourney.net/how-to-become-dotnet-full-stack-in-2025-300-300/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?fit=300%2C300&amp;ssl=1" data-orig-size="300,300" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;1&quot;}" data-image-title="How-to-become-dotnet-full-stack-in-2025-300-300" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?fit=300%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?fit=300%2C300&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?resize=300%2C300&#038;ssl=1" alt="16 Steps to become .NET full stack developer in 2025 - download a free guide" class="wp-image-4950" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?w=300&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2025/01/How-to-become-dotnet-full-stack-in-2025-300-300.jpg?resize=50%2C50&amp;ssl=1 50w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></figure>
</div>


<p></p>
<p>The post <a href="https://www.codejourney.net/what-is-a-javascript-bundler/">What Is A JavaScript Bundler?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/what-is-a-javascript-bundler/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4453</post-id>	</item>
		<item>
		<title>How (And Why?) To Wrap External Libraries?</title>
		<link>https://www.codejourney.net/how-and-why-to-wrap-external-libraries/</link>
					<comments>https://www.codejourney.net/how-and-why-to-wrap-external-libraries/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 20 Feb 2023 09:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[road-to-dotnet-full-stack]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4346</guid>

					<description><![CDATA[<p>If you use external libraries in your application, wrapping them may be very helpful. How to wrap external libraries and why it&#8217;s worth doing that? Today we&#8217;re going to dive into that, based on a TypeScript web app example 😉 Why? You probably know what a&#160;wrapper is. As its name suggests, it&#8217;s a practice of&#8230;</p>
<p>The post <a href="https://www.codejourney.net/how-and-why-to-wrap-external-libraries/">How (And Why?) To Wrap External Libraries?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you use external libraries in your application, wrapping them may be very helpful. How to wrap external libraries and why it&#8217;s worth doing that? Today we&#8217;re going to dive into that, based on a TypeScript web app example <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4346"></span>



<h2 class="wp-block-heading">Why?</h2>



<p>You probably know what a&nbsp;<em>wrapper</em> is. As its name suggests, it&#8217;s a practice of putting another layer on a piece of something. In our case, <em>wrapping</em>&nbsp;a piece of code in another piece of code <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>But why would you do that? To make your life easier! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4aa.png" alt="💪" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br>Wrapping external libraries lets you abstract your code from their implementation details. In effect, it makes your life a lot easier when you want to keep the behavior, but change the library providing it. This approach also lets you use only those features from a given external dependency that you actually need.<br>Let&#8217;s see that with an example.</p>



<h2 class="wp-block-heading">Wrapping HTTP client</h2>



<p>A very good example is HTTP client&nbsp;wrapper. HTTP calls are used in almost every web application. In order to perform them, we need to choose an HTTP client. We can either use <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch">fetch</a></code>, or something more sophisticated like&nbsp;<code><a href="https://axios-http.com/docs/intro">axios</a></code>.</p>



<p>However, with time, we may decide to replace it with something else. There might be many reasons for that &#8211; either the library stops to be maintained or something new and better is out there. It would be a shame if we&#8217;d now need to change the code in those thousands of places where the current library is being used. This would take a lot of time and might be error-prone. We can definitely prepare better for such cases <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Create HttpClient wrapper for axios</h3>



<p>Let&#8217;s say that, for now, we will go with <code>axios</code>. Instead of <a href="https://axios-http.com/docs/example">calling it directly from our code</a>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="2,10-11" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">import { useEffect, useState } from &quot;react&quot;;<br/>import axios from &quot;axios&quot;;<br/>import { Product } from &quot;../types/product&quot;;<br/>import { DummyJsonProductsResult } from &quot;../types/dummyJsonProductsResult&quot;;<br/><br/>export const ProductsList = () =&gt; {<br/>  const [products, setProducts] = useState&lt;Product[] | null&gt;(null);<br/><br/>  useEffect(() =&gt; {<br/>    axios<br/>      .get&lt;DummyJsonProductsResult&gt;(&quot;https://dummyjson.com/products&quot;)<br/>      .then((result) =&gt; {<br/>        setProducts(result.data.products);<br/>      });<br/>  }, []);<br/> <br/> // ...<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">ProductsList.tsx &#8211; calling axios directly from component&#039;s code</span> </div> </div></div>



<p>I will create an <em>HttpClient</em> wrapper for it and use it instead.</p>



<p>First, I create <code>httpClient.ts</code> file in <code>wrappers</code> folder. I like to have such a catalog in my React projects and keep all the wrappers there.</p>



<p>I start writing all wrappers with an interface. In that case, I treat the interface as a <em>contract</em>. It should say what I need this small wrapper to do, without worrying about implementation details.</p>



<p><code>IHttpClient</code> interface initially looks as follows:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">interface IHttpClient {<br/>  get&lt;TResponse&gt;(url: string): Promise&lt;TResponse&gt;;<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; IHttpClient interface (get only)</span> </div> </div></div>



<p>That&#8217;s what we have so far. We just need to retrieve the data with <code>GET</code> method.</p>



<p>Next step is to create the actual implementation of <code>IHttpClient</code> using <code>axios</code>. This is pretty straightforward using a <code>class</code> implementing <code>IHttpClient</code> interface and taking a look at <a href="https://axios-http.com/docs/instance"><code>axios</code>&#8216;s documentation</a>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">class AxiosHttpClient implements IHttpClient {<br/>  private instance: AxiosInstance | null = null;<br/><br/>  private get axiosClient(): AxiosInstance {<br/>    return this.instance ?? this.initAxiosClient();<br/>  }<br/><br/>  private initAxiosClient() {<br/>    return axios.create();<br/>  }<br/><br/>  get&lt;TResponse&gt;(url: string): Promise&lt;TResponse&gt; {<br/>    return new Promise&lt;TResponse&gt;((resolve, reject) =&gt; {<br/>      this.axiosClient<br/>        .get&lt;TResponse, AxiosResponse&lt;TResponse&gt;&gt;(url)<br/>        .then((result) =&gt; {<br/>          resolve(result.data);<br/>        })<br/>        .catch((error: Error | AxiosError) =&gt; {<br/>          reject(error);<br/>        });<br/>    });<br/>  }<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; AxiosHttpClient implementation (get only)</span> </div> </div></div>



<p>This implementation lets us encapsulate a simple singleton inside the class.</p>



<p>The last step here is to expose the instance of our HTTP client. Remember to <strong>always export the interface type variable</strong>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const httpClient: IHttpClient = new AxiosHttpClient();</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; exporting AxiosHttpClient as IHttpClient singleton instance</span> </div> </div></div>



<p>That&#8217;s basically how we wrap external libraries in TypeScript. Easy-peasy <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Using the wrapper</h3>



<p>I can now use our wrapper in <code>ProductsList.tsx</code> component:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="2,10-11" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">import { useEffect, useState } from &quot;react&quot;;<br/>import { httpClient } from &quot;../wrappers/httpClient&quot;; // we don&#039;t import axios here anymore<br/>import { Product } from &quot;../types/product&quot;;<br/>import { DummyJsonProductsResult } from &quot;../types/dummyJsonProductsResult&quot;;<br/><br/>export const ProductsList = () =&gt; {<br/>  const [products, setProducts] = useState&lt;Product[] | null&gt;(null);<br/><br/>  useEffect(() =&gt; {<br/>    httpClient // instead of axios, we use our wrapper in components<br/>      .get&lt;DummyJsonProductsResult&gt;(&quot;https://dummyjson.com/products&quot;)<br/>      .then((result) =&gt; {<br/>        setProducts(result.data.products);<br/>      });<br/>  }, []);<br/> <br/> // ...<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">ProductsList.tsx &#8211; using IHttpClient wrapper</span> </div> </div></div>



<p>Notice how easy that was. Since now, we only import stuff from <code>axios</code> package in <code>httpClient.ts</code> file. Only this single file is dependent on <a href="https://www.npmjs.com/package/axios"><code>axios</code> npm package</a>. None of our components (and other project files) know about <code>axios</code>. Our IDE only knows that the wrapper is an object instance fulfilling <code>IHttpClient</code> contract:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="537" height="332" data-attachment-id="4361" data-permalink="https://www.codejourney.net/how-and-why-to-wrap-external-libraries/1_usingwrapperinterface/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_UsingWrapperInterface.png?fit=537%2C332&amp;ssl=1" data-orig-size="537,332" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1_UsingWrapperInterface" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_UsingWrapperInterface.png?fit=537%2C332&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_UsingWrapperInterface.png?fit=537%2C332&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_UsingWrapperInterface.png?resize=537%2C332&#038;ssl=1" alt="Interface-implementing wrapper instance usage in Visual Studio Code" class="wp-image-4361"/></figure>
</div>


<h3 class="wp-block-heading">Extra wrapper features</h3>



<p>Apart from nicely isolating us from dependencies, wrappers have more advantages. One of them is a possibility to configure the library in a single place. In our example with <code>axios</code> &#8211; imagine that one day you want to add custom headers to each HTTP request. Having all API calls going via <code>AxiosHttpClient</code>, you can configure such things there, in a single place. That way, you follow the <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY principle</a> and keep all the logic related to <code>axios</code> (or to any other external dependency) in a single place. It also comes with benefits like easy testability etc.</p>



<p>For clarity, I also added <code>post</code> support to our <code>IHttpClient</code>. You can <a href="https://github.com/dsibinski/codejourney/commit/200f31ba0c5483f3e834a5ec4cd82566887e3d98">check it here</a>.</p>



<h2 class="wp-block-heading">Replacing the wrapped library</h2>



<p>Ok, it&#8217;s time to have our solution battle-tested. We have the HTTP client nicely wrapped and exposed as an instance of <code>IHttpClient</code>. However, we came to the conclusion that <code>axios</code> is not good enough, and we want to have it replaced with <code>fetch</code>.</p>



<p>Remember that in the real web application, you would have hundreds or thousands of usages of <code>IHttpClient</code> instance. That&#8217;s where the power of wrappers comes into play <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>So how do I make sure those thousands of usages will now use <code>fetch</code> instead of <code>axios</code>? That&#8217;s actually pretty straightforward. I&#8217;ll simply add a new class &#8211; <code>FetchHttpClient</code> implementing <code>IHttpClient</code> interface:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="4,23" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">class FetchHttpClient implements IHttpClient {<br/>  get&lt;TResponse&gt;(url: string): Promise&lt;TResponse&gt; {<br/>    return new Promise&lt;TResponse&gt;((resolve, reject) =&gt; {<br/>      fetch(url)<br/>        .then((response) =&gt;<br/>          response<br/>            .json()<br/>            .then((responseJson) =&gt; {<br/>              resolve(responseJson as TResponse);<br/>            })<br/>            .catch((error: Error) =&gt; {<br/>              reject(`Response JSON parsing error: ${error}`);<br/>            })<br/>        )<br/>        .catch((error: Error) =&gt; {<br/>          reject(error);<br/>        });<br/>    });<br/>  }<br/><br/>  post&lt;TResponse&gt;(url: string, data?: object): Promise&lt;TResponse&gt; {<br/>    return new Promise&lt;TResponse&gt;((resolve, reject) =&gt; {<br/>      fetch(url, {<br/>        method: &quot;POST&quot;,<br/>        headers: {<br/>          &quot;Content-Type&quot;: &quot;application/json&quot;,<br/>        },<br/>        body: JSON.stringify(data),<br/>      })<br/>        .then((response) =&gt;<br/>          response<br/>            .json()<br/>            .then((responseJson) =&gt; {<br/>              resolve(responseJson as TResponse);<br/>            })<br/>            .catch((error: Error) =&gt; {<br/>              reject(`Response JSON parsing error: ${error}`);<br/>            })<br/>        )<br/>        .catch((error: Error) =&gt; {<br/>          reject(error);<br/>        });<br/>    });<br/>  }<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; new FetchHttpClient using fetch for get and post</span> </div> </div></div>



<p>For completion, I included <code>POST</code> here as well.</p>



<p>The one last thing I have to do to make our new <code>FetchHttpClient</code> be used in the whole app in place of <code>AxiosHttpClient</code> is to change a single line with export:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const httpClient: IHttpClient = new FetchHttpClient(); // instead of new AxiosHttpClient()</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; AxiosHttpClient replaced with FetchHttpClient</span> </div> </div></div>



<p>and that&#8217;s it! Our whole application now uses <code>fetch</code> for <code>GET</code> and <code>POST</code> HTTP requests <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> And it even still works <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f605.png" alt="😅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Summary and source code</h2>



<p>I hope that now you see how important it is to wrap external libraries. We have seen that on JavaScript/TypeScript app example, but this is applicable to any programming language and framework. </p>



<p>It&#8217;s always good to be as independent as possible of 3rd party stuff. Too many times I&#8217;ve been in a situation that some <code>npm</code> package is so extensively used in a project, directly in the source code in hundreds of places, that it cannot be replaced without spending several days on it. Creating wrappers forces us to think abstract, which is another great advantage.</p>



<p>You can find the complete source code here: <a href="https://github.com/dsibinski/codejourney/tree/main/wrapping-external-libraries">https://github.com/dsibinski/codejourney/tree/main/wrapping-external-libraries</a></p>


<script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.9 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-1" class="mc4wp-form mc4wp-form-2612" method="post" data-id="2612" data-name="Download a free guide form" ><div class="mc4wp-form-fields"><table bgcolor="#f2f6f5"><tr><td> <p><p>
    <label>
<h1 style="">
  <center>GET A FREE GUIDE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f381.png" alt="🎁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></center>
      </h1>        
      <h2 style="font-family: Helvetica">
        <center>16 STEPS TO BECOME <br/>.NET FULL STACK WEB DEVELOPER </br>IN 2025</center>
      </h2>
</p>
  <center><div>
	<input type="email" name="EMAIL" placeholder="Email address" required />
    <p>
    <input type="text" name="FNAME" placeholder="Your name"
    required="">
  </p>
  </div>
    <center>

	<center><input type="submit" value="DOWNLOAD THE FREE GUIDE" style="color: #7b1fa2; font-weight:bold; font-size: 20px" /></center>
<p style="font-size: 12px; font-style: italic;">After you sign up, I may be sending you some emails with additional free content from time to time.
<br/>No spam, only awesome stuff</p>
</p></td></tr></table>

</div><label style="display: none !important;">Leave this field empty if you're human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1767004789" /><input type="hidden" name="_mc4wp_form_id" value="2612" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-1" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin -->
<p>The post <a href="https://www.codejourney.net/how-and-why-to-wrap-external-libraries/">How (And Why?) To Wrap External Libraries?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/how-and-why-to-wrap-external-libraries/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4346</post-id>	</item>
		<item>
		<title>Adding Meatballs Menu To React-Table Rows</title>
		<link>https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/</link>
					<comments>https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 06 Feb 2023 08:16:17 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[react-table]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4302</guid>

					<description><![CDATA[<p>Meatballs menu (⋯), also called three horizontal dots menu, is a great way of providing contextual options for grid rows. In this article, I will show you how to add the meatballs menu to a table built with @tanstack/react-table. After reading this article, you will know how to add such a menu to your React&#8230;</p>
<p>The post <a href="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/">Adding Meatballs Menu To React-Table Rows</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Meatballs menu (⋯), also called <em>three horizontal dots menu</em>, is a great way of providing contextual options for grid rows. In this article, I will show you how to add the meatballs menu to a table built with <a href="https://www.npmjs.com/package/@tanstack/react-table">@tanstack/react-table</a>.</p>



<p>After reading this article, you will know how to add such a menu to your React app. The end result will look as in the highlighted picture of this article <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4302"></span>



<h2 class="wp-block-heading">Creating a table with row selection support</h2>



<p>First, let&#8217;s define a type of data that we want to display. For our example, we will display a list of <code>Car</code>s:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export type Car = {<br/>  id: string;<br/>  brand: string;<br/>  model: string;<br/>  productionYear: number;<br/>  isAvailable: boolean;<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">Car.ts</span> </div> </div></div>



<p>Next, we create a new component called <code>CarsTable</code>, responsible for rendering the table. We will use <a href="https://www.npmjs.com/package/@tanstack/react-table">@tanstack/react-table</a> for the table behavior and <a href="https://www.npmjs.com/package/react-bootstrap">react-bootstrap</a> for UI elements.</p>



<p>I implemented the <code>CarsTable</code> component in <a href="https://tanstack.com/table/v8/docs/examples/react/basic">quite a standard way according to react-table docs</a>, so I won&#8217;t copy-paste it here. You can check the whole component&#8217;s code <a href="https://github.com/dsibinski/react-table-context-menu/blob/faccde72b4be060c9dbca3d59d7b1506bc9f9b01/src/components/CarsTable.tsx">here</a>. What&#8217;s interesting is that I added support for row selection in a way that makes our table a <a href="https://reactjs.org/docs/forms.html#controlled-components">controlled React component</a>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="2-3,10,14,16" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">type CarsTableProps = {<br/>  selectedCar: Car | null;<br/>  onCarSelected: (car: Car) =&gt; void;<br/>};<br/>export const CarsTable = (props: CarsTableProps) =&gt; {<br/>  return (<br/>    // ...<br/>    &lt;tbody&gt;<br/>        {table.getRowModel().rows.map((row) =&gt; {<br/>          const isActive = row.original.id === props.selectedCar?.id;<br/>          return (<br/>            &lt;tr<br/>              key={row.id}<br/>              style={isActive === true ? { backgroundColor: &quot;#3a7a11&quot; } : undefined}<br/>              onClick={() =&gt; {<br/>                props.onCarSelected(row.original);<br/>              }}<br/>            &gt;<br/>              {row.getVisibleCells().map((cell) =&gt; (<br/>                &lt;td key={cell.id}&gt;<br/>                  {flexRender(cell.column.columnDef.cell, cell.getContext())}<br/>                &lt;/td&gt;<br/>              ))}<br/>            &lt;/tr&gt;<br/>          );<br/>        })}<br/>      &lt;/tbody&gt;<br/>    // ...<br/>  )<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarsTable.tsx &#8211; controlled row selection</span> </div> </div></div>



<p>As you can see, <code>selectedCar</code> and <code>onCarSelected</code> are managed from outside. Line 14 shows how the row color gets changed for the currently selected car. I recently had to deal with such a case in one of my projects.</p>



<p>So far, so good. This is how it looks, populated with sample data:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="991" height="440" data-attachment-id="4312" data-permalink="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/1_initialtable/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?fit=991%2C440&amp;ssl=1" data-orig-size="991,440" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1_InitialTable" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?fit=991%2C440&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?fit=991%2C440&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?resize=991%2C440&#038;ssl=1" alt="react-table table with row selection support" class="wp-image-4312" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?w=991&amp;ssl=1 991w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_InitialTable.png?resize=768%2C341&amp;ssl=1 768w" sizes="auto, (max-width: 991px) 100vw, 991px" /></figure>
</div>


<h2 class="wp-block-heading">Adding meatballs menu</h2>



<p>Ok, we have a table. Now we want to add the meatballs menu. We need a three dots icon and a dropdown menu to open on clicking it.</p>



<p>After quickly <a href="https://react-bootstrap.github.io/components/dropdowns/">going through the react-bootstrap docs</a>, let&#8217;s create a new component for that:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">import { Dropdown } from &quot;react-bootstrap&quot;;<br/>import { Car } from &quot;../types/Car&quot;;<br/>import CustomDivToggle from &quot;./CustomDivToggle&quot;;<br/>import { BsThreeDots } from &quot;react-icons/bs&quot;;<br/><br/>export const CarRowContextMenu = ({ carRow }: { carRow: Car }) =&gt; {<br/>  return (<br/>    &lt;Dropdown key={carRow.id}&gt;<br/>      &lt;Dropdown.Toggle as={CustomDivToggle} style={{ cursor: &quot;pointer&quot; }}&gt;<br/>        &lt;BsThreeDots /&gt;<br/>      &lt;/Dropdown.Toggle&gt;<br/>      &lt;Dropdown.Menu&gt;<br/>        &lt;Dropdown.Item&gt;Option 1&lt;/Dropdown.Item&gt;<br/>        &lt;Dropdown.Item&gt;Option 2&lt;/Dropdown.Item&gt;<br/>      &lt;/Dropdown.Menu&gt;<br/>    &lt;/Dropdown&gt;<br/>  );<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarRowContextMenu.tsx</span> </div> </div></div>



<p>As we want our dropdown toggle to have custom style, I had to provide <code>CustomDivToggle</code> as a <a href="https://react-bootstrap.github.io/components/dropdowns/#custom-dropdown-components">custom dropdown component</a>. It&#8217;s nothing very interesting, but you can check its implementation <a href="https://github.com/dsibinski/react-table-context-menu/blob/c98687413a28eecfc5fa4175a4b55a79dee8300f/src/components/CustomDivToggle.tsx">here</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Next, as we&#8217;d like our meatballs menu to be an additional column in the grid, it seems natural to <a href="https://tanstack.com/table/v8/docs/guide/column-defs#column-def-types">use <code>react-table</code>&#8216;s display column</a>. Let&#8217;s try adding it:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">columnHelper.display({<br/>      id: &quot;context-menu&quot;,<br/>      cell: (cellContext) =&gt; {<br/>        const row = cellContext.row.original;<br/>        return &lt;CarRowContextMenu carRow={row} /&gt;;<br/>      },<br/>    }),</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarsTable.tsx &#8211; adding meatballs menu with display-type column</span> </div> </div></div>



<p>It looks we have it:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="984" height="442" data-attachment-id="4316" data-permalink="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/3_contextmenuaddedv1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?fit=984%2C442&amp;ssl=1" data-orig-size="984,442" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3_ContextMenuAddedV1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?fit=984%2C442&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?fit=984%2C442&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?resize=984%2C442&#038;ssl=1" alt="Meatballs menu added to the table with react-tabe's display column" class="wp-image-4316" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?w=984&amp;ssl=1 984w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/3_ContextMenuAddedV1.png?resize=768%2C345&amp;ssl=1 768w" sizes="auto, (max-width: 984px) 100vw, 984px" /></figure>
</div>


<p>However, after clicking through it for a while, it seems we have an issue. The toggle only opens on every 2nd click:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1140" height="498" data-attachment-id="4317" data-permalink="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/4_contextmenudoubleclickissue/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?fit=1153%2C504&amp;ssl=1" data-orig-size="1153,504" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4_ContextMenuDoubleClickIssue" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?fit=1153%2C504&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?fit=1153%2C504&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?resize=1140%2C498&#038;ssl=1" alt="Meatballs menu with react-table's display column. Double-click issue" class="wp-image-4317" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?w=1153&amp;ssl=1 1153w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/4_ContextMenuDoubleClickIssue.gif?resize=768%2C336&amp;ssl=1 768w" sizes="auto, (max-width: 1140px) 100vw, 1140px" /></figure>
</div>


<p>Why is that? The reason is our controlled <code>CarsTable</code> component. On clicking a new row, the change event occurs, which triggers the re-render of the <code>CarsTable</code> component (because <code>selectedCar</code> <em>actually</em> changes). It makes <code>react-table</code> re-render the table, with the meatballs menu in its default state (collapsed). On clicking the menu in the same row again, the change event occurs, but the <code>selectedCar</code> <em>does not actually change</em>, which <em>does not</em> trigger the re-render. Initially, it took me a while to figure that out <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Fixing double-click issue</h2>



<p>In our case, a fix for the double click issue is quite simple. Instead of adding the column with the menu using <code>columnHelper.display()</code> function from <code>react-table</code>, we can render it <em>manually</em>. To do that, we should simply add a new <code>&lt;td&gt;</code> to each row of the table:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="17-19" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">&lt;tbody&gt;<br/>        {table.getRowModel().rows.map((row) =&gt; {<br/>          const isActive = row.original.id === props.selectedCar?.id;<br/>          return (<br/>            &lt;tr<br/>              key={row.id}<br/>              style={isActive === true ? { backgroundColor: &quot;#3a7a11&quot; } : undefined}<br/>              onClick={() =&gt; {<br/>                props.onCarSelected(row.original);<br/>              }}<br/>            &gt;<br/>              {row.getVisibleCells().map((cell) =&gt; (<br/>                &lt;td key={cell.id}&gt;<br/>                  {flexRender(cell.column.columnDef.cell, cell.getContext())}<br/>                &lt;/td&gt;<br/>              ))}<br/>              &lt;td&gt;<br/>                &lt;CarRowContextMenu carRow={row.original} /&gt;<br/>              &lt;/td&gt;<br/>            &lt;/tr&gt;<br/>          );<br/>        })}<br/>&lt;/tbody&gt;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarsTable.tsx &#8211; context menu added as a separate &lt;td&gt;</span> </div> </div></div>



<p>Additionally, to make it work, we need an additional table&#8217;s header placeholder:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="14-15" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">&lt;thead&gt;<br/>        {table.getHeaderGroups().map((headerGroup) =&gt; (<br/>          &lt;tr key={headerGroup.id}&gt;<br/>            {headerGroup.headers.map((header) =&gt; (<br/>              &lt;th key={header.id}&gt;<br/>                {header.isPlaceholder<br/>                  ? null<br/>                  : flexRender(<br/>                      header.column.columnDef.header,<br/>                      header.getContext()<br/>                    )}<br/>              &lt;/th&gt;<br/>            ))}<br/>            {/* placeholder header for context menu */}<br/>            &lt;th&gt;&lt;/th&gt;<br/>          &lt;/tr&gt;<br/>        ))}<br/>&lt;/thead&gt;</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">CarsTable.tsx &#8211; placeholder &lt;th&gt; for context menu</span> </div> </div></div>



<p>That&#8217;s it! Our `react-table` table with row selection support and the meatballs menu works like a charm now:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1140" height="493" data-attachment-id="4322" data-permalink="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/5_contextmenufinalversion-1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?fit=1150%2C497&amp;ssl=1" data-orig-size="1150,497" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="5_ContextMenuFinalVersion-1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?fit=1150%2C497&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?fit=1150%2C497&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?resize=1140%2C493&#038;ssl=1" alt="react-table table with row selection and meatballs menu - final version" class="wp-image-4322" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?w=1150&amp;ssl=1 1150w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/5_ContextMenuFinalVersion-1.gif?resize=768%2C332&amp;ssl=1 768w" sizes="auto, (max-width: 1140px) 100vw, 1140px" /></figure>
</div>


<h2 class="wp-block-heading">Meatballs menu with react-table &#8211; source code</h2>



<p>You can find the complete <a href="https://github.com/dsibinski/react-table-context-menu">source code here</a>. I hope you find it useful! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>


<script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.9 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-2" class="mc4wp-form mc4wp-form-2612" method="post" data-id="2612" data-name="Download a free guide form" ><div class="mc4wp-form-fields"><table bgcolor="#f2f6f5"><tr><td> <p><p>
    <label>
<h1 style="">
  <center>GET A FREE GUIDE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f381.png" alt="🎁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></center>
      </h1>        
      <h2 style="font-family: Helvetica">
        <center>16 STEPS TO BECOME <br/>.NET FULL STACK WEB DEVELOPER </br>IN 2025</center>
      </h2>
</p>
  <center><div>
	<input type="email" name="EMAIL" placeholder="Email address" required />
    <p>
    <input type="text" name="FNAME" placeholder="Your name"
    required="">
  </p>
  </div>
    <center>

	<center><input type="submit" value="DOWNLOAD THE FREE GUIDE" style="color: #7b1fa2; font-weight:bold; font-size: 20px" /></center>
<p style="font-size: 12px; font-style: italic;">After you sign up, I may be sending you some emails with additional free content from time to time.
<br/>No spam, only awesome stuff</p>
</p></td></tr></table>

</div><label style="display: none !important;">Leave this field empty if you're human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1767004789" /><input type="hidden" name="_mc4wp_form_id" value="2612" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-2" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin -->
<p>The post <a href="https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/">Adding Meatballs Menu To React-Table Rows</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/adding-meatballs-menu-to-react-table-rows/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4302</post-id>	</item>
		<item>
		<title>10 VS Code Extensions I Couldn&#8217;t Live Without</title>
		<link>https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/</link>
					<comments>https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Thu, 19 Jan 2023 18:57:07 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[road-to-dotnet-full-stack]]></category>
		<category><![CDATA[typescript]]></category>
		<category><![CDATA[vscode]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4232</guid>

					<description><![CDATA[<p>I love VS Code 😍 There&#8217;s no better web code editor out there for me. Today, I&#8217;m going to share with you the 10 VS Code extensions that make my life easier. I can&#8217;t imagine coding without them. Let&#8217;s dive into it! Prettier My number one. I was a bit skeptical initially, but today I&#8230;</p>
<p>The post <a href="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/">10 VS Code Extensions I Couldn&#8217;t Live Without</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>I love VS Code <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60d.png" alt="😍" class="wp-smiley" style="height: 1em; max-height: 1em;" /> There&#8217;s no better web code editor out there for me. Today, I&#8217;m going to share with you the 10 VS Code extensions that make my life easier. I can&#8217;t imagine coding without them. Let&#8217;s dive into it!</p>



<span id="more-4232"></span>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode">Prettier</a></h2>



<p>My number one. I was a bit skeptical initially, but today I can&#8217;t imagine my life without Prettier <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /> For those unfamiliar with the tool, Prettier is an <em>opinionated</em> code formatted. Opinionated, because it allows for almost no configuration. It reformats your code automatically based on its own rules.</p>



<p>It may sound scary and limiting, but it really frees your mind. Suddenly you forget about all those <em>spaces vs tabs wars</em> and discussions on which column shall the code be wrapped. Prettier does that all automatically. It can even be done on saving the file! Just take a look, how beautiful that is:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="917" height="637" data-attachment-id="4236" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/1_vscode_prettier/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/1_VSCode_Prettier.gif?fit=917%2C637&amp;ssl=1" data-orig-size="917,637" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1_VSCode_Prettier" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/1_VSCode_Prettier.gif?fit=300%2C208&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/1_VSCode_Prettier.gif?fit=917%2C637&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/1_VSCode_Prettier.gif?resize=917%2C637&#038;ssl=1" alt="Gif presenting Prettier auto-formatting on saving the file in VS Code" class="wp-image-4236"/></figure>
</div>


<p></p>



<p>It is also very easy to <a href="https://prettier.io/docs/en/precommit.html">execute prettier formatting with pre-commit git hook</a>, so the checked-in code is always formatted, even if some team members don&#8217;t use it in their IDE.</p>



<p>Never again without Prettier! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=dsznajder.es7-react-js-snippets">ES7+ React/Redux/React-Native snippets</a></h2>



<p>This is one of the first VS Code extensions I installed. It&#8217;s simple, yet very powerful! This plugin adds multiple JavaScript/React snippets. I create many new components every day. Snippets make it very easy:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="932" height="487" data-attachment-id="4239" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/2_vscode_react_snippets/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/2_VSCode_React_snippets.gif?fit=932%2C487&amp;ssl=1" data-orig-size="932,487" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2_VSCode_React_snippets" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/2_VSCode_React_snippets.gif?fit=300%2C157&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/2_VSCode_React_snippets.gif?fit=932%2C487&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/2_VSCode_React_snippets.gif?resize=932%2C487&#038;ssl=1" alt="One of the VS Code extensions that adds React snipets" class="wp-image-4239"/></figure>
</div>


<p></p>



<p>I recommend trying this extension out and exploring its built-in snippets.</p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=adrianwilczynski.csharp-to-typescript">C# to TypeScript</a></h2>



<p>This one is a time-saver for .NET developers working with TypeScript. It allows generating TypeScript interfaces from C# classes. You can simply copy a C# <code>class</code> and paste is as a TypeScript&#8217;s <code>interface</code> in Visual Studio Code:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1005" height="431" data-attachment-id="4241" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/3_vscode_csharp_to_typescript-1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/3_VSCode_CSharp_to_TypeScript-1.gif?fit=1005%2C431&amp;ssl=1" data-orig-size="1005,431" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3_VSCode_CSharp_to_TypeScript-1" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/3_VSCode_CSharp_to_TypeScript-1.gif?fit=300%2C129&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/3_VSCode_CSharp_to_TypeScript-1.gif?fit=1005%2C431&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/3_VSCode_CSharp_to_TypeScript-1.gif?resize=1005%2C431&#038;ssl=1" alt="" class="wp-image-4241"/></figure>
</div>


<p></p>



<p>You can also paste using keyboard shortcuts. There&#8217;s even <a href="https://www.nuget.org/packages/CSharpToTypeScript.CLITool/">a CLI tool</a> available in case you&#8217;d want to automate this process. This extension doesn&#8217;t support all scenarios (especially no support for <code>records</code> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f62d.png" alt="😭" class="wp-smiley" style="height: 1em; max-height: 1em;" />), but it still covers most simple copy-paste cases. By the way, synchronizing C# with TypeScript types is one of the challenges for full stack .NET developers.</p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=mattpocock.ts-error-translator">Total TypeScript</a></h2>



<p>From all VS Code extensions I use, this is the only one that actually helps me learn on the job. It was called <em>TypeScript Error Translator</em> before, and this is what I initially used it for. It basically provides more meaningful, crowdsourced translations for often enigmatic TypeScript errors:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="821" height="280" data-attachment-id="4243" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/4_vscode_errorstranslator/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?fit=821%2C280&amp;ssl=1" data-orig-size="821,280" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4_VSCode_ErrorsTranslator" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?fit=300%2C102&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?fit=821%2C280&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?resize=821%2C280&#038;ssl=1" alt="TypeScript error translator shows nice explanation of TS error" class="wp-image-4243" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?w=821&amp;ssl=1 821w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?resize=300%2C102&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/4_VSCode_ErrorsTranslator.png?resize=768%2C262&amp;ssl=1 768w" sizes="auto, (max-width: 821px) 100vw, 821px" /></figure>
</div>


<p></p>



<p>When a translation is missing, you can always contribute one. Additionally, as its new name suggests, it also lets you learn programming concepts directly inside your code. As soon as it finds a new concept in the code, a brief explanation is shown:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="485" data-attachment-id="4244" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/5_vscode_totaltypescriptlearning/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?fit=2082%2C986&amp;ssl=1" data-orig-size="2082,986" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="5_VSCode_TotalTypescriptLearning" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?fit=300%2C142&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?fit=1024%2C485&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=1024%2C485&#038;ssl=1" alt="" class="wp-image-4244" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=1024%2C485&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=300%2C142&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=768%2C364&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=1536%2C727&amp;ssl=1 1536w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/5_VSCode_TotalTypescriptLearning.png?resize=2048%2C970&amp;ssl=1 2048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p></p>



<p>It will stop highlighting this particular type of concept as soon as you mark it as learned. I think it&#8217;s a quite interesting way of learning on the job <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">ESLint</a></h2>



<p>This is the extension for integrating the most well-known linter for JavaScript into Visual Studio Code. <a href="https://eslint.org/">ESLint</a> is a static analyzer of your source code which helps you find problems quickly. It will highlight issues in your code for all loaded files from your workspace. I don&#8217;t feel there&#8217;s much to write about here &#8211; just <a href="https://eslint.org/">check their website</a> and give this extension a try <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client">Thunder Client</a></h2>



<p>Thunder Client is like <a href="https://www.postman.com/">Postman</a>, but built into VS Code. It lets you easily play with HTTP APIs. You can create your own collections of requests and export them to JSON, which can be checked into the source control.</p>



<p>Yet another very useful extension! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f44d.png" alt="👍" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=zenclabs.previewjs">Preview.js</a></h2>



<p>This is one of my recent discoveries. Preview.js allows to live-preview React, Solid or Vue components directly in VS Code. It also generates the initial props data, sometimes even for complex objects:</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1140" height="593" data-attachment-id="4261" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/6_vscode_previewjs-2/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?fit=1467%2C763&amp;ssl=1" data-orig-size="1467,763" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="6_VSCode_PreviewJS-2" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?fit=1467%2C763&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?fit=1467%2C763&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?resize=1140%2C593&#038;ssl=1" alt="" class="wp-image-4261" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?w=1467&amp;ssl=1 1467w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/6_VSCode_PreviewJS-2.gif?resize=768%2C399&amp;ssl=1 768w" sizes="auto, (max-width: 1140px) 100vw, 1140px" /></figure>



<p></p>



<p>The plugin is also configurable for custom scenarios like external dependencies. You can find more information <a href="https://previewjs.com/docs/config">in the documentation</a>.</p>


<script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.9 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-3" class="mc4wp-form mc4wp-form-2612" method="post" data-id="2612" data-name="Download a free guide form" ><div class="mc4wp-form-fields"><table bgcolor="#f2f6f5"><tr><td> <p><p>
    <label>
<h1 style="">
  <center>GET A FREE GUIDE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f381.png" alt="🎁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></center>
      </h1>        
      <h2 style="font-family: Helvetica">
        <center>16 STEPS TO BECOME <br/>.NET FULL STACK WEB DEVELOPER </br>IN 2025</center>
      </h2>
</p>
  <center><div>
	<input type="email" name="EMAIL" placeholder="Email address" required />
    <p>
    <input type="text" name="FNAME" placeholder="Your name"
    required="">
  </p>
  </div>
    <center>

	<center><input type="submit" value="DOWNLOAD THE FREE GUIDE" style="color: #7b1fa2; font-weight:bold; font-size: 20px" /></center>
<p style="font-size: 12px; font-style: italic;">After you sign up, I may be sending you some emails with additional free content from time to time.
<br/>No spam, only awesome stuff</p>
</p></td></tr></table>

</div><label style="display: none !important;">Leave this field empty if you're human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1767004789" /><input type="hidden" name="_mc4wp_form_id" value="2612" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-3" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin -->



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=WakaTime.vscode-wakatime">WakaTime</a></h2>



<p>If you like analytics, you should check this VS Code extension. WakaTime measures how much time you spend actually writing code. It shows exactly in which language you programmed for how much time. It also divides time entries into projects you worked on:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1140" height="1018" data-attachment-id="4264" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/7_wakatime_samplestats/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?fit=2062%2C1842&amp;ssl=1" data-orig-size="2062,1842" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="7_WakaTime_SampleStats" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?fit=2062%2C1842&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?fit=2062%2C1842&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?resize=1140%2C1018&#038;ssl=1" alt="" class="wp-image-4264" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?w=2062&amp;ssl=1 2062w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?resize=768%2C686&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?resize=1536%2C1372&amp;ssl=1 1536w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/7_WakaTime_SampleStats.png?resize=2048%2C1829&amp;ssl=1 2048w" sizes="auto, (max-width: 1140px) 100vw, 1140px" /></figure>
</div>


<p></p>



<p>It&#8217;s interesting to get such insights &#8220;for free&#8221; and see how little time we spend <em>actually</em> coding <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" />Try it for yourself and let me know your results!</p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=zhuangtongfa.Material-theme">One Dark Pro</a></h2>



<p>Who never started a new project by installing fancy tools, text editors or choosing the best color theme for your IDE? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f604.png" alt="😄" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Of course &#8211; good colors = smooth -and pleasant &#8211; coding <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /> One Dark Pro is a must-have theme for my Visual Studio Code. I love it, it makes source code much more readable for me. What&#8217;s your favorite color theme for VS Code? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading"><a href="https://marketplace.visualstudio.com/items?itemName=vscode-icons-team.vscode-icons">vscode-icons</a></h2>



<p>Last but not least, a good friend to the One Dard Pro theme, legendary VS Code icons set. It has over 13M installs! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92f.png" alt="🤯" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Apart from getting +100 to fanciness <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" />, it makes icons more explicit in the explorer:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="599" height="612" data-attachment-id="4265" data-permalink="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/8_vscodeicons/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?fit=599%2C612&amp;ssl=1" data-orig-size="599,612" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="8_VSCodeIcons" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?fit=599%2C612&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?fit=599%2C612&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?resize=599%2C612&#038;ssl=1" alt="" class="wp-image-4265" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?w=599&amp;ssl=1 599w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/01/8_VSCodeIcons.png?resize=50%2C50&amp;ssl=1 50w" sizes="auto, (max-width: 599px) 100vw, 599px" /></figure>
</div>


<p></p>



<p>As you can see, it even guesses the folder type based on its name &#8211; see <code>types</code> or <code>utils</code> folders&#8217; icons above.</p>



<p>Not critical, yet helpful. Undeniably adds a million to your professionalism as a web developer <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Summary</h2>



<p>So, that&#8217;s it! The list of my 10 favorite Visual Studio Code extensions. </p>



<p>What about you? What are your must-have VS Code extensions? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/">10 VS Code Extensions I Couldn&#8217;t Live Without</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/10-vs-code-extensions-i-couldnt-live-without/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4232</post-id>	</item>
		<item>
		<title>How To Fix Visual Studio Code IntelliSense Loading Infinitely</title>
		<link>https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/</link>
					<comments>https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sun, 09 Oct 2022 08:57:31 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[intellisense]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[typescript]]></category>
		<category><![CDATA[visual studio code]]></category>
		<category><![CDATA[vscode]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4188</guid>

					<description><![CDATA[<p>Continuing with weird errors you might encounter in JavaScript world, I have another one: Visual Studio Code IntelliSense loading infinitely 😀 Solution included, of course! The symptoms of this issue are putting your mouse on something where you&#8217;d expect the IntelliSense guidelines, but instead you only see the &#8220;Loading&#8230;&#8221; text. In this short article, I&#8217;m&#8230;</p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/">How To Fix Visual Studio Code IntelliSense Loading Infinitely</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Continuing with <a href="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/">weird errors you might encounter in JavaScript world</a>, I have another one: <strong>Visual Studio Code IntelliSense loading infinitely</strong> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Solution included, of course!</p>



<p>The symptoms of this issue are putting your mouse on something where you&#8217;d expect the IntelliSense guidelines, but instead you only see the &#8220;Loading&#8230;&#8221; text.</p>



<p>In this short article, I&#8217;m sharing the reason of this issue and my way of fixing it.</p>



<span id="more-4188"></span>



<h2 class="wp-block-heading">How VS Code Infinite IntelliSense Loading Looks Like</h2>



<p>This is what I struggled with:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="4195" data-permalink="https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/2_vscodeloadingissue/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/2_VSCodeLoadingIssue.gif?fit=497%2C907&amp;ssl=1" data-orig-size="497,907" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2_VSCodeLoadingIssue" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/2_VSCodeLoadingIssue.gif?fit=164%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/2_VSCodeLoadingIssue.gif?fit=497%2C907&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/2_VSCodeLoadingIssue.gif?resize=497%2C907&#038;ssl=1" alt="Visual Studio Code IntelliSense Loading Infinitely - a gif animation" class="wp-image-4195" style="width:497px;height:907px" width="497" height="907"/></figure>
</div>


<p>As you can see, I didn&#8217;t get any IntelliSense while hovering my mouse on various elements in the editor.</p>



<p>A similar thing happened when I tried to use <code>Go to References</code> (or <code>Shift</code> + <code>F12</code> on Windows) for any object:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="509" height="907" data-attachment-id="4197" data-permalink="https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/3_vscode_find_references_loading/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/3_VSCode_find_references_loading.gif?fit=509%2C907&amp;ssl=1" data-orig-size="509,907" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3_VSCode_find_references_loading" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/3_VSCode_find_references_loading.gif?fit=168%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/3_VSCode_find_references_loading.gif?fit=509%2C907&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/3_VSCode_find_references_loading.gif?resize=509%2C907&#038;ssl=1" alt="Visual Studio Code IntelliSense Loading Infinitely for Go to References - a gif animation" class="wp-image-4197"/></figure>
</div>


<p>As you can see, the loading bar at the top of VS Code was showing progress infinitely.</p>



<p>Let&#8217;s now see how to fix this annoying issue <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Fixing Visual Studio Code IntelliSense Loading Infinitely</h2>



<p>After quite long investigation, I finally found the reason. It turned out that <strong><a href="https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-newer-typescript-versions">sometimes your Visual Studio Code might be using a different TypeScript version than your workspace uses</a></strong>. In my case, it was exactly the cause of the infinite loading problem <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9d0.png" alt="🧐" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>In order to fix it, use <code>TypeScript: Select TypeScript Version...</code> command. You can access commands with <code>Ctrl + Shift + P</code> on Windows or <code>⇧⌘P</code> on Mac:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="600" height="262" data-attachment-id="4199" data-permalink="https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/5_selecttypescriptversion/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/5_SelectTypeScriptVersion.png?fit=600%2C262&amp;ssl=1" data-orig-size="600,262" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="5_SelectTypeScriptVersion" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/5_SelectTypeScriptVersion.png?fit=300%2C131&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/5_SelectTypeScriptVersion.png?fit=600%2C262&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/5_SelectTypeScriptVersion.png?resize=600%2C262&#038;ssl=1" alt="Visual Studio Code: command palette view, &quot;TypeScript: Select TypeScript Version&quot; command" class="wp-image-4199" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/5_SelectTypeScriptVersion.png?w=600&amp;ssl=1 600w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/5_SelectTypeScriptVersion.png?resize=300%2C131&amp;ssl=1 300w" sizes="auto, (max-width: 600px) 100vw, 600px" /></figure>
</div>


<p>Select this command and tell Visual Studio Code to use the workspace version of TypeScript:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="627" height="139" data-attachment-id="4200" data-permalink="https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/6_selecttypescriptversion_useworkspaceversion/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/6_SelectTypeScriptVersion_UseWorkspaceVersion.png?fit=627%2C139&amp;ssl=1" data-orig-size="627,139" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="6_SelectTypeScriptVersion_UseWorkspaceVersion" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/6_SelectTypeScriptVersion_UseWorkspaceVersion.png?fit=300%2C67&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/6_SelectTypeScriptVersion_UseWorkspaceVersion.png?fit=627%2C139&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/6_SelectTypeScriptVersion_UseWorkspaceVersion.png?resize=627%2C139&#038;ssl=1" alt="Visual Studio Code: command palette view, &quot;TypeScript: Select TypeScript Version&quot; command, telling VS Code to use workspace version of TypeScript" class="wp-image-4200" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/6_SelectTypeScriptVersion_UseWorkspaceVersion.png?w=627&amp;ssl=1 627w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/10/6_SelectTypeScriptVersion_UseWorkspaceVersion.png?resize=300%2C67&amp;ssl=1 300w" sizes="auto, (max-width: 627px) 100vw, 627px" /></figure>
</div>


<p>As you can see on the screenshot above, in my case the VS Code&#8217;s TypeScript version was <code>4.8.2</code>, while the workspace version was <code>4.8.3</code>. Changing it to use the newer, workspace version solved the problem. Visual Studio Code IntelliSense loading infinitely issue was gone <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f389.png" alt="🎉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>If it doesn&#8217;t work straightaway, you might need restarting your VS Code editor.</p>



<p>I hope that worked for you and saved you some hours of debugging and exploring StackOverflow <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>


<script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.9 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-4" class="mc4wp-form mc4wp-form-2612" method="post" data-id="2612" data-name="Download a free guide form" ><div class="mc4wp-form-fields"><table bgcolor="#f2f6f5"><tr><td> <p><p>
    <label>
<h1 style="">
  <center>GET A FREE GUIDE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f381.png" alt="🎁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></center>
      </h1>        
      <h2 style="font-family: Helvetica">
        <center>16 STEPS TO BECOME <br/>.NET FULL STACK WEB DEVELOPER </br>IN 2025</center>
      </h2>
</p>
  <center><div>
	<input type="email" name="EMAIL" placeholder="Email address" required />
    <p>
    <input type="text" name="FNAME" placeholder="Your name"
    required="">
  </p>
  </div>
    <center>

	<center><input type="submit" value="DOWNLOAD THE FREE GUIDE" style="color: #7b1fa2; font-weight:bold; font-size: 20px" /></center>
<p style="font-size: 12px; font-style: italic;">After you sign up, I may be sending you some emails with additional free content from time to time.
<br/>No spam, only awesome stuff</p>
</p></td></tr></table>

</div><label style="display: none !important;">Leave this field empty if you're human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1767004789" /><input type="hidden" name="_mc4wp_form_id" value="2612" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-4" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin -->
<p>The post <a href="https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/">How To Fix Visual Studio Code IntelliSense Loading Infinitely</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/how-to-fix-visual-studio-code-intellisense-loading-infinitely/feed/</wfw:commentRss>
			<slash:comments>20</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4188</post-id>	</item>
		<item>
		<title>Typing API Responses With Zod</title>
		<link>https://www.codejourney.net/typing-api-responses-with-zod/</link>
					<comments>https://www.codejourney.net/typing-api-responses-with-zod/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 11 Jul 2022 07:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[typescript]]></category>
		<category><![CDATA[zod]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4093</guid>

					<description><![CDATA[<p>Have you ever needed to synchronize types in your frontend app with the backend API? If you ever had an API action defined like that in your controller: and fetched this data using TypeScript in the following way: at some point, you probably also experienced the desynchronization of backend (C#, in our example) and frontend&#8230;</p>
<p>The post <a href="https://www.codejourney.net/typing-api-responses-with-zod/">Typing API Responses With Zod</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Have you ever needed to synchronize types in your frontend app with the backend API?</p>



<p>If you ever had an API action defined like that in your controller:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117275988" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-aspnetcontrollersample-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="AspNetControllerSample.cs content, created by dsibinski on 02:22PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="AspNetControllerSample.cs">
        <tr>
          <td id="file-aspnetcontrollersample-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-aspnetcontrollersample-cs-LC1" class="blob-code blob-code-inner js-file-line">public record UserViewModel(Guid Id, string Name, string LastName, string Login, </td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-aspnetcontrollersample-cs-LC2" class="blob-code blob-code-inner js-file-line">    bool IsActive, int LoyaltyPoints, AddressViewModel? Address = null);</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-aspnetcontrollersample-cs-LC3" class="blob-code blob-code-inner js-file-line">    </td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-aspnetcontrollersample-cs-LC4" class="blob-code blob-code-inner js-file-line">// &#8230;</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-aspnetcontrollersample-cs-LC5" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-aspnetcontrollersample-cs-LC6" class="blob-code blob-code-inner js-file-line">public List&lt;UserViewModel&gt; AllUsers()</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-aspnetcontrollersample-cs-LC7" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-aspnetcontrollersample-cs-LC8" class="blob-code blob-code-inner js-file-line">    var usersViewModels = TestDataGenerator.GetTestUsers();</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-aspnetcontrollersample-cs-LC9" class="blob-code blob-code-inner js-file-line">    return usersViewModels.OrderBy(uvm =&gt; uvm.Name).ToList();</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrollersample-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-aspnetcontrollersample-cs-LC10" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/bf658760106de7269ebf3d0f351c0411/raw/2336445c10cd9b76b7a5b9af3a747eeaddfb75c4/AspNetControllerSample.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/bf658760106de7269ebf3d0f351c0411#file-aspnetcontrollersample-cs" class="Link--inTextBlock">
          AspNetControllerSample.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>and fetched this data using TypeScript in the following way:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276011" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-apifetchsample-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="ApiFetchSample.ts content, created by dsibinski on 02:23PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="ApiFetchSample.ts">
        <tr>
          <td id="file-apifetchsample-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-apifetchsample-ts-LC1" class="blob-code blob-code-inner js-file-line"><span class=pl-en>getAllUsers</span> <span class=pl-c1>=</span> <span class=pl-k>async</span> <span class=pl-kos>(</span><span class=pl-kos>)</span>: <span class=pl-smi>Promise</span><span class=pl-c1>&lt;</span><span class=pl-smi>UserViewModel</span><span class=pl-kos>[</span><span class=pl-kos>]</span><span class=pl-c1>&gt;</span> <span class=pl-c1>=&gt;</span> <span class=pl-kos>{</span></td>
        </tr>
        <tr>
          <td id="file-apifetchsample-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-apifetchsample-ts-LC2" class="blob-code blob-code-inner js-file-line">    <span class=pl-k>const</span> <span class=pl-s1>url</span> <span class=pl-c1>=</span> <span class=pl-s>`<span class=pl-s1><span class=pl-kos>${</span><span class=pl-smi>this</span><span class=pl-kos>.</span><span class=pl-c1>apiEndpoint</span><span class=pl-kos>}</span></span>/AllUsers`</span><span class=pl-kos>;</span></td>
        </tr>
        <tr>
          <td id="file-apifetchsample-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-apifetchsample-ts-LC3" class="blob-code blob-code-inner js-file-line">    <span class=pl-k>const</span> <span class=pl-s1>response</span> <span class=pl-c1>=</span> <span class=pl-k>await</span> <span class=pl-en>fetch</span><span class=pl-kos>(</span><span class=pl-s1>url</span><span class=pl-kos>)</span><span class=pl-kos>;</span></td>
        </tr>
        <tr>
          <td id="file-apifetchsample-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-apifetchsample-ts-LC4" class="blob-code blob-code-inner js-file-line">    <span class=pl-k>const</span> <span class=pl-s1>users</span> <span class=pl-c1>=</span> <span class=pl-kos>(</span><span class=pl-k>await</span> <span class=pl-s1>response</span><span class=pl-kos>.</span><span class=pl-en>json</span><span class=pl-kos>(</span><span class=pl-kos>)</span><span class=pl-kos>)</span> <span class=pl-k>as</span> <span class=pl-smi>UserViewModel</span><span class=pl-kos>[</span><span class=pl-kos>]</span><span class=pl-kos>;</span></td>
        </tr>
        <tr>
          <td id="file-apifetchsample-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-apifetchsample-ts-LC5" class="blob-code blob-code-inner js-file-line">    <span class=pl-k>return</span> <span class=pl-s1>users</span><span class=pl-kos>;</span></td>
        </tr>
        <tr>
          <td id="file-apifetchsample-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-apifetchsample-ts-LC6" class="blob-code blob-code-inner js-file-line">  <span class=pl-kos>}</span><span class=pl-kos>;</span></td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/44438baa3b6ece08a191b0e918c01a77/raw/5eb52ec2972eae06d298fd6fecca1aadb568ac13/ApiFetchSample.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/44438baa3b6ece08a191b0e918c01a77#file-apifetchsample-ts" class="Link--inTextBlock">
          ApiFetchSample.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>at some point, you probably also experienced the desynchronization of backend (C#, in our example) and frontend (TypeScript) types definitions. What if someone has changed the C# version of <code>UserViewModel</code>, but no one corrected its TypeScript&#8217;s equivalent? </p>



<p>Your TypeScript fetching code will tell nothing about that. <strong>There will be no error, even though the fetched data doesn&#8217;t match the expected <code>UserViewModel</code> type</strong>.</p>



<p>I&#8217;ll try to address this issue in this article <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Let&#8217;s see how typing API responses with <code>zod</code> can help us here.</p>



<span id="more-4093"></span>



<h2 class="wp-block-heading">Synchronization of Backend and Frontend API Typings</h2>



<p>First, why would we want to keep the backend and frontend models in sync?</p>



<p>For me, that&#8217;s the purpose of using TypeScript. <strong>We want our code to be as well-typed as possible</strong>. For instance, we normally want the data displayed to the user to be fully typed. TypeScript enhances our programming experience by providing us with typing information. Thanks to that, we know <em>what is what</em> and <em>what contains</em> <em>what</em>. We also express what types of data we expect in particular cases.</p>



<p>The APIs mostly return JSON data, which can be anything. Because of that, it is much easier to have the data returned from the API fully typed in TypeScript. Thanks to that, <strong>we know what properties are available on the data models received from the API and whether we can use and display them to the users</strong>.</p>



<p>The sample code used within this article is available <a href="https://github.com/dsibinski/DotNetReactZodApp">on GitHub</a>. We will use ASP.NET Core (C#) and React (TypeScript) apps as examples.</p>



<h3 class="wp-block-heading">Models synchronization example</h3>



<p>As we saw in the beginning, a classic example is an API controller that returns a strongly-typed data:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276293" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-aspnetcontrolleraction-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="AspNetControllerAction.cs content, created by dsibinski on 02:57PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="AspNetControllerAction.cs">
        <tr>
          <td id="file-aspnetcontrolleraction-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-aspnetcontrolleraction-cs-LC1" class="blob-code blob-code-inner js-file-line">public List&lt;UserViewModel&gt; AllUsers()</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrolleraction-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-aspnetcontrolleraction-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrolleraction-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-aspnetcontrolleraction-cs-LC3" class="blob-code blob-code-inner js-file-line">    var usersViewModels = TestDataGenerator.GetTestUsers();</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrolleraction-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-aspnetcontrolleraction-cs-LC4" class="blob-code blob-code-inner js-file-line">    return usersViewModels.OrderBy(uvm =&gt; uvm.Name).ToList();</td>
        </tr>
        <tr>
          <td id="file-aspnetcontrolleraction-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-aspnetcontrolleraction-cs-LC5" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/237ce046ebdfcae6b4b7485f8c8c2006/raw/f71478548ff7ccf10fe92893a8c1aedf3ead32e3/AspNetControllerAction.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/237ce046ebdfcae6b4b7485f8c8c2006#file-aspnetcontrolleraction-cs" class="Link--inTextBlock">
          AspNetControllerAction.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>The returned data type is a collection of <code>UserViewModel</code> objects. Here&#8217;s the C# definition of this type:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276303" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-userviewmodel-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="UserViewModel.cs content, created by dsibinski on 02:58PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="UserViewModel.cs">
        <tr>
          <td id="file-userviewmodel-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-userviewmodel-cs-LC1" class="blob-code blob-code-inner js-file-line"><span class=pl-k>public</span> <span class=pl-k>record</span> <span class=pl-smi>UserViewModel</span><span class=pl-kos>(</span><span class=pl-smi>Guid</span> <span class=pl-s1>Id</span><span class=pl-kos>,</span> <span class=pl-smi>string</span> <span class=pl-s1>Name</span><span class=pl-kos>,</span> <span class=pl-smi>string</span> <span class=pl-s1>LastName</span><span class=pl-kos>,</span> </td>
        </tr>
        <tr>
          <td id="file-userviewmodel-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-userviewmodel-cs-LC2" class="blob-code blob-code-inner js-file-line">    <span class=pl-smi>string</span> <span class=pl-s1>Login</span><span class=pl-kos>,</span> <span class=pl-smi>bool</span> <span class=pl-s1>IsActive</span><span class=pl-kos>,</span> <span class=pl-smi>int</span> <span class=pl-s1>LoyaltyPoints</span><span class=pl-kos>,</span> <span class=pl-smi>AddressViewModel</span><span class=pl-c1>?</span> <span class=pl-s1>Address</span> <span class=pl-c1>=</span> <span class=pl-c1>null</span><span class=pl-kos>)</span><span class=pl-kos>;</span></td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/a6921cad261fb274900c991956a18f5a/raw/646dc5ed47ba33b0d663d20716697c1da6933fd3/UserViewModel.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/a6921cad261fb274900c991956a18f5a#file-userviewmodel-cs" class="Link--inTextBlock">
          UserViewModel.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>Its equivalent is also defined on TypeScript side:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276311" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-userviewmodel-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="userViewModel.ts content, created by dsibinski on 02:59PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="userViewModel.ts">
        <tr>
          <td id="file-userviewmodel-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-userviewmodel-ts-LC1" class="blob-code blob-code-inner js-file-line">export interface UserViewModel {</td>
        </tr>
        <tr>
          <td id="file-userviewmodel-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-userviewmodel-ts-LC2" class="blob-code blob-code-inner js-file-line">  id: Guid;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-userviewmodel-ts-LC3" class="blob-code blob-code-inner js-file-line">  name: string;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-userviewmodel-ts-LC4" class="blob-code blob-code-inner js-file-line">  lastName: string;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-userviewmodel-ts-LC5" class="blob-code blob-code-inner js-file-line">  login: string;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-userviewmodel-ts-LC6" class="blob-code blob-code-inner js-file-line">  isActive: boolean;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-userviewmodel-ts-LC7" class="blob-code blob-code-inner js-file-line">  loyaltyPoints: number;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel-ts-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-userviewmodel-ts-LC8" class="blob-code blob-code-inner js-file-line">  address: AddressViewModel | null;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel-ts-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-userviewmodel-ts-LC9" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/d03ea25ba0be55e645a0b6fe35595f47/raw/5d93472032cf0339bc34e88bb76c6be66f17241c/userViewModel.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/d03ea25ba0be55e645a0b6fe35595f47#file-userviewmodel-ts" class="Link--inTextBlock">
          userViewModel.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<h3 class="wp-block-heading">Usage in TypeScript</h3>



<p>Cool. With this simple code, we can create a <code>usersService.ts</code> file and fetch our users&#8217; data from the API. Notice how we make this call strongly typed:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276316" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-usersservice-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="usersService.ts content, created by dsibinski on 03:00PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="usersService.ts">
        <tr>
          <td id="file-usersservice-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-usersservice-ts-LC1" class="blob-code blob-code-inner js-file-line">export class UsersService {</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-usersservice-ts-LC2" class="blob-code blob-code-inner js-file-line">  private apiEndpoint: string;</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-usersservice-ts-LC3" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-usersservice-ts-LC4" class="blob-code blob-code-inner js-file-line">  constructor() {</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-usersservice-ts-LC5" class="blob-code blob-code-inner js-file-line">    this.apiEndpoint = &quot;https://localhost:7131/Users&quot;;</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-usersservice-ts-LC6" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-usersservice-ts-LC7" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-usersservice-ts-LC8" class="blob-code blob-code-inner js-file-line">  getAllUsers = async (): Promise&lt;UserViewModel[]&gt; =&gt; {</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-usersservice-ts-LC9" class="blob-code blob-code-inner js-file-line">    const url = `${this.apiEndpoint}/AllUsers`;</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-usersservice-ts-LC10" class="blob-code blob-code-inner js-file-line">    const response = await fetch(url);</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-usersservice-ts-LC11" class="blob-code blob-code-inner js-file-line">    const users = (await response.json()) as UserViewModel[];</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-usersservice-ts-LC12" class="blob-code blob-code-inner js-file-line">    return users;</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-usersservice-ts-LC13" class="blob-code blob-code-inner js-file-line">  };</td>
        </tr>
        <tr>
          <td id="file-usersservice-ts-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-usersservice-ts-LC14" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/b49678cb4bb3d9ed23b9e59ecd58b595/raw/7cd1a292499e4610a6f85f20678496f1592192b8/usersService.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/b49678cb4bb3d9ed23b9e59ecd58b595#file-usersservice-ts" class="Link--inTextBlock">
          usersService.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>Everything looks legit. We can use the data retrieved from the API in <code>UsersList</code> component and everything is nicely typed:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="736" height="403" data-attachment-id="4108" data-permalink="https://www.codejourney.net/typing-api-responses-with-zod/1_userslisttyped/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/1_UsersListTyped.png?fit=736%2C403&amp;ssl=1" data-orig-size="736,403" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1_UsersListTyped" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/1_UsersListTyped.png?fit=300%2C164&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/1_UsersListTyped.png?fit=736%2C403&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/1_UsersListTyped.png?resize=736%2C403&#038;ssl=1" alt="UsersList React component" class="wp-image-4108" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/1_UsersListTyped.png?w=736&amp;ssl=1 736w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/1_UsersListTyped.png?resize=300%2C164&amp;ssl=1 300w" sizes="auto, (max-width: 736px) 100vw, 736px" /></figure>
</div>


<p>The data is even perfectly displayed:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="950" height="381" data-attachment-id="4109" data-permalink="https://www.codejourney.net/typing-api-responses-with-zod/2_usersdatadispayed/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/2_UsersDataDispayed.png?fit=950%2C381&amp;ssl=1" data-orig-size="950,381" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="2_UsersDataDispayed" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/2_UsersDataDispayed.png?fit=300%2C120&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/2_UsersDataDispayed.png?fit=950%2C381&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/2_UsersDataDispayed.png?resize=950%2C381&#038;ssl=1" alt="Table with users rendered on the web page" class="wp-image-4109" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/2_UsersDataDispayed.png?w=950&amp;ssl=1 950w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/2_UsersDataDispayed.png?resize=300%2C120&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/2_UsersDataDispayed.png?resize=768%2C308&amp;ssl=1 768w" sizes="auto, (max-width: 950px) 100vw, 950px" /></figure>
</div>


<p>So, what can go wrong here? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">The Problem &#8211; Typings&#8217; Desynchronization</h2>



<p>Let&#8217;s say that a backend developer implements a requirement to rename &#8220;loyalty points&#8221; into &#8220;fidelity points&#8221;. Easy. (S)he renames <code>LoyaltyPoints</code> property in the C#&#8217;s <code>UserViewModel</code> to <code>FidelityPoints</code>.</p>



<p>The new C# model looks as follows:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276412" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-userviewmodel_renamed-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="UserViewModel_renamed.cs content, created by dsibinski on 03:12PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="UserViewModel_renamed.cs">
        <tr>
          <td id="file-userviewmodel_renamed-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-userviewmodel_renamed-cs-LC1" class="blob-code blob-code-inner js-file-line">public record UserViewModel(Guid Id, string Name, string LastName, </td>
        </tr>
        <tr>
          <td id="file-userviewmodel_renamed-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-userviewmodel_renamed-cs-LC2" class="blob-code blob-code-inner js-file-line">    string Login, bool IsActive, int FidelityPoints, AddressViewModel? Address = null);</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/7e0f39f3c7ffb2010000bb0ea1629701/raw/f40ac01938a57e487280fe35c4f07400d63b7cd9/UserViewModel_renamed.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/7e0f39f3c7ffb2010000bb0ea1629701#file-userviewmodel_renamed-cs" class="Link--inTextBlock">
          UserViewModel_renamed.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>Nice! The backend dev is a very good programmer, so (s)he even launches the React web application to make sure that everything still works correctly and there are no errors in the dev console:</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="972" height="405" data-attachment-id="4111" data-permalink="https://www.codejourney.net/typing-api-responses-with-zod/3_userddatawithoutloyaltypointsdisplayed/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/3_UserdDataWithoutLoyaltyPointsDisplayed.png?fit=972%2C405&amp;ssl=1" data-orig-size="972,405" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="3_UserdDataWithoutLoyaltyPointsDisplayed" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/3_UserdDataWithoutLoyaltyPointsDisplayed.png?fit=300%2C125&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/3_UserdDataWithoutLoyaltyPointsDisplayed.png?fit=972%2C405&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/3_UserdDataWithoutLoyaltyPointsDisplayed.png?resize=972%2C405&#038;ssl=1" alt="Table of users rendered on the web page with empty &quot;Loyalty points&quot; column and no errors on Chrome DevTools console" class="wp-image-4111" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/3_UserdDataWithoutLoyaltyPointsDisplayed.png?w=972&amp;ssl=1 972w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/3_UserdDataWithoutLoyaltyPointsDisplayed.png?resize=300%2C125&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/3_UserdDataWithoutLoyaltyPointsDisplayed.png?resize=768%2C320&amp;ssl=1 768w" sizes="auto, (max-width: 972px) 100vw, 972px" /></figure>



<p>After a quick look, everything looks awesome. The users list is displayed, there are no errors in the console. Apparently, these test users don&#8217;t have any loyalty points assigned &#8211; that&#8217;s why the empty values in &#8220;Loyalty points&#8221; column. What&#8217;s more, translators will update the column&#8217;s translation later. We are good! Let&#8217;s go on prod! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>I guess you already know what went wrong here. <strong>API definition changed, but TypeScript didn&#8217;t inform us about that</strong> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f614.png" alt="😔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Our <code>UserViewModel</code> still uses the old property name:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="508" height="290" data-attachment-id="4115" data-permalink="https://www.codejourney.net/typing-api-responses-with-zod/4_tsoutdateduserviewmodel/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/4_TSOutdatedUserViewModel.png?fit=508%2C290&amp;ssl=1" data-orig-size="508,290" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="4_TSOutdatedUserViewModel" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/4_TSOutdatedUserViewModel.png?fit=300%2C171&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/4_TSOutdatedUserViewModel.png?fit=508%2C290&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/4_TSOutdatedUserViewModel.png?resize=508%2C290&#038;ssl=1" alt="UserViewModel typescript type definition" class="wp-image-4115" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/4_TSOutdatedUserViewModel.png?w=508&amp;ssl=1 508w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/4_TSOutdatedUserViewModel.png?resize=300%2C171&amp;ssl=1 300w" sizes="auto, (max-width: 508px) 100vw, 508px" /></figure>
</div>


<p>However, it still works. When rendering the <code>UsersList</code>, we simply get <code>undefined</code> in place of <code>loyaltyPoints</code>:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="661" height="313" data-attachment-id="4116" data-permalink="https://www.codejourney.net/typing-api-responses-with-zod/5_tsoldpropertyundefined/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/5_TSOldPropertyUndefined.png?fit=661%2C313&amp;ssl=1" data-orig-size="661,313" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="5_TSOldPropertyUndefined" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/5_TSOldPropertyUndefined.png?fit=300%2C142&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/5_TSOldPropertyUndefined.png?fit=661%2C313&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/5_TSOldPropertyUndefined.png?resize=661%2C313&#038;ssl=1" alt="UsersList rendering at runtime. user.loyaltyPoints property is always undefined" class="wp-image-4116" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/5_TSOldPropertyUndefined.png?w=661&amp;ssl=1 661w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/5_TSOldPropertyUndefined.png?resize=300%2C142&amp;ssl=1 300w" sizes="auto, (max-width: 661px) 100vw, 661px" /></figure>
</div>


<p>In the end, <em>this is <a href="https://www.codejourney.net/2018/11/10-most-shocking-javascript-features-for-csharp-developers/">all JavaScript there</a></em>. What&#8217;s interesting, the renamed <code>fidelityPoints</code> property is already there at runtime:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="441" height="239" data-attachment-id="4117" data-permalink="https://www.codejourney.net/typing-api-responses-with-zod/6_newpropertypresentatjsruntime/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/6_NewPropertyPresentAtJsRuntime.png?fit=441%2C239&amp;ssl=1" data-orig-size="441,239" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="6_NewPropertyPresentAtJsRuntime" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/6_NewPropertyPresentAtJsRuntime.png?fit=300%2C163&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/6_NewPropertyPresentAtJsRuntime.png?fit=441%2C239&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/6_NewPropertyPresentAtJsRuntime.png?resize=441%2C239&#038;ssl=1" alt="At runtime we can see the new fidelityPoints property already present" class="wp-image-4117" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/6_NewPropertyPresentAtJsRuntime.png?w=441&amp;ssl=1 441w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/6_NewPropertyPresentAtJsRuntime.png?resize=300%2C163&amp;ssl=1 300w" sizes="auto, (max-width: 441px) 100vw, 441px" /></figure>
</div>


<p>but no one cared about it <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f614.png" alt="😔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p><strong>With the current solution, we will never be informed soon enough about API models changes in our React application</strong>. In the best case, we&#8217;ll get an <code>undefiend</code> or <code>null</code> error when clicking through the app. However, it&#8217;s usually an end user who finds such problems on production. This is definitely not what we want <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f636.png" alt="😶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>We can solve this problem by typing API responses with <a href="https://github.com/colinhacks/zod">zod</a>. Let&#8217;s now see how to do that.</p>



<h2 class="wp-block-heading">The Solution &#8211; zod</h2>



<p>Our remedy &#8211; <a href="https://github.com/colinhacks/zod"><code>zod</code></a> &#8211; is quite a <a href="https://www.npmjs.com/package/zod">decent npm package</a> with with ~600k weekly downloads. Its GitHub page advertises the library as <em>TypeScript-first schema validation with static type inference</em>.</p>



<p>You can definitely do many things with <code>zod</code>. It can be used together with libraries like <a href="https://react-hook-form.com/">react-hook-form</a> to perform complex forms validation. However, in our case, <strong>we&#8217;ll treat <code>zod</code> as a solution for better typings in TypeScript</strong>.</p>



<h3 class="wp-block-heading">Adding zod to React app</h3>



<p>First, let&#8217;s install <code>zod</code> into our React application:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
npm i zod
</pre></div>


<h3 class="wp-block-heading">First schema definition with zod</h3>



<p>With <code>zod</code>, we define our types in a slightly different way. Instead of creating a <code>type</code> or <code>interface</code> directly, we first create a <em>schema</em>. In our case, we can define a <code>UserViewModelSchema</code> using <code>z.object</code> creator function:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276653" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-userviewmodel_zodschema-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="UserViewModel_ZodSchema.ts content, created by dsibinski on 03:39PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="UserViewModel_ZodSchema.ts">
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-userviewmodel_zodschema-ts-LC1" class="blob-code blob-code-inner js-file-line">export const UserViewModelSchema = z.object({</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-userviewmodel_zodschema-ts-LC2" class="blob-code blob-code-inner js-file-line">  id: z.string().uuid(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-userviewmodel_zodschema-ts-LC3" class="blob-code blob-code-inner js-file-line">  name: z.string(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-userviewmodel_zodschema-ts-LC4" class="blob-code blob-code-inner js-file-line">  lastName: z.string(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-userviewmodel_zodschema-ts-LC5" class="blob-code blob-code-inner js-file-line">  login: z.string(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-userviewmodel_zodschema-ts-LC6" class="blob-code blob-code-inner js-file-line">  isActive: z.boolean(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-userviewmodel_zodschema-ts-LC7" class="blob-code blob-code-inner js-file-line">  loyaltyPoints: z.number(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-userviewmodel_zodschema-ts-LC8" class="blob-code blob-code-inner js-file-line">  address: AddressViewModelSchema.nullable(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zodschema-ts-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-userviewmodel_zodschema-ts-LC9" class="blob-code blob-code-inner js-file-line">});</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/9c7759dff996babccf1b14da276d76c8/raw/959d8bf640a1f20dc10b7975c7a8fa40e62392d5/UserViewModel_ZodSchema.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/9c7759dff996babccf1b14da276d76c8#file-userviewmodel_zodschema-ts" class="Link--inTextBlock">
          UserViewModel_ZodSchema.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>Few interesting parts here:</p>



<ul class="wp-block-list"><li><em>Line 2</em>: notice how <code>zod</code> helps us to define types like <code>Guid</code> with built-in schemas like <code>uuid()</code></li><li><em>Line 8</em>: first, I used <code>AddressViewModelSchema</code> here. This is a custom schema of an <code>AddressViewModel</code> object, which is another type used internally in <code>UserViewModel</code>. You can use such custom schemas in other schemas. Also notice the <code>nullable()</code> call here, which makes the <code>address</code> property nullable</li></ul>



<p>First step done &#8211; we have our <code>UserViewModelSchema</code>. But can we use it instead of <code>UserViewModel</code> type? Not really. Schema is used for validation purposes only. We still need the <code>UserViewModel</code> TypeScript&#8217;s type. </p>



<h3 class="wp-block-heading">Inferring type from zod&#8217;s schema</h3>



<p>Fortunately, <code>zod</code> comes with a handy <code>z.infer</code> function that allows us to <em>infer</em> the type from the schema.</p>



<p>Finally, the <code>userViewModel.ts</code> file looks as follows:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276693" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-userviewmodel_zod-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="userViewModel_zod.ts content, created by dsibinski on 03:43PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="userViewModel_zod.ts">
        <tr>
          <td id="file-userviewmodel_zod-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-userviewmodel_zod-ts-LC1" class="blob-code blob-code-inner js-file-line">import { z } from &quot;zod&quot;;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-userviewmodel_zod-ts-LC2" class="blob-code blob-code-inner js-file-line">import { AddressViewModelSchema } from &quot;./addressViewModel&quot;;</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-userviewmodel_zod-ts-LC3" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-userviewmodel_zod-ts-LC4" class="blob-code blob-code-inner js-file-line">export const UserViewModelSchema = z.object({</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-userviewmodel_zod-ts-LC5" class="blob-code blob-code-inner js-file-line">  id: z.string().uuid(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-userviewmodel_zod-ts-LC6" class="blob-code blob-code-inner js-file-line">  name: z.string(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-userviewmodel_zod-ts-LC7" class="blob-code blob-code-inner js-file-line">  lastName: z.string(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-userviewmodel_zod-ts-LC8" class="blob-code blob-code-inner js-file-line">  login: z.string(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-userviewmodel_zod-ts-LC9" class="blob-code blob-code-inner js-file-line">  isActive: z.boolean(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-userviewmodel_zod-ts-LC10" class="blob-code blob-code-inner js-file-line">  loyaltyPoints: z.number(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-userviewmodel_zod-ts-LC11" class="blob-code blob-code-inner js-file-line">  address: AddressViewModelSchema.nullable(),</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-userviewmodel_zod-ts-LC12" class="blob-code blob-code-inner js-file-line">});</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-userviewmodel_zod-ts-LC13" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-userviewmodel_zod-ts-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-userviewmodel_zod-ts-LC14" class="blob-code blob-code-inner js-file-line">export type UserViewModel = z.infer&lt;typeof UserViewModelSchema&gt;;</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/64cb91fade703c4bbf6ade4c6ad84d19/raw/d45af9863a80557b643b3b04c3440287c20a923b/userViewModel_zod.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/64cb91fade703c4bbf6ade4c6ad84d19#file-userviewmodel_zod-ts" class="Link--inTextBlock">
          userViewModel_zod.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>We can use the exported <code>UserViewModel</code> type as previously used type. It&#8217;s an equivalent, inferred from <code>UserViewModelSchema</code>.</p>



<h3 class="wp-block-heading">Validating API responses with zod schema</h3>



<p>One last step is to make use of <code>UserViewModelSchema</code>. Let&#8217;s modify the <code>getAllUsers</code> function from <code>usersService</code> to validate the data received from the API against our schema:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist117276973" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-getallusers_with_zod-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="getAllUsers_with_zod.ts content, created by dsibinski on 04:13PM on July 09, 2022."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="getAllUsers_with_zod.ts">
        <tr>
          <td id="file-getallusers_with_zod-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-getallusers_with_zod-ts-LC1" class="blob-code blob-code-inner js-file-line">getAllUsers = async (): Promise&lt;UserViewModel[]&gt; =&gt; {</td>
        </tr>
        <tr>
          <td id="file-getallusers_with_zod-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-getallusers_with_zod-ts-LC2" class="blob-code blob-code-inner js-file-line">  const url = `${this.apiEndpoint}/AllUsers`;</td>
        </tr>
        <tr>
          <td id="file-getallusers_with_zod-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-getallusers_with_zod-ts-LC3" class="blob-code blob-code-inner js-file-line">  const response = await fetch(url);</td>
        </tr>
        <tr>
          <td id="file-getallusers_with_zod-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-getallusers_with_zod-ts-LC4" class="blob-code blob-code-inner js-file-line">  const usersJson = await response.json();</td>
        </tr>
        <tr>
          <td id="file-getallusers_with_zod-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-getallusers_with_zod-ts-LC5" class="blob-code blob-code-inner js-file-line">  const users = z.array(UserViewModelSchema).parse(usersJson);</td>
        </tr>
        <tr>
          <td id="file-getallusers_with_zod-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-getallusers_with_zod-ts-LC6" class="blob-code blob-code-inner js-file-line">  return users;</td>
        </tr>
        <tr>
          <td id="file-getallusers_with_zod-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-getallusers_with_zod-ts-LC7" class="blob-code blob-code-inner js-file-line">};</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/ac10bdf2e256f549939d6eeed68067aa/raw/d996f3ee73f81ada41ee15ac3fadf14064992acc/getAllUsers_with_zod.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/ac10bdf2e256f549939d6eeed68067aa#file-getallusers_with_zod-ts" class="Link--inTextBlock">
          getAllUsers_with_zod.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>Notice the usage of <code>z.array</code>. This function call tells <code>zod</code> to validate an array of objects meeting the rules defined by <code>UserViewModelSchema</code>, not a single object.</p>



<p>Now, let&#8217;s run our React app and see what happens when we click the &#8220;Fetch users&#8221; button:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="651" height="592" data-attachment-id="4121" data-permalink="https://www.codejourney.net/typing-api-responses-with-zod/7_zodschemavalidationerror/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/7_ZodSchemaValidationError.png?fit=651%2C592&amp;ssl=1" data-orig-size="651,592" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="7_ZodSchemaValidationError" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/7_ZodSchemaValidationError.png?fit=300%2C273&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/7_ZodSchemaValidationError.png?fit=651%2C592&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/7_ZodSchemaValidationError.png?resize=651%2C592&#038;ssl=1" alt="zod throws schema validation error on Chrome dev tools console" class="wp-image-4121" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/7_ZodSchemaValidationError.png?w=651&amp;ssl=1 651w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/07/7_ZodSchemaValidationError.png?resize=300%2C273&amp;ssl=1 300w" sizes="auto, (max-width: 651px) 100vw, 651px" /></figure>
</div>


<p>This is awesome! Exactly what we wanted &#8211; a <strong>schema validation error for API response</strong>. Notice how the <strong>error message precisely points to the missing (or wrong, in other cases) property.</strong> It tells us we expected a <code>number</code> called <code>loyaltyPoints</code>, but instead we received <code>undefined</code>. The reason for this error message is that the <code>loyaltyPoints</code> field is <code>Required</code> in our schema.</p>



<p>After renaming <code>loyaltyPoints</code> to <code>fidelityPoints</code> in <code>UserViewModelSchema</code> and updating the <code>UsersList</code> component accordingly, everything works fine again. </p>



<p>We are now fully typed and prepared for the future, in case an issue with desynchronization of frontend and backend typings happens again <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f680.png" alt="🚀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Summary</h2>



<p>Today, we&#8217;ve seen how typing API responses with <code>zod</code> can help us detect frontend and backend models desynchronization. Schema validation throws errors when the data doesn&#8217;t match its expected shape. </p>



<p>Remember that <code>zod</code> is an extended library with many options. I recommend <a href="https://github.com/colinhacks/zod">exploring them</a> on your own. An interesting feature we didn&#8217;t cover in this article is <a href="https://github.com/colinhacks/zod#strict">strict mode</a>, which doesn&#8217;t allow additional fields not present in the schema definition when validating the data object. </p>



<p>The open question remains whether to use schema validation on production. One could think that it&#8217;s better to not throw any validation errors, because <em>JavaScript may just work</em>. However, I think that throwing an error is always better than <em>silently</em> letting things through. An error lets programmers, automated tests or manual testers to detect the issue before the end user does <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>You can explore the complete code presented in this article <a href="https://github.com/dsibinski/DotNetReactZodApp">here</a>.</p>
<p>The post <a href="https://www.codejourney.net/typing-api-responses-with-zod/">Typing API Responses With Zod</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/typing-api-responses-with-zod/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4093</post-id>	</item>
		<item>
		<title>How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</title>
		<link>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/</link>
					<comments>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Fri, 30 Apr 2021 04:33:47 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[npm ERR! enoent ENOENT]]></category>
		<category><![CDATA[npm error]]></category>
		<category><![CDATA[npm install package]]></category>
		<category><![CDATA[react]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3927</guid>

					<description><![CDATA[<p>I recently struggled for a while with an npm error thrown when executing npm install of some package. The error message was npm ERR! enoent ENOENT: no such file or directory, rename 'D:\\WebApp\\node_modules\\lz-string' -&#62; 'D:\\WebApp\\node_modules.lz-string.DELETE' Finally, I found a solution and a reason for that issue. The error occurred when I was trying to install&#8230;</p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/">How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[


<p>I recently struggled for a while with an <code>npm</code> error thrown when executing <code>npm install</code> of some package. The error message was <code>npm ERR! enoent ENOENT: no such file or directory, rename 'D:\\WebApp\\node_modules\\lz-string' -&gt; 'D:\\WebApp\\node_modules.lz-string.DELETE'</code></p>



<p>Finally, I found a solution and a reason for that issue.</p>



<span id="more-3927"></span>



<p>The error occurred when I was trying to install <code>@testing-library/react</code> npm package. It looked like that:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="404" data-attachment-id="3928" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/npm_rename_error/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=1360%2C536&amp;ssl=1" data-orig-size="1360,536" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="npm_rename_error" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=300%2C118&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=1024%2C404&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=1024%2C404&#038;ssl=1" alt="npm ERR! enoent ENOENT error in Visual Studio Code" class="wp-image-3928" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=1024%2C404&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=300%2C118&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=768%2C303&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=676%2C266&amp;ssl=1 676w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?w=1360&amp;ssl=1 1360w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>
</div>


<h2 class="wp-block-heading">Hotfix</h2>



<p>If you ever get this error, the hotfix is to follow these steps:</p>



<ul class="wp-block-list"><li>delete <code>node-modules</code> folder</li><li>run command <code>npm cache clean --force</code></li><li>run command <code>npm install</code></li><li>install the package again with <code>npm install your-package-name</code></li></ul>



<p>It should all work fine after that. </p>



<p>If these commands still don&#8217;t solve your issue, and you use <code>git</code> as a source control system, you can try the unbeatable <code>git clean -fdx</code> command. After that, run <code>npm install</code> again. Beware &#8211; it removes all files not checked in to git. It may also remove your IDE settings etc., so use with care.</p>



<p>But it&#8217;s only a hotfix, a solution <em>for now</em> to unblock you.</p>



<h2 class="wp-block-heading">Coldfix (solution)</h2>



<p>The real reason for this issue in my case turned out to be related to <code>jest</code>. However, not to the testing library itself, but to <a href="https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest" target="_blank" rel="noreferrer noopener">jest extension for Visual Studio Code</a>.</p>



<p>The reason for the issue is the jest tests runner working in the background. You can see that in the VS Code bottom bar:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="320" height="158" data-attachment-id="3929" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/vscode_jest_runner/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=320%2C158&amp;ssl=1" data-orig-size="320,158" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="vsCode_jest_runner" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=300%2C148&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=320%2C158&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?resize=320%2C158&#038;ssl=1" alt="Visual Studio Code - jest runner" class="wp-image-3929" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?w=320&amp;ssl=1 320w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?resize=300%2C148&amp;ssl=1 300w" sizes="auto, (max-width: 320px) 100vw, 320px" /></figure>
</div>


<p>The real solution is to disable jest runner when installing new packages. You can do it with a <code>Jest: Stop Runner</code> command in Ctrl+Shift+P:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="777" height="246" data-attachment-id="3930" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/vscode_jest_stop_runner/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=777%2C246&amp;ssl=1" data-orig-size="777,246" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="VSCode_jest_stop_runner" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=300%2C95&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=777%2C246&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=777%2C246&#038;ssl=1" alt="npm ERR! enoent ENOENT fix in Visual Studio by stopping jest runner" class="wp-image-3930" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?w=777&amp;ssl=1 777w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=300%2C95&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=768%2C243&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=676%2C214&amp;ssl=1 676w" sizes="auto, (max-width: 777px) 100vw, 777px" /></figure>
</div>


<p>I don&#8217;t know exactly why this is an issue. I guess jest runner is blocking some files in <code>node-modules</code>, so they cannot be renamed/processed. I hope it helps you too <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/">How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/feed/</wfw:commentRss>
			<slash:comments>13</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3927</post-id>	</item>
		<item>
		<title>Is It Worth Migrating to TypeScript?</title>
		<link>https://www.codejourney.net/is-it-worth-migrating-to-typescript/</link>
					<comments>https://www.codejourney.net/is-it-worth-migrating-to-typescript/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sat, 29 Aug 2020 11:03:05 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ts]]></category>
		<category><![CDATA[typescript]]></category>
		<category><![CDATA[typescript migration]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3840</guid>

					<description><![CDATA[<p>Choosing migration strategy Bright side of migrating to TypeScript If you are a JavaScript developer, the best advice I can give you is to start using TypeScript 🙂 In this section I&#8217;m describing what positively surprised me since the beginning of starting to use TypeScript. Types &#8220;for free&#8221; As soon as we changed our transpiler&#8230;</p>
<p>The post <a href="https://www.codejourney.net/is-it-worth-migrating-to-typescript/">Is It Worth Migrating to TypeScript?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>For the past year I&#8217;ve been working on a JavaScript project for my client. We have a mid-size web application. We use React as the web development framework. Few months ago we decided to start migrating to TypeScript.</p>
<p>Today I can say that <strong>it was the best decision we could make</strong>. TypeScript makes working with JavaScript, which is sometimes <a href="https://www.codejourney.net/2018/11/10-most-shocking-javascript-features-for-csharp-developers/" target="_blank" rel="noopener noreferrer">surprising and quite hard to understand</a>, so much better experience. However, there were some challenges on this journey. If you want to know what issues we met and how we solved them, but also what huge advantages TypeScript gave us &#8211; read on <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><em>I will not discuss TypeScript itself in this article. This is a pure recap of challenges and the experience I gained when migrating to TypeScript.</em></p>



<span id="more-3840"></span>



<h2 class="wp-block-heading">Choosing migration strategy</h2>



<p>Before starting migrating to TypeScript, we had to choose the best strategy for this process. There were two possibilities we considered: <strong>migrating the whole JavaScript codebase to TypeScript at once</strong> or <strong>switching to TypeScript compiler and migrating part-by-part</strong>.</p>
<p>For those who are not familiar with TypeScript &#8211; it is a superset of JavaScript which adds typing information to your JS code. It means that <strong>every JavaScript code is also a valid TypeScript code</strong>. When compiling TypeScript code, TS compiler removes the typing data and outputs readable, vanilla JavaScript code ready to be executed.</p>
<p>Migrating the whole codebase to TypeScript would mean to change all files extensions to TypeScript ones: <em>.js</em> to <em>.ts</em> and <em>.jsx</em> to <em>.tsx</em>. It would also mean that TypeScript compiler starts checking types in those files. This approach is doable, but &#8211; depending on the size of your project &#8211; can take a long time. During migration the code would be frozen until you fully migrate the project. Also, for TypeScript beginners, it could be a bit frustrating having to type everything at once or use some tricks to tell TypeScript to ignore checking types in many cases (because the type is not known at this stage, or we want to do it later etc.).</p>
<p>Because of that, <strong>we decided to migrate part-by-part, having JavaScript and TypeScript files at the same time</strong>. It is possible thanks to enabling <a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html" target="_blank" rel="noopener noreferrer"><em>allowJs&nbsp;</em>TS compiler option</a>. It means that TypeScript will compile both TS and JS files. Such approach allows the coexistence of old JS code with the new TS parts. It also allows moving only chosen parts of the application to TypeScript at the beginning and come back to the others later.</p>
<p>In our web application the biggest JS files have few thousand lines of code. Some of this legacy code still didn&#8217;t use React, but <a href="https://backbonejs.org/" target="_blank" rel="noopener noreferrer">Backbone</a> as their framework. We didn&#8217;t want to touch these oldest files in the beginning. Instead, <strong>we moved few recently written functionalities to TypeScript straightaway and decided to implement everything new in TS as well</strong>.</p>



<h2 class="wp-block-heading">Bright side of migrating to TypeScript</h2>



<p>If you are a JavaScript developer, the best advice I can give you is to start using TypeScript <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> In this section I&#8217;m describing what positively surprised me since the beginning of starting to use TypeScript.</p>



<h3 class="wp-block-heading">Types &#8220;for free&#8221;</h3>



<p>As soon as we changed our transpiler from <em>babel-loader</em> to <em>ts-loader, </em>I wanted to know how we can get typing information for already-installed npm packages. It turns out that <strong>for 98% of the packages we used there&#8217;s already a typing information available</strong> as a separate npm package. </p>



<p>In most cases if you have a npm package called <em>abc</em> there&#8217;s a typing package available called <em>@types/abc</em>. I explored our <em>package.json </em>and for each package listed there I just executed <em>npm install @types/package-name</em>. 2 or 3 older or not-so-popular packages didn&#8217;t have types definitions available, but for the rest we got types for free, without having to type anything ourselves. This was a first, very nice surprise <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>You can find more information about TypeScript types definition packages, as well as contribute one yourself, <a href="https://github.com/DefinitelyTyped/DefinitelyTyped" target="_blank" rel="noreferrer noopener">here</a>.</p>



<h3 class="wp-block-heading">All new code is typed</h3>



<p>As I wrote before, every new feature we&#8217;ve added since the migration is written in TypeScript. Thanks to that, <strong>all n</strong>ew code we write <strong>is strongly typed</strong>. What&#8217;s remarkable here is that it required almost no effort. We just switched the compiler to TS (with a small struggle with webpack configuration, about which you can read below) and suddenly got into typed world for every new feature we implement. Quite impressive, isn&#8217;t it? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">JS-TS friendship</h3>



<p>Coexistence and friendship between JavaScript and TypeScript is HUGE. <strong>These two creatures are not enemies &#8211; they are best friends</strong> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /> We&#8217;ve already gone through many scenarios when we needed to use something written in TS in a JS file and otherwise. It turns out that all of these scenarios are manageable. Sometimes you need to import something differently (for example using <em>require</em> instead of <em>import</em>), but it&#8217;s all possible. We found it super easy to write new features or even add parts of new features to already-existing JS code, writing everything in TypeScript.</p>



<p>We also rewrote some JS files that were used from multiple places in the app to TypeScript. As an example of JS-TS coexistence, I&#8217;m presenting below the potential <em>.ts</em> source code you might have. It contains both old, legacy JS code when we used <em>module.exports</em> and <em>prototype</em> to expose various services for data fetching, as well as a brand-new, nicely-exported TypeScript class:</p>



<p><style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist105109386" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-services-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="services.ts content, created by dsibinski on 08:09AM on August 29, 2020."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="services.ts">
        <tr>
          <td id="file-services-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-services-ts-LC1" class="blob-code blob-code-inner js-file-line">module.exports.UsersService = function (url) {</td>
        </tr>
        <tr>
          <td id="file-services-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-services-ts-LC2" class="blob-code blob-code-inner js-file-line">  // &#8230; UsersService init code &#8230;</td>
        </tr>
        <tr>
          <td id="file-services-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-services-ts-LC3" class="blob-code blob-code-inner js-file-line">};</td>
        </tr>
        <tr>
          <td id="file-services-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-services-ts-LC4" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-services-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-services-ts-LC5" class="blob-code blob-code-inner js-file-line">module.exports.UsersService.prototype.getUser = function (id) {</td>
        </tr>
        <tr>
          <td id="file-services-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-services-ts-LC6" class="blob-code blob-code-inner js-file-line">  // &#8230; code to get active user by id &#8230;</td>
        </tr>
        <tr>
          <td id="file-services-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-services-ts-LC7" class="blob-code blob-code-inner js-file-line">};</td>
        </tr>
        <tr>
          <td id="file-services-ts-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-services-ts-LC8" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-services-ts-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-services-ts-LC9" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-services-ts-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-services-ts-LC10" class="blob-code blob-code-inner js-file-line">export class NewUsersService {</td>
        </tr>
        <tr>
          <td id="file-services-ts-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-services-ts-LC11" class="blob-code blob-code-inner js-file-line">  url: string;</td>
        </tr>
        <tr>
          <td id="file-services-ts-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-services-ts-LC12" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-services-ts-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-services-ts-LC13" class="blob-code blob-code-inner js-file-line">  constructor(url: string) {</td>
        </tr>
        <tr>
          <td id="file-services-ts-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-services-ts-LC14" class="blob-code blob-code-inner js-file-line">    // &#8230; NewUsersService init code &#8230;</td>
        </tr>
        <tr>
          <td id="file-services-ts-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-services-ts-LC15" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-services-ts-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-services-ts-LC16" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-services-ts-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-services-ts-LC17" class="blob-code blob-code-inner js-file-line">  getUser(id: number): JQuery.Promise&lt;UserViewModel, any, any&gt; {</td>
        </tr>
        <tr>
          <td id="file-services-ts-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-services-ts-LC18" class="blob-code blob-code-inner js-file-line">    // &#8230; code to get active user by id &#8230;</td>
        </tr>
        <tr>
          <td id="file-services-ts-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-services-ts-LC19" class="blob-code blob-code-inner js-file-line">  }</td>
        </tr>
        <tr>
          <td id="file-services-ts-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-services-ts-LC20" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/fceb3650f2121640a5e32a78ec3dc740/raw/504b504a930a8262f334017f8aeddc675e3692bf/services.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/fceb3650f2121640a5e32a78ec3dc740#file-services-ts" class="Link--inTextBlock">
          services.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>
 </p>



<p>Such code can of course be in separate files, but it only shows how good TS and JS can coexist together. You can keep your old code working, while implementing anything new in TypeScript.</p>



<h3 class="wp-block-heading">Flexibility in partial migrating to TypeScript</h3>



<p>It turns out that choosing to partially move our web project to TypeScript was a great decision. The team wouldn&#8217;t be so happy and enthusiastic about TS if we told them to move legacy Backbone code to TypeScript. Instead, we gave them a nice tool which can be used for all new stuff. Also, if someone likes the idea and wants to move already-existing JS file to TS, he or she is welcome to do so. </p>



<p>That&#8217;s how <strong>we are still migrating our project to TypeScript, without any hustle and frustration</strong> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Wisdom of TypeScript</h3>



<p>I&#8217;m positively surprised by TypeScript almost every day. At some point I get to the issue that seems hard to be solved or quite non-standard. Then I find a solution online and <strong>it makes me really impressed about the job <a href="https://github.com/microsoft/TypeScript" target="_blank" rel="noreferrer noopener">the community and Microsoft has been doing</a> around the language</strong>. </p>



<p>These are sometimes small, but smart things. Recently we got surprised by <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#keyof-and-lookup-types" target="_blank" rel="noreferrer noopener">keyof</a>, which lets you get names of the allowed properties of your type. Generally <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html" target="_blank" rel="noreferrer noopener">the typing system</a> is really advanced and impressive. For some it can be a disadvantage, but I think it gives an enormous flexibility. </p>



<p>This is somehow the JavaScript you-can-do-whatever-you-want philosophy smuggled into TypeScript <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f601.png" alt="😁" class="wp-smiley" style="height: 1em; max-height: 1em;" /> But in a much smarter way. I&#8217;m sure that when you start working with TypeScript you&#8217;ll get what I mean by its wisdom <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Small effort, huge benefits</h3>



<p>Last, but not least, I must admit that <strong>migrating to TypeScript was quite an easy and smooth process. The value for the effort is huge</strong>. You get a typed world with very little work.</p>



<h2 class="wp-block-heading">Migrating to TypeScript struggles</h2>



<p>During the migration process we met few difficulties. Some of them we managed to solve fully or partially. If you have any better ideas on these things, feel free to let me know in the comments.</p>



<h3 class="wp-block-heading">Complexity of webpack configuration</h3>



<p>When switching to TypeScript we had few issues with <a href="https://webpack.js.org/" target="_blank" rel="noreferrer noopener">webpack</a> configuration. We have used <a href="https://github.com/babel/babel-loader" target="_blank" rel="noreferrer noopener"><em>babel-loader</em></a> before for transpiling JavaScript and initially we wanted to keep it in the TS compilation process. We first tried to keep <em>babel-loader</em> for JS(X) files and use <em>ts-loader</em> for TS(X) files, but it didn&#8217;t work &#8211; we were getting weird compilation errors. </p>



<p>Then we tried to compile everything using <em>ts-loader</em> and then recompile it again using <em>babel</em> to ensure (as we initially thought would be wise) backwards compatibility. We also couldn&#8217;t make it working.</p>



<p><strong>Finally, we decided to compile everything &#8211; both JavaScript and TypeScript files &#8211; using <em>ts-loader</em></strong> (including React files). It worked, except source code maps. When we tried to debug in Chrome console it seemed the source code is not refreshing properly after rebuilding. Breakpoints were often not hit and the debugging didn&#8217;t seem right. </p>



<p>After a bit of struggling we found a solution. We set <a href="https://webpack.js.org/configuration/devtool/" target="_blank" rel="noreferrer noopener">devtool</a> webpack option to <em>eval-source-map</em> and configured <em><a href="https://www.npmjs.com/package/source-map-loader" target="_blank" rel="noreferrer noopener">source-map-loader</a></em> for <em>.js</em> files. Finally, the proper part of webpack config looks as follows:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist105110022" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-webpack_partial-config-js" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="webpack_partial.config.js content, created by dsibinski on 09:40AM on August 29, 2020."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="webpack_partial.config.js">
        <tr>
          <td id="file-webpack_partial-config-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-webpack_partial-config-js-LC1" class="blob-code blob-code-inner js-file-line">devtool: &quot;eval-source-map&quot;,</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-webpack_partial-config-js-LC2" class="blob-code blob-code-inner js-file-line">module: {</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-webpack_partial-config-js-LC3" class="blob-code blob-code-inner js-file-line">    rules: [</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-webpack_partial-config-js-LC4" class="blob-code blob-code-inner js-file-line">        { </td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-webpack_partial-config-js-LC5" class="blob-code blob-code-inner js-file-line">            enforce: &quot;pre&quot;, </td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-webpack_partial-config-js-LC6" class="blob-code blob-code-inner js-file-line">            test: /\.js$/, </td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-webpack_partial-config-js-LC7" class="blob-code blob-code-inner js-file-line">            exclude: /node_modules/, </td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-webpack_partial-config-js-LC8" class="blob-code blob-code-inner js-file-line">            loader: &quot;source-map-loader&quot; </td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-webpack_partial-config-js-LC9" class="blob-code blob-code-inner js-file-line">        },</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-webpack_partial-config-js-LC10" class="blob-code blob-code-inner js-file-line">        {</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-webpack_partial-config-js-LC11" class="blob-code blob-code-inner js-file-line">            test: /\.(t|j)sx?$/,</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-webpack_partial-config-js-LC12" class="blob-code blob-code-inner js-file-line">            exclude: /node_modules/,</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-webpack_partial-config-js-LC13" class="blob-code blob-code-inner js-file-line">            use: { </td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-webpack_partial-config-js-LC14" class="blob-code blob-code-inner js-file-line">                loader: &#39;ts-loader&#39;</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-webpack_partial-config-js-LC15" class="blob-code blob-code-inner js-file-line">            }</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-webpack_partial-config-js-LC16" class="blob-code blob-code-inner js-file-line">        },  </td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-webpack_partial-config-js-LC17" class="blob-code blob-code-inner js-file-line">    ],</td>
        </tr>
        <tr>
          <td id="file-webpack_partial-config-js-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-webpack_partial-config-js-LC18" class="blob-code blob-code-inner js-file-line">},</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/2cb3c4c10fe63a9020508c20fa31eef3/raw/6e5f9baf9bf87fec5c43cccf25e5610607c36fa6/webpack_partial.config.js" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/2cb3c4c10fe63a9020508c20fa31eef3#file-webpack_partial-config-js" class="Link--inTextBlock">
          webpack_partial.config.js
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>So far we also haven&#8217;t noticed any issues related to backwards compatibility that were theoretically solved by <em>babel</em>. TypeScript compiler does its job very well.</p>



<h3 class="wp-block-heading">Complicated JS -&gt; TS refactoring</h3>



<p>I found out that JS to TS refactoring is not always an easy task. <strong>Some things that are allowed by JavaScript are not valid in TypeScript</strong>. In most cases it means that something was wrong with your JavaScript code, but we all know how it is with legacy code refactoring <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p class="has-dark-gray-color has-text-color">As an example, we had several functions that we added to a <span style="color:#ef7604" class="tadv-color">String</span> prototype in JavaScript:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist105110060" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-string_prototype_helpers-js" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="string_prototype_helpers.js content, created by dsibinski on 09:47AM on August 29, 2020."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="string_prototype_helpers.js">
        <tr>
          <td id="file-string_prototype_helpers-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-string_prototype_helpers-js-LC1" class="blob-code blob-code-inner js-file-line">String.prototype.contains = function (it) {</td>
        </tr>
        <tr>
          <td id="file-string_prototype_helpers-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-string_prototype_helpers-js-LC2" class="blob-code blob-code-inner js-file-line">        return this.indexOf(it) != -1;</td>
        </tr>
        <tr>
          <td id="file-string_prototype_helpers-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-string_prototype_helpers-js-LC3" class="blob-code blob-code-inner js-file-line">    };</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/5e9553424d7441fddefee5f2519ad6dc/raw/d67095fec29dee3eda04be5994faa58f7f94cee7/string_prototype_helpers.js" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/5e9553424d7441fddefee5f2519ad6dc#file-string_prototype_helpers-js" class="Link--inTextBlock">
          string_prototype_helpers.js
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>Such code becomes invalid in TypeScript:</p>



<figure class="wp-block-image size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="782" height="174" data-attachment-id="3862" data-permalink="https://www.codejourney.net/is-it-worth-migrating-to-typescript/typescript_prototype_functions_error/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2020/08/TypeScript_prototype_functions_error.png?fit=782%2C174&amp;ssl=1" data-orig-size="782,174" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TypeScript_prototype_functions_error" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2020/08/TypeScript_prototype_functions_error.png?fit=300%2C67&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2020/08/TypeScript_prototype_functions_error.png?fit=782%2C174&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2020/08/TypeScript_prototype_functions_error.png?resize=782%2C174&#038;ssl=1" alt="Migrating to TypeScript: TS error about non-existing function on String prototype in VS Code" class="wp-image-3862" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2020/08/TypeScript_prototype_functions_error.png?w=782&amp;ssl=1 782w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2020/08/TypeScript_prototype_functions_error.png?resize=300%2C67&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2020/08/TypeScript_prototype_functions_error.png?resize=768%2C171&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2020/08/TypeScript_prototype_functions_error.png?resize=676%2C150&amp;ssl=1 676w" sizes="auto, (max-width: 782px) 100vw, 782px" /><figcaption>TypeScript complaining about non-existing function</figcaption></figure>



<p>A non-obvious solution to that issue is to extend <span style="color:#ef7604" class="tadv-color">String</span> interface by declaring typed functions you want to add:</p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler"><div class="wp-block-embed__wrapper">
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist105110103" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-string_prototype_helpers-ts" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-typescript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="string_prototype_helpers.ts content, created by dsibinski on 09:53AM on August 29, 2020."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="string_prototype_helpers.ts">
        <tr>
          <td id="file-string_prototype_helpers-ts-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-string_prototype_helpers-ts-LC1" class="blob-code blob-code-inner js-file-line">interface String {</td>
        </tr>
        <tr>
          <td id="file-string_prototype_helpers-ts-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-string_prototype_helpers-ts-LC2" class="blob-code blob-code-inner js-file-line">    contains(this : string, it : any): boolean;</td>
        </tr>
        <tr>
          <td id="file-string_prototype_helpers-ts-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-string_prototype_helpers-ts-LC3" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
        <tr>
          <td id="file-string_prototype_helpers-ts-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-string_prototype_helpers-ts-LC4" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-string_prototype_helpers-ts-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-string_prototype_helpers-ts-LC5" class="blob-code blob-code-inner js-file-line">String.prototype.contains = function (it) {</td>
        </tr>
        <tr>
          <td id="file-string_prototype_helpers-ts-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-string_prototype_helpers-ts-LC6" class="blob-code blob-code-inner js-file-line">    return this.indexOf(it) != -1;</td>
        </tr>
        <tr>
          <td id="file-string_prototype_helpers-ts-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-string_prototype_helpers-ts-LC7" class="blob-code blob-code-inner js-file-line">};</td>
        </tr>
  </table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/f0cd64ae8967aee9a0cfc13409259e1f/raw/92dd7b6a53393c445096b7b63ba08149acf256c5/string_prototype_helpers.ts" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/dsibinski/f0cd64ae8967aee9a0cfc13409259e1f#file-string_prototype_helpers-ts" class="Link--inTextBlock">
          string_prototype_helpers.ts
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

</div></figure>



<p>There are more subtle difficulties like that you can meet while migrating JS code to TypeScript. However, in the end it gives you a possibility to make your codebase better.</p>



<h3 class="wp-block-heading">Keeping view models in sync</h3>



<p>An issue I didn&#8217;t think about before migrating to TypeScript was a need to keep frontend and backend view models in sync.</p>



<p>One of the first things you&#8217;d like to have in your TypeScript codebase is to have as much typed data as possible. We use .NET and C# as the backend of our application. ASP.NET MVC controllers returned the strongly-typed objects into the previously chaotic and untyped JS world. Now we are strongly-typed with TypeScript, so we&#8217;d like all this data typed as well <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Here comes the problem. First of all <strong>our C# data models which we returned from the controllers were not the best</strong>. In most cases we returned a DTO object which contained many properties from which only few were used by JavaScript code. We never saw this problem, because in JS we just dynamically typed properties names we wanted to use. We didn&#8217;t see how much of not needed data is sent from ASP controllers to the web browser.</p>



<p>Now <strong>with TypeScript we needed to create these objects definitions also in TypeScript</strong>. Having a huge DTO object returned from the controller, with another X objects on which this DTO depends, it was a nightmare. <strong>It turned out that for a single DTO I had to create 10-15 TypeScript objects just to use 5-10 properties in the frontend code!</strong></p>



<p>That&#8217;s the moment when we came to the necessity of <strong>creating web view models</strong>. The idea is to <strong>return a view model from a controller, not the whole DTO object. This view model contains only the data which is needed by TypeScript (frontend)</strong>.</p>



<p>I think this is nothing new for experienced web developers. However, now comes the question: <strong>how do we keep backend (C#) view models in sync with frontend (TypeScript) ones?</strong></p>



<p>Unfortunately, I haven&#8217;t found a perfect solution for that. The best method I came up with is&#8230; <strong>manually keeping these view models in sync</strong>. </p>



<p>There&#8217;s a <a href="https://marketplace.visualstudio.com/items?itemName=adrianwilczynski.csharp-to-typescript" target="_blank" rel="noreferrer noopener">C# to TypeScript</a> VS Code extension which allows to paste the copied C# code as its TypeScript equivalent. It makes the job a bit easier. It even has a <a href="https://www.nuget.org/packages/CSharpToTypeScript.CLITool/" target="_blank" rel="noreferrer noopener">CLI tool</a>, but it produces a single <em>.ts</em> file from a single <em>.cs</em> file. If you have multiple objects in a single C# file it will produce a single TypeScript file with all these objects. If any of those objects is used in other files (unfortunately we have many of such cases), these dependencies will not be automatically added as imports in the auto-generated <em>.ts</em> files.</p>



<p>However, I don&#8217;t find it very problematic for now. As soon as the TS view models are created, we just need to update them as often as we update C# view models. I think this is a very common issue so if you know any better solution here &#8211; please share in the comments <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Migrating to TypeScript &#8211; summary</h2>



<p>Migrating to TypeScript has been a very interesting and rewarding process. Compared to vanilla JavaScript, TypeScript lets programmers see many problems. This allows to see how bad our codebase was. We even realized that our server-side C# code was not the best (see the section about view models above). Thanks to introducing TypeScript we improved a lot of our JavaScript and C# code, including its architecture.</p>



<p>I think that TypeScript uses many simple concepts under the hood and it makes a lot of things possible (see <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#keyof-and-lookup-types" target="_blank" rel="noreferrer noopener">keyof</a> or <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#keyof-and-lookup-types" target="_blank" rel="noreferrer noopener">unknown</a>). In many cases TypeScript <em>lets</em> you do something, but <em>doesn&#8217;t force</em> you to do it. That&#8217;s the beauty of this typed JavaScript world <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/is-it-worth-migrating-to-typescript/">Is It Worth Migrating to TypeScript?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/is-it-worth-migrating-to-typescript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3840</post-id>	</item>
		<item>
		<title>10 Most Shocking JavaScript Features for C# Developers</title>
		<link>https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/</link>
					<comments>https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 07 Nov 2018 14:00:43 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=2999</guid>

					<description><![CDATA[<p>Whether you&#8217;re a C# (or similar language like Java) developer eager to learn JavaScript or you&#8217;ve already been working with JS for some time, I hope you find this article interesting. I collected for you 10 JavaScript features which are/were the most shocking for C# developers who learnt JavaScript. These features of JavaScript language are the&#8230;</p>
<p>The post <a href="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/">10 Most Shocking JavaScript Features for C# Developers</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Whether you&#8217;re a C# (or similar language like Java) developer eager to learn JavaScript or you&#8217;ve already been working with JS for some time, I hope you find this article interesting. I collected for you <strong>10 JavaScript features which are/were the most shocking for C# developers</strong> who learnt JavaScript.</p>
<p><span id="more-2999"></span></p>
<p>These features of JavaScript language are the most striking differences compared to C# ecosystem. If you&#8217;re about to learn JavaScript, sooner or later you&#8217;ll also have to deal with them.</p>
<p><span style="font-size: 10pt;"><em>Before we begin, one disclaimer: the goal of this post isn&#8217;t stating which language is better or worse. JavaScript and C# are different programming languages and their use cases are also completely different. One is better for some usages, while the other is better for others. The list is <a href="https://twitter.com/DawidSibinski/status/1056998751234113536" target="_blank" rel="noopener">my and other readers&#8217;</a> subjective one and you don&#8217;t need to agree with all points. I&#8217;m myself a C# developer on my JavaScript learning journey, so I&#8217;d like to help you grasping these confusing concepts <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></em></span></p>
<h1>1. Dynamic Typing</h1>
<p>Obviously, the first difference JavaScript newbie notices is that the language is <strong>dynamically-typed</strong>. It means that the <strong>types</strong> (of variables, functions, actually of almost everything) <strong>are checked at runtime</strong>, <strong>not at compile time</strong> like in C# or Java.</p>
<p>Because of that, there&#8217;s not much difference in defining variables and assigning them data of various <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" target="_blank" rel="noopener">data types</a>:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874493" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-ja-variablestypes-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JA-variablesTypes.js content, created by dsibinski on 05:47PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JA-variablesTypes.js">
<tr>
<td id="file-ja-variablestypes-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-ja-variablestypes-js-LC1" class="blob-code blob-code-inner js-file-line">  var someNumber = 1;</td>
</tr>
<tr>
<td id="file-ja-variablestypes-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-ja-variablestypes-js-LC2" class="blob-code blob-code-inner js-file-line">  var someBoolean = true;</td>
</tr>
<tr>
<td id="file-ja-variablestypes-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-ja-variablestypes-js-LC3" class="blob-code blob-code-inner js-file-line">  var someString = &#39;Hello JS!&#39;;</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/d2b8e6303e99cf7fe98280eed7d31f82/raw/8c1465a21bee34cd735a3a9b9481bd6eae602f38/JA-variablesTypes.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/d2b8e6303e99cf7fe98280eed7d31f82#file-ja-variablestypes-js" class="Link--inTextBlock"><br />
          JA-variablesTypes.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>JavaScript&#8217;s variables are not associated with any particular data type. That&#8217;s why it&#8217;s completely legit to write something like that:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874504" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_dynamic-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_dynamic.js content, created by dsibinski on 05:48PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_dynamic.js">
<tr>
<td id="file-js_dynamic-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_dynamic-js-LC1" class="blob-code blob-code-inner js-file-line">  var a = 1;         // typeof a =&gt; number</td>
</tr>
<tr>
<td id="file-js_dynamic-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_dynamic-js-LC2" class="blob-code blob-code-inner js-file-line">  a = true;          // typeof a =&gt; boolean</td>
</tr>
<tr>
<td id="file-js_dynamic-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_dynamic-js-LC3" class="blob-code blob-code-inner js-file-line">  a = &#39;Hello JS!&#39;;   // typeof a =&gt; string</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/a0261ec670ae6d49f344956b825c9627/raw/3d80658605472ec71a8ebaee4c1fd59b87281834/JS_dynamic.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/a0261ec670ae6d49f344956b825c9627#file-js_dynamic-js" class="Link--inTextBlock"><br />
          JS_dynamic.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Because JavaScript is dynamically-typed, issues with types are detected at runtime (e.g. error is thrown as soon as you try to use a variable in a context which expects another data type), not during compilation time as it would be in C# or Java.</p>
<p>If you have some experience with statically-typed languages, you may now feel how much confusion and troubles dynamic typing can bring. At least at the beginning.</p>
<h1>2. Implicit types coercion</h1>
<div>JavaScript has one more interesting feature &#8211; <strong>types coercion</strong>. Even though this term&#8217;s name sounds a bit weird <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" />, it&#8217;s quite simple: types coercion happens when a <strong>value of one type is converted to another one</strong>.</div>
<div> </div>
<div>It may be explicit, but harder to grasp is the <strong>implicit types coercion</strong>. It means that developer doesn&#8217;t necessarily ask for converting value of some type to another, but it still happens &#8220;silently&#8221;. To visualize it, consider the following code:</div>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874409" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_typescoercion-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_typesCoercion.js content, created by dsibinski on 05:42PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_typesCoercion.js">
<tr>
<td id="file-js_typescoercion-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_typescoercion-js-LC1" class="blob-code blob-code-inner js-file-line">  var name, isGreatDeveloper;</td>
</tr>
<tr>
<td id="file-js_typescoercion-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_typescoercion-js-LC2" class="blob-code blob-code-inner js-file-line">  name = &#39;Dawid&#39;;</td>
</tr>
<tr>
<td id="file-js_typescoercion-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_typescoercion-js-LC3" class="blob-code blob-code-inner js-file-line">  isGreatDeveloper = true;</td>
</tr>
<tr>
<td id="file-js_typescoercion-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-js_typescoercion-js-LC4" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-js_typescoercion-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-js_typescoercion-js-LC5" class="blob-code blob-code-inner js-file-line">  console.log(&#39;Is &#39; + name + &#39; a great dev?: &#39; + isGreatDeveloper); </td>
</tr>
<tr>
<td id="file-js_typescoercion-js-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-js_typescoercion-js-LC6" class="blob-code blob-code-inner js-file-line">  // displays &#39;Is Dawid a great dev?: true&#39;</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/ad25af60a0cfee06066f72cd7ccbc5ab/raw/136633dc5779e33da22d14658ef3daab76610df0/JS_typesCoercion.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/ad25af60a0cfee06066f72cd7ccbc5ab#file-js_typescoercion-js" class="Link--inTextBlock"><br />
          JS_typesCoercion.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<div>Even though <span style="color: #ff6600;">console.log()</span> expects data of text type, <span style="color: #ff6600;">isGreatDeveloper</span> which holds a <em>boolean</em> value at the moment is implicitly converted to text.</p>
</div>
<div>The above example was just to show you what types coercion is all about, but it gets more confusing in reality. First misleading concept is that JS has actually two equality operators: <span style="color: #ff6600;">==</span> and <span style="color: #ff6600;">===</span>.</p>
</div>
<div>Let&#8217;s see some code example to know the difference between them:</div>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874715" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_equalityoperators-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_equalityOperators.js content, created by dsibinski on 06:01PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_equalityOperators.js">
<tr>
<td id="file-js_equalityoperators-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_equalityoperators-js-LC1" class="blob-code blob-code-inner js-file-line">  var age = 26;</td>
</tr>
<tr>
<td id="file-js_equalityoperators-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_equalityoperators-js-LC2" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-js_equalityoperators-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_equalityoperators-js-LC3" class="blob-code blob-code-inner js-file-line">  if(age == &#39;26&#39;){</td>
</tr>
<tr>
<td id="file-js_equalityoperators-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-js_equalityoperators-js-LC4" class="blob-code blob-code-inner js-file-line">      // this is executed</td>
</tr>
<tr>
<td id="file-js_equalityoperators-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-js_equalityoperators-js-LC5" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
<tr>
<td id="file-js_equalityoperators-js-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-js_equalityoperators-js-LC6" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-js_equalityoperators-js-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-js_equalityoperators-js-LC7" class="blob-code blob-code-inner js-file-line">  if(age === &#39;26&#39;){</td>
</tr>
<tr>
<td id="file-js_equalityoperators-js-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-js_equalityoperators-js-LC8" class="blob-code blob-code-inner js-file-line">      // this is NOT executed</td>
</tr>
<tr>
<td id="file-js_equalityoperators-js-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-js_equalityoperators-js-LC9" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/c509a2162e81f44e34d2995f2a8e08bd/raw/08ca99ee63291d8b599282c3489a0d383d5a8a8c/JS_equalityOperators.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/c509a2162e81f44e34d2995f2a8e08bd#file-js_equalityoperators-js" class="Link--inTextBlock"><br />
          JS_equalityOperators.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<div>The first <strong><span style="color: #ff6600;">==</span> operator performs an implicit types coercion before performing the actual comparison</strong>. That&#8217;s why the text &#8217;26&#8217; is &#8220;silently&#8221; converted to a number 26 and the <em>if</em> statement is <em>true</em> at this point.</p>
</div>
<div>Because of that, we should normally always use <span style="color: #ff6600;">===</span> operator to avoid such unclear situations.</p>
</div>
<div>Existence of such (and many more) nice concepts of JavaScript leads to finding such interesting Tweets:</div>
<div align="center">
<blockquote class="twitter-tweet" data-lang="en">
<p dir="ltr" lang="en">*shuts laptop forever* <a href="https://t.co/3GS1JfNDSP">pic.twitter.com/3GS1JfNDSP</a></p>
<p>— Shayna Gentiluomo (@kd2luw) <a href="https://twitter.com/kd2luw/status/992096570802765824?ref_src=twsrc%5Etfw">May 3, 2018</a></p>
</blockquote>
<p><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></p>
</div>
<div>If you want to dig into types coercion, you can read <a href="https://medium.freecodecamp.org/js-type-coercion-explained-27ba3d9a2839" target="_blank" rel="noopener">this article</a>.</div>
<h1>3. <em>null</em> and <em>undefined</em></h1>
<p>In JavaScript there are two values which represent kind of unassigned variables: <em>undefined</em> and <em>null</em>. What&#8217;s the difference between them? The snippet below clarifies:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874833" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_null_undefined-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_null_undefined.js content, created by dsibinski on 06:10PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_null_undefined.js">
<tr>
<td id="file-js_null_undefined-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_null_undefined-js-LC1" class="blob-code blob-code-inner js-file-line">  var x;</td>
</tr>
<tr>
<td id="file-js_null_undefined-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_null_undefined-js-LC2" class="blob-code blob-code-inner js-file-line">  console.log(x); // prints &#39;undefined&#39;</td>
</tr>
<tr>
<td id="file-js_null_undefined-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_null_undefined-js-LC3" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-js_null_undefined-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-js_null_undefined-js-LC4" class="blob-code blob-code-inner js-file-line">  x = null;</td>
</tr>
<tr>
<td id="file-js_null_undefined-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-js_null_undefined-js-LC5" class="blob-code blob-code-inner js-file-line">  console.log(x); // prints &#39;null&#39;</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/be2f7e534a64b397cf351fc9e02517f3/raw/8ae993119a9b05faa44c09edd390eabe04166381/JS_null_undefined.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/be2f7e534a64b397cf351fc9e02517f3#file-js_null_undefined-js" class="Link--inTextBlock"><br />
          JS_null_undefined.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<div>As soon as you declare a variable, but don&#8217;t assign any value to it, it contains <em>undefined</em>. It may contain <em>null</em> if it was assigned to it, either explicitly as in the example or as a result of some operation.</p>
</div>
<div>I see it as the names suggest: <strong><em>undefined</em></strong> is something that <strong>has really not been defined</strong> (assigned) yet, while <strong><em>null</em> </strong>is more like a special <strong>kind of value which has some useful meaning</strong>.</div>
<div>
<h1>4. Truly and Falsy values</h1>
<div>As we already know <em>null</em> and <em>undefined</em>,  it&#8217;s important to note that there are some &#8220;falsy&#8221; and &#8220;truly&#8221; values in JavaScript:</div>
<ul>
<li>&#8220;falsy&#8221; values: <em>undefined</em>, <em>null</em>, <em>0,</em> <em>&#8221;</em>, <em>NaN</em></li>
<li>&#8220;truly&#8221; values: all NOT falsy values</li>
</ul>
<div>It means that &#8220;falsy&#8221; values are implicitly converted to <em>false</em> <em>boolean</em> value e.g. in comparison context:</div>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874876" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_falsy-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_falsy.js content, created by dsibinski on 06:12PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_falsy.js">
<tr>
<td id="file-js_falsy-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_falsy-js-LC1" class="blob-code blob-code-inner js-file-line">  var x; // x is &#39;undefined&#39;</td>
</tr>
<tr>
<td id="file-js_falsy-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_falsy-js-LC2" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-js_falsy-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_falsy-js-LC3" class="blob-code blob-code-inner js-file-line">  if(x){</td>
</tr>
<tr>
<td id="file-js_falsy-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-js_falsy-js-LC4" class="blob-code blob-code-inner js-file-line">      // this is NOT executed</td>
</tr>
<tr>
<td id="file-js_falsy-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-js_falsy-js-LC5" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/78f259ce6528f4bcbe4574f12ac6417b/raw/b10f88989d1f4a0f6316d9b9a179c1c2a82a3c77/JS_falsy.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/78f259ce6528f4bcbe4574f12ac6417b#file-js_falsy-js" class="Link--inTextBlock"><br />
          JS_falsy.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Knowing that you might want to check if the value has been defined in the following way:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874887" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_definedcheckwrong-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_definedCheckWrong.js content, created by dsibinski on 06:13PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_definedCheckWrong.js">
<tr>
<td id="file-js_definedcheckwrong-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_definedcheckwrong-js-LC1" class="blob-code blob-code-inner js-file-line">  if(x){</td>
</tr>
<tr>
<td id="file-js_definedcheckwrong-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_definedcheckwrong-js-LC2" class="blob-code blob-code-inner js-file-line">      console.log(&#39;x is defined!&#39;);</td>
</tr>
<tr>
<td id="file-js_definedcheckwrong-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_definedcheckwrong-js-LC3" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/d6b3fcdc60f8df404aaa0a2d96e8c57e/raw/39700b4b317fd7ac3896d486bf23fb5d8a7f0a29/JS_definedCheckWrong.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/d6b3fcdc60f8df404aaa0a2d96e8c57e#file-js_definedcheckwrong-js" class="Link--inTextBlock"><br />
          JS_definedCheckWrong.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<div>However, assuming <span style="color: #ff6600;">x</span> is a number, this will not work if <span style="color: #ff6600;">x</span> has a value of <em>0</em>, which is also one of the &#8220;falsy&#8221; values.<br />
So the proper way of checking whether the number has been defined is as follows:</div>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92875102" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_definedcheckcorrect-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_definedCheckCorrect.js content, created by dsibinski on 06:29PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_definedCheckCorrect.js">
<tr>
<td id="file-js_definedcheckcorrect-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_definedcheckcorrect-js-LC1" class="blob-code blob-code-inner js-file-line">  if(x || x === 0){</td>
</tr>
<tr>
<td id="file-js_definedcheckcorrect-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_definedcheckcorrect-js-LC2" class="blob-code blob-code-inner js-file-line">      console.log(&#39;x is defined!&#39;);</td>
</tr>
<tr>
<td id="file-js_definedcheckcorrect-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_definedcheckcorrect-js-LC3" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/35a7702884c752f5de1f4dbb4d8ff1d7/raw/a5d0c3d2d6198603ad8ab570b5345a0f2745ea7d/JS_definedCheckCorrect.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/35a7702884c752f5de1f4dbb4d8ff1d7#file-js_definedcheckcorrect-js" class="Link--inTextBlock"><br />
          JS_definedCheckCorrect.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Of course, for another data types (like text) you need to handle other &#8220;falsy&#8221; values as well. It depends what you consider a meaningful value.</p>
</div>
<h1>5. Hoisting</h1>
<p>In JavaScript you can legally write such a code:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874773" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_hoisting-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_hoisting.js content, created by dsibinski on 06:06PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_hoisting.js">
<tr>
<td id="file-js_hoisting-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_hoisting-js-LC1" class="blob-code blob-code-inner js-file-line">  name = &#39;Dawid&#39;;</td>
</tr>
<tr>
<td id="file-js_hoisting-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_hoisting-js-LC2" class="blob-code blob-code-inner js-file-line">  console.log(name);</td>
</tr>
<tr>
<td id="file-js_hoisting-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_hoisting-js-LC3" class="blob-code blob-code-inner js-file-line">  var name;</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/6624f7742d1f05cfad7ea30a023a71e3/raw/42d05b4060261630986efe9b275946d50bba8032/JS_hoisting.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/6624f7742d1f05cfad7ea30a023a71e3#file-js_hoisting-js" class="Link--inTextBlock"><br />
          JS_hoisting.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>As you can notice, <span style="color: #ff6600;">name</span> <strong>variable is used before it&#8217;s declared</strong> 2 lines below. This is maybe not that weird, as in JS we can even declare variables without the <span style="color: #ff6600;">var</span> keyword, but we can do the same with functions:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874785" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_hoisting_functions-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_hoisting_functions.js content, created by dsibinski on 06:07PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_hoisting_functions.js">
<tr>
<td id="file-js_hoisting_functions-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_hoisting_functions-js-LC1" class="blob-code blob-code-inner js-file-line">  printHello(); // prints &#39;Hello!&#39;</td>
</tr>
<tr>
<td id="file-js_hoisting_functions-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_hoisting_functions-js-LC2" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-js_hoisting_functions-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_hoisting_functions-js-LC3" class="blob-code blob-code-inner js-file-line">  function printHello(){</td>
</tr>
<tr>
<td id="file-js_hoisting_functions-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-js_hoisting_functions-js-LC4" class="blob-code blob-code-inner js-file-line">      console.log(&#39;Hello!&#39;);</td>
</tr>
<tr>
<td id="file-js_hoisting_functions-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-js_hoisting_functions-js-LC5" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/045d0c12cb31565baad369a3f6e1f373/raw/12a8f254043f6b23608d075c59212729f2743270/JS_hoisting_functions.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/045d0c12cb31565baad369a3f6e1f373#file-js_hoisting_functions-js" class="Link--inTextBlock"><br />
          JS_hoisting_functions.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>So here we call the function before declaring it. Why does this work?</p>
<p>This works thanks to <strong>hoisting</strong>. It&#8217;s a compile-time mechanism which <strong>&#8220;moves&#8221; all variables and functions declarations to the top of our JavaScript code</strong>. In fact, during the execution of above-listed code, JavaScript engine processes variables and functions declarations before executing any other code.</p>
<p>It however applies only to declarations, not to assignments:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92874805" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-js_hoisting_declarations-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="JS_hoisting_declarations.js content, created by dsibinski on 06:08PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="JS_hoisting_declarations.js">
<tr>
<td id="file-js_hoisting_declarations-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-js_hoisting_declarations-js-LC1" class="blob-code blob-code-inner js-file-line">  console.log(name); // prints &#39;undefined&#39;</td>
</tr>
<tr>
<td id="file-js_hoisting_declarations-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-js_hoisting_declarations-js-LC2" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-js_hoisting_declarations-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-js_hoisting_declarations-js-LC3" class="blob-code blob-code-inner js-file-line">  var name = 5;</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/43f61dbfadb5a1d017039024b2b23f16/raw/17efcaca3652487875c5de628cd7e12375415e72/JS_hoisting_declarations.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/43f61dbfadb5a1d017039024b2b23f16#file-js_hoisting_declarations-js" class="Link--inTextBlock"><br />
          JS_hoisting_declarations.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<div>In this case, only <span style="color: #ff6600;">var name</span> part of the declaration and assignment is &#8220;moved to the top&#8221; and executed first. The assignment part (<span style="color: #ff6600;">=5</span>) is not. That&#8217;s why when calling <span style="color: #ff6600;">console.log(name)</span> the <span style="color: #ff6600;">name</span> variable is <em>undefined</em>.</p>
</div>
<div>Because of hoisting, we should declare and preferably assign all variables used in the given scope at the top of it.</div>
<h1>6. Lexical scoping and <em>this</em> keyword</h1>
<p>To create a scope (space in which the variables defined in it are accessible) in JS, we need to create a new function. Variable defined in a function is not accessible outside this function:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92876193" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-functionscope-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="FunctionScope.js content, created by dsibinski on 07:31PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="FunctionScope.js">
<tr>
<td id="file-functionscope-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-functionscope-js-LC1" class="blob-code blob-code-inner js-file-line">  function a() {</td>
</tr>
<tr>
<td id="file-functionscope-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-functionscope-js-LC2" class="blob-code blob-code-inner js-file-line">      var x = 5;</td>
</tr>
<tr>
<td id="file-functionscope-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-functionscope-js-LC3" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
<tr>
<td id="file-functionscope-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-functionscope-js-LC4" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-functionscope-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-functionscope-js-LC5" class="blob-code blob-code-inner js-file-line">  console.log(x); // Uncaught ReferenceError: </td>
</tr>
<tr>
<td id="file-functionscope-js-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-functionscope-js-LC6" class="blob-code blob-code-inner js-file-line">                  // x is not defined</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/73fa86f7173ec8a2523f0256fb24bab9/raw/a877636b5fa397cb9695972bf5cc2b8b60ea2046/FunctionScope.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/73fa86f7173ec8a2523f0256fb24bab9#file-functionscope-js" class="Link--inTextBlock"><br />
          FunctionScope.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>It means that other blocks like the ones associated with <em>if</em> statement don&#8217;t introduce a new scope:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92876215" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-ifblock-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="IfBlock.cs content, created by dsibinski on 07:32PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="IfBlock.cs">
<tr>
<td id="file-ifblock-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-ifblock-cs-LC1" class="blob-code blob-code-inner js-file-line">  if(true) {</td>
</tr>
<tr>
<td id="file-ifblock-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-ifblock-cs-LC2" class="blob-code blob-code-inner js-file-line">      var x = 5;</td>
</tr>
<tr>
<td id="file-ifblock-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-ifblock-cs-LC3" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
<tr>
<td id="file-ifblock-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-ifblock-cs-LC4" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-ifblock-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-ifblock-cs-LC5" class="blob-code blob-code-inner js-file-line">  console.log(x); // prints &#39;5&#39;</td>
</tr>
<tr>
<td id="file-ifblock-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-ifblock-cs-LC6" class="blob-code blob-code-inner js-file-line">                  // x is a global scope variable</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/096fc8e846dcea4b8e0eef6bd5783969/raw/4ce0e4dc8d8c0a297b26e0b2b7be42a2b7c7b206/IfBlock.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/096fc8e846dcea4b8e0eef6bd5783969#file-ifblock-cs" class="Link--inTextBlock"><br />
          IfBlock.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>If a variable is defined outside of any function, it&#8217;s assigned to a <strong>global scope</strong>.</p>
<p>However, in JavaScript we also have something called <strong>lexical scoping</strong>. It basically <strong>makes functions declared within another functions having access to the parent function&#8217;s scope</strong>, as the following example presents<strong>:</strong></p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92877551" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-lexicalscoping-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="LexicalScoping.js content, created by dsibinski on 09:06PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="LexicalScoping.js">
<tr>
<td id="file-lexicalscoping-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-lexicalscoping-js-LC1" class="blob-code blob-code-inner js-file-line">  function a() {</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-lexicalscoping-js-LC2" class="blob-code blob-code-inner js-file-line">      var x = &#39;Hello&#39;;</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-lexicalscoping-js-LC3" class="blob-code blob-code-inner js-file-line">      b();</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-lexicalscoping-js-LC4" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-lexicalscoping-js-LC5" class="blob-code blob-code-inner js-file-line">      function b() {</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-lexicalscoping-js-LC6" class="blob-code blob-code-inner js-file-line">          var y = &#39; world!&#39;;</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-lexicalscoping-js-LC7" class="blob-code blob-code-inner js-file-line">          console.log(x + y); </td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-lexicalscoping-js-LC8" class="blob-code blob-code-inner js-file-line">      }</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-lexicalscoping-js-LC9" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-lexicalscoping-js-LC10" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-lexicalscoping-js-LC11" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-lexicalscoping-js-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-lexicalscoping-js-LC12" class="blob-code blob-code-inner js-file-line">  a(); // prints &#39;Hello world!&#39;</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/cae61de9d60e5d23d4db60deb2f0afa7/raw/a242daae4fc84440ffbc6cdeecf048bb1c4207a1/LexicalScoping.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/cae61de9d60e5d23d4db60deb2f0afa7#file-lexicalscoping-js" class="Link--inTextBlock"><br />
          LexicalScoping.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Forming such &#8220;hierarchy&#8221; of functions is also referred to as <strong>scope chain</strong>. You should remember that it refers to the <strong>order in which functions are lexically written, not in which they are called:</strong></p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92877739" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-lexicalscoping_wrong-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="LexicalScoping_wrong.js content, created by dsibinski on 09:17PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="LexicalScoping_wrong.js">
<tr>
<td id="file-lexicalscoping_wrong-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-lexicalscoping_wrong-js-LC1" class="blob-code blob-code-inner js-file-line">  function a() {</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-lexicalscoping_wrong-js-LC2" class="blob-code blob-code-inner js-file-line">      b();</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-lexicalscoping_wrong-js-LC3" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-lexicalscoping_wrong-js-LC4" class="blob-code blob-code-inner js-file-line">      function b() {</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-lexicalscoping_wrong-js-LC5" class="blob-code blob-code-inner js-file-line">          var x = &#39;Hello world!&#39;;</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-lexicalscoping_wrong-js-LC6" class="blob-code blob-code-inner js-file-line">          c();</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-lexicalscoping_wrong-js-LC7" class="blob-code blob-code-inner js-file-line">      }</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-lexicalscoping_wrong-js-LC8" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-lexicalscoping_wrong-js-LC9" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-lexicalscoping_wrong-js-LC10" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-lexicalscoping_wrong-js-LC11" class="blob-code blob-code-inner js-file-line">  function c() {</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-lexicalscoping_wrong-js-LC12" class="blob-code blob-code-inner js-file-line">      console.log(x); // Uncaught ReferenceError: </td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-lexicalscoping_wrong-js-LC13" class="blob-code blob-code-inner js-file-line">                      // x is not defined</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-lexicalscoping_wrong-js-LC14" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-lexicalscoping_wrong-js-LC15" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-lexicalscoping_wrong-js-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-lexicalscoping_wrong-js-LC16" class="blob-code blob-code-inner js-file-line">  a();</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/edf0470e2d6e832298738d41937bbaf7/raw/a93d7b1c2cf952dadd77612c7dc0d22edf8ff499/LexicalScoping_wrong.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/edf0470e2d6e832298738d41937bbaf7#file-lexicalscoping_wrong-js" class="Link--inTextBlock"><br />
          LexicalScoping_wrong.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>As you can see above, as soon as we call <span style="color: #ff6600;">a()</span>, which then calls <span style="color: #ff6600;">b()</span>, within which we define variable <span style="color: #ff6600;">x</span> and then call function <span style="color: #ff6600;">c()</span>, inside of function <span style="color: #ff6600;">c()</span> we don&#8217;t have access to <span style="color: #ff6600;">x</span> variable (<em>x is not defined</em>). Why does it happen even though function <span style="color: #ff6600;">b()</span> in which <span style="color: #ff6600;">x</span> variable is declared called function <span style="color: #ff6600;">c()</span>? Because <strong>function <span style="color: #ff6600;">c()</span> is not lexically written as the child function of <span style="color: #ff6600;">b()</span></strong>. Parent scope for function <span style="color: #ff6600;">c()</span> is always the global scope.</p>
<h1>7. <em>this</em> keyword</h1>
<p>All these scoping rules examined above somehow lead to another confusing concept with <span style="color: #ff6600;">this</span> keyword. As soon as we define a method on some object and try to print the <span style="color: #ff6600;">this</span> keyword in this object&#8217;s method, the result seems reasonable:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92877952" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-this_1-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="this_1.js content, created by dsibinski on 09:33PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="this_1.js">
<tr>
<td id="file-this_1-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-this_1-js-LC1" class="blob-code blob-code-inner js-file-line">  var blogger = {</td>
</tr>
<tr>
<td id="file-this_1-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-this_1-js-LC2" class="blob-code blob-code-inner js-file-line">      name: &#39;Dawid&#39;,</td>
</tr>
<tr>
<td id="file-this_1-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-this_1-js-LC3" class="blob-code blob-code-inner js-file-line">      website: &#39;codejourney.net&#39;,</td>
</tr>
<tr>
<td id="file-this_1-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-this_1-js-LC4" class="blob-code blob-code-inner js-file-line">      aboutMe: function(){</td>
</tr>
<tr>
<td id="file-this_1-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-this_1-js-LC5" class="blob-code blob-code-inner js-file-line">          console.log(this);</td>
</tr>
<tr>
<td id="file-this_1-js-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-this_1-js-LC6" class="blob-code blob-code-inner js-file-line">      }</td>
</tr>
<tr>
<td id="file-this_1-js-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-this_1-js-LC7" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
<tr>
<td id="file-this_1-js-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-this_1-js-LC8" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-this_1-js-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-this_1-js-LC9" class="blob-code blob-code-inner js-file-line">  blogger.aboutMe(); // prints: {name: &quot;Dawid&quot;, website: &quot;codejourney.net&quot;, aboutMe: ƒ}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/aa92f36cec8f93026b25702bf9798cd7/raw/37b18ae0ceddc5194a838ff7593c071ee29d06f4/this_1.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/aa92f36cec8f93026b25702bf9798cd7#file-this_1-js" class="Link--inTextBlock"><br />
          this_1.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>However, if we just define an inner function in our method and try to print <span style="color: #ff6600;">this</span> keyword inside it, weird stuff happens:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92877991" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-this_2-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="this_2.js content, created by dsibinski on 09:37PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="this_2.js">
<tr>
<td id="file-this_2-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-this_2-js-LC1" class="blob-code blob-code-inner js-file-line">  var blogger = {</td>
</tr>
<tr>
<td id="file-this_2-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-this_2-js-LC2" class="blob-code blob-code-inner js-file-line">      name: &#39;Dawid&#39;,</td>
</tr>
<tr>
<td id="file-this_2-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-this_2-js-LC3" class="blob-code blob-code-inner js-file-line">      website: &#39;codejourney.net&#39;,</td>
</tr>
<tr>
<td id="file-this_2-js-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-this_2-js-LC4" class="blob-code blob-code-inner js-file-line">      aboutMe: function(){</td>
</tr>
<tr>
<td id="file-this_2-js-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-this_2-js-LC5" class="blob-code blob-code-inner js-file-line">          function innerFunc(){</td>
</tr>
<tr>
<td id="file-this_2-js-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-this_2-js-LC6" class="blob-code blob-code-inner js-file-line">              console.log(this);</td>
</tr>
<tr>
<td id="file-this_2-js-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-this_2-js-LC7" class="blob-code blob-code-inner js-file-line">          }</td>
</tr>
<tr>
<td id="file-this_2-js-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-this_2-js-LC8" class="blob-code blob-code-inner js-file-line">          innerFunc();   </td>
</tr>
<tr>
<td id="file-this_2-js-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-this_2-js-LC9" class="blob-code blob-code-inner js-file-line">      }</td>
</tr>
<tr>
<td id="file-this_2-js-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-this_2-js-LC10" class="blob-code blob-code-inner js-file-line">  }</td>
</tr>
<tr>
<td id="file-this_2-js-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-this_2-js-LC11" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-this_2-js-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-this_2-js-LC12" class="blob-code blob-code-inner js-file-line">  blogger.aboutMe(); // (!!!) prints: Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/4219bf76cbdbf55596ac2fb03ab9f49e/raw/c669e8d2d3a687d330ed1377916c5d2954dfe0d7/this_2.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/4219bf76cbdbf55596ac2fb03ab9f49e#file-this_2-js" class="Link--inTextBlock"><br />
          this_2.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>As you can see, the <span style="color: #ff6600;">this</span> keyword in the <span style="color: #ff6600;">innerFunc()</span> function doesn&#8217;t point to the <span style="color: #ff6600;">blogger</span> object anymore. Instead it points to a <span style="color: #ff6600;">Window</span> object, which represents the browser&#8217;s window (<span style="color: #ff6600;">this</span> used in the global scope points to it).</p>
<p>This happens because <strong>in a regular function the <span style="color: #ff6600;">this</span> keyword points to the global object </strong>(<span style="color: #ff6600;">Window</span> object in our case). <span style="color: #ff6600;">innerFunc()</span> is here considered as a regular function, not as an object&#8217;s method. This behavior may be confusing and is often a subject of dispute in JavaScript community.</p>
<h1>8. <em>sort()</em> function</h1>
<p>You&#8217;re a newbie JS developer and you&#8217;d like to sort an array of numbers. You found a <span style="color: #ff6600;">sort()</span> method on the array, so why not to use it? You write such code:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92878145" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-sort_numbers-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="sort_numbers.js content, created by dsibinski on 09:48PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="sort_numbers.js">
<tr>
<td id="file-sort_numbers-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-sort_numbers-js-LC1" class="blob-code blob-code-inner js-file-line">  var numbers = [40, 20, 15, -5, 7];</td>
</tr>
<tr>
<td id="file-sort_numbers-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-sort_numbers-js-LC2" class="blob-code blob-code-inner js-file-line">  console.log(numbers.sort());</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/5714737927db7c97af0de9a26d994298/raw/60baaa4c93ce3b0cec31593ab4cb9d97934291ae/sort_numbers.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/5714737927db7c97af0de9a26d994298#file-sort_numbers-js" class="Link--inTextBlock"><br />
          sort_numbers.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>What do you expect to be logged in the console? Let&#8217;s see:</p>
<blockquote>
<p>(5) [-5, 15, 20, 40, 7]</p>
</blockquote>
<p>Nice! What happens here? 7 is greater than 40? Well, yes <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> It works as expected, because the <span style="color: #ff6600;">sort()</span> function sorts array&#8217;s elements as strings and alphabetically. Therefore, if our array contains numbers, the final order depends on the Unicode representations values of each number.</p>
<p>Fortunately, there are <a href="https://www.w3schools.com/js/js_array_sort.asp" target="_blank" rel="noopener">ways to use <span style="color: #ff6600;">sort()</span> function correctly for numbers</a>, but this functionality is still confusing for new JavaScript developers.</p>
<h1>9. Prototypes</h1>
<p>Every object in JavaScript has a <span style="color: #ff6600;">__proto__</span> property. We can access it directly on our object or by using <span style="color: #ff6600;">Object.getPrototypeOf()</span> method. In both cases, having created an empty object and printing its prototype to the console like that:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92878559" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-prorotype-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Prorotype.js content, created by dsibinski on 10:17PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Prorotype.js">
<tr>
<td id="file-prorotype-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-prorotype-js-LC1" class="blob-code blob-code-inner js-file-line">  let x = {}</td>
</tr>
<tr>
<td id="file-prorotype-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-prorotype-js-LC2" class="blob-code blob-code-inner js-file-line">  console.log(x.__proto__);</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/1f655703669aa751860afc22e534041a/raw/a7f653502ca84980dde87fc455ae7f4a766c7fb6/Prorotype.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/1f655703669aa751860afc22e534041a#file-prorotype-js" class="Link--inTextBlock"><br />
          Prorotype.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>produces the same output:</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="3067" data-permalink="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/prototype/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/Prototype.png?fit=678%2C223&amp;ssl=1" data-orig-size="678,223" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Prototype" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/Prototype.png?fit=300%2C99&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/Prototype.png?fit=678%2C223&amp;ssl=1" class="size-full wp-image-3067 aligncenter" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/Prototype.png?resize=678%2C223&#038;ssl=1" alt="" width="678" height="223" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/Prototype.png?w=678&amp;ssl=1 678w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/Prototype.png?resize=300%2C99&amp;ssl=1 300w" sizes="auto, (max-width: 678px) 100vw, 678px" /></p>
<p>On the top of objects hierarchy, there&#8217;s <span style="color: #ff6600;">Object</span>. Because of that, our <span style="color: #ff6600;">x</span> object is an instance of <span style="color: #ff6600;">Object</span>. That&#8217;s why the prototype of our <span style="color: #ff6600;">x</span> object is actually the <span style="color: #ff6600;">Object.prototype</span>, which contains some common methods we can use (like <span style="color: #ff6600;">toString()</span>).</p>
<p>If we try to access <span style="color: #ff6600;">Object.prototype.__proto__<span style="color: #000000;">, we&#8217;ll see that is contains <em>null</em>.</span></span></p>
<p><strong>Prototypes in JavaScript are mainly used to provide some connection between two or more objects</strong> (like exposing some common methods).</p>
<p>All this &#8220;hierarchy&#8221; of prototypes is used to search for fields and methods of objects: the JS engine first searches in the object itself and if it cannot find particular property, it looks in this object&#8217;s prototype, and then in its prototype until it finds the first matching occurrence. As soon as it reaches <em>null </em>in the <span style="color: #ff6600;">Object.prototype.__proto__ <span style="color: #000000;">it means that the prototypes chain has ended and <em>undefined</em> is returned.</p>
<p></span></span></p>
<p>This kind of prototype inheritance is different than class inheritance, so it&#8217;s often confusing for developers willing to learn JavaScript. Actually, I could write a separate post on this, so if you&#8217;re interested you can read more <a href="https://www.digitalocean.com/community/tutorials/understanding-prototypes-and-inheritance-in-javascript" target="_blank" rel="noopener">for instance here</a>.</p>
<h1>10. IIFE</h1>
<p>IIFE is an acronym for <strong>Immediately-Invoked Function Expression</strong>. So what is it and how can it be useful?</p>
<p>Going straight into the code, IIFE can be written as follows:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist92879015" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-iife-js" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-javascript  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="IIFE.js content, created by dsibinski on 10:59PM on November 06, 2018."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="IIFE.js">
<tr>
<td id="file-iife-js-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-iife-js-LC1" class="blob-code blob-code-inner js-file-line">  (function() {</td>
</tr>
<tr>
<td id="file-iife-js-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-iife-js-LC2" class="blob-code blob-code-inner js-file-line">      console.log(&#39;Hey!&#39;);</td>
</tr>
<tr>
<td id="file-iife-js-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-iife-js-LC3" class="blob-code blob-code-inner js-file-line">  })()</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/e7441e98355c00a073864e1c65ba5733/raw/5626f46947f63dd141bf0368aa0dd44d811742a3/IIFE.js" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/e7441e98355c00a073864e1c65ba5733#file-iife-js" class="Link--inTextBlock"><br />
          IIFE.js<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>As a result of executing such script in a web browser, we&#8217;ll see the following console output:</p>
<blockquote>
<p>Hey!</p>
</blockquote>
<p>How does it work? We neither declared a function&#8217;s name nor called it, right? Actually, we did <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>There are two unusual parts of IIFE implementation: outer parentheses in which we &#8220;wrapped&#8221; our function and &#8220;empty&#8221; parentheses together at its end.</p>
<p>Let&#8217;s start by &#8220;wrapping parentheses&#8221;:</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="3068" data-permalink="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/parentheses_wrapping/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_wrapping.png?fit=315%2C90&amp;ssl=1" data-orig-size="315,90" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="parentheses_wrapping" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_wrapping.png?fit=300%2C86&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_wrapping.png?fit=315%2C90&amp;ssl=1" class="aligncenter wp-image-3068 size-full" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_wrapping.png?resize=315%2C90&#038;ssl=1" alt="" width="315" height="90" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_wrapping.png?w=315&amp;ssl=1 315w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_wrapping.png?resize=300%2C86&amp;ssl=1 300w" sizes="auto, (max-width: 315px) 100vw, 315px" /></p>
<p>Why do we declare them? Because if you don&#8217;t, JavaScript parser will give you an error:</p>
<blockquote>
<p>script.js:1 Uncaught SyntaxError: Unexpected token (</p>
</blockquote>
<p>In such case it thinks that we&#8217;re writing a function declaration and we forgot about providing the function&#8217;s name after the <em>function</em> keyword. Wrapping this into parentheses (&#8230;..) tells the parser that we&#8217;re declaring a <strong>function expression, not a declaration</strong>. After doing this, there are no errors anymore.</p>
<p>The second important part is the &#8220;empty&#8221; parentheses () just after the function expression:</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="3069" data-permalink="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/parentheses_end/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_end.png?fit=315%2C90&amp;ssl=1" data-orig-size="315,90" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="parentheses_end" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_end.png?fit=300%2C86&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_end.png?fit=315%2C90&amp;ssl=1" class="aligncenter wp-image-3069 size-full" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_end.png?resize=315%2C90&#038;ssl=1" alt="" width="315" height="90" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_end.png?w=315&amp;ssl=1 315w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/parentheses_end.png?resize=300%2C86&amp;ssl=1 300w" sizes="auto, (max-width: 315px) 100vw, 315px" /></p>
<p>As you know, () allows to call the function. That&#8217;s exactly what we do here here &#8211; our function is immediately executed.</p>
<p>Why would we need such construct as IIFE? Developers say that it doesn&#8217;t &#8220;pollute&#8221; the global scope. In effect, we are not leaving the function somewhere after being executed, so no one else can call it accidentally from another place. What&#8217;s more, IIFE is even considered a design pattern.</p>
<p>You should know that such construct exists even only to know what it does seeing it in someone else&#8217;s code. If you want to know more about its use cases, I recommend reading <a href="https://blog.mariusschulz.com/2016/01/19/use-cases-for-javascripts-iifes" target="_blank" rel="noopener">this article</a>.</p>
<h1>Summary</h1>
<p>In this post we&#8217;ve gone through some of the most shocking features of JavaScript language for C# developers. Most of them are confusing for programmers coming from statically-typed and class-based languages and can bring a lot of issues in the beginning of their JavaScript journey.</p>
<p>Just remember that these are the JavaScript&#8217;s language features, not its issues or bugs. That&#8217;s just how it is, but it cannot be said that JavaScript is worse or better than other languages like Java or C#.</p>
<p>The list for sure is not exhaustive, so <span style="text-decoration: underline;">if you have anything to add or correct don&#8217;t hesitate to leave a comment</span>! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>For the end, I&#8217;ll just leave you with the following meme-but-true (thanks <a href="https://twitter.com/YouennBouglouan" target="_blank" rel="noopener">Youenn</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> ):</p>
<p><figure id="attachment_3041" aria-describedby="caption-attachment-3041" style="width: 700px" class="wp-caption aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="3041" data-permalink="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/thanks_javascript/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/thanks_javascript.png?fit=700%2C793&amp;ssl=1" data-orig-size="700,793" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="thanks_javascript" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/thanks_javascript.png?fit=265%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/thanks_javascript.png?fit=700%2C793&amp;ssl=1" class="wp-image-3041 size-full" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/thanks_javascript.png?resize=700%2C793&#038;ssl=1" alt="" width="700" height="793" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/thanks_javascript.png?w=700&amp;ssl=1 700w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/11/thanks_javascript.png?resize=265%2C300&amp;ssl=1 265w" sizes="auto, (max-width: 700px) 100vw, 700px" /><figcaption id="caption-attachment-3041" class="wp-caption-text"><a href="http://devhumor.com/media/thanks-brendan-for-giving-us-the-javascript" target="_blank" rel="noopener">Source</a></figcaption></figure></p>
<p>Stay tuned!</p>
<p><script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.9 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-5" class="mc4wp-form mc4wp-form-2612" method="post" data-id="2612" data-name="Download a free guide form" ><div class="mc4wp-form-fields"><table bgcolor="#f2f6f5"><tr><td> <p><p>
    <label>
<h1 style="">
  <center>GET A FREE GUIDE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f381.png" alt="🎁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></center>
      </h1>        
      <h2 style="font-family: Helvetica">
        <center>16 STEPS TO BECOME <br/>.NET FULL STACK WEB DEVELOPER </br>IN 2025</center>
      </h2>
</p>
  <center><div>
	<input type="email" name="EMAIL" placeholder="Email address" required />
    <p>
    <input type="text" name="FNAME" placeholder="Your name"
    required="">
  </p>
  </div>
    <center>

	<center><input type="submit" value="DOWNLOAD THE FREE GUIDE" style="color: #7b1fa2; font-weight:bold; font-size: 20px" /></center>
<p style="font-size: 12px; font-style: italic;">After you sign up, I may be sending you some emails with additional free content from time to time.
<br/>No spam, only awesome stuff</p>
</p></td></tr></table>

</div><label style="display: none !important;">Leave this field empty if you're human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1767004803" /><input type="hidden" name="_mc4wp_form_id" value="2612" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-5" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin --></p>
<p>&nbsp;</p>
<p>The post <a href="https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/">10 Most Shocking JavaScript Features for C# Developers</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/10-most-shocking-javascript-features-for-csharp-developers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2999</post-id>	</item>
	</channel>
</rss>
