<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Udayan Maurya</title>
    <description>The latest articles on DEV Community by Udayan Maurya (@udayanmaurya).</description>
    <link>https://dev.to/udayanmaurya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F658654%2Fd33b639d-b61c-4ed2-a2be-a8c2db8cb6b0.jpeg</url>
      <title>DEV Community: Udayan Maurya</title>
      <link>https://dev.to/udayanmaurya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/udayanmaurya"/>
    <language>en</language>
    <item>
      <title>Engineering for Product market fit</title>
      <dc:creator>Udayan Maurya</dc:creator>
      <pubDate>Mon, 13 May 2024 13:57:27 +0000</pubDate>
      <link>https://dev.to/udayanmaurya/engineering-for-product-market-fit-47a8</link>
      <guid>https://dev.to/udayanmaurya/engineering-for-product-market-fit-47a8</guid>
      <description>&lt;p&gt;Product market fit is the &lt;strong&gt;&lt;em&gt;only&lt;/em&gt;&lt;/strong&gt; north start that you early stage startup should be chasing. Engineer your product to support you in that journey. &lt;/p&gt;

&lt;p&gt;Do not get in your own way with overly complex engineering. Keep it simple!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Journey of product market fit
&lt;/h2&gt;

&lt;h3&gt;
  
  
  a) Research
&lt;/h3&gt;

&lt;p&gt;Market is the most important component of product market fit journey. Market is alive even if your product is not, vice versa is &lt;em&gt;not&lt;/em&gt; true. While building your product you'll research the market throughly. Come up with ideas and hypotheses to serve some unserved need and keep doing it over and over again until a &lt;strong&gt;fit&lt;/strong&gt; is found.&lt;/p&gt;

&lt;h3&gt;
  
  
  b) Experiment
&lt;/h3&gt;

&lt;p&gt;This journey involves a lot of experimentation and hypothesis testing. Your rounds of user interviews, sales calls and analyses will result in product specifications. These product specifications are your bets on things that can close the identified gap in the market.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F99mdv3opy3c5gweykc9e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F99mdv3opy3c5gweykc9e.png" alt="Product spec chasing market" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  c) Grind
&lt;/h3&gt;

&lt;p&gt;You then need to develop this product and actually test your theories in the market. On real user testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your hypotheses can be proven wrong&lt;/li&gt;
&lt;li&gt;You might need to make adjustments to product&lt;/li&gt;
&lt;li&gt;Try a new product direction&lt;/li&gt;
&lt;li&gt;Seek an entirely new market&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The grind is inevitable and it is costly. Unfortunately, you can make your life harder by doing &lt;em&gt;unfit engineering&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unfit engineering&lt;/strong&gt;: Engineering practices that are not suitable for this grind.&lt;/p&gt;

&lt;p&gt;In a nutshell job of engineering is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Develop on product specifications, fast&lt;/li&gt;
&lt;li&gt;Change directions quickly&lt;/li&gt;
&lt;li&gt;Let go of artifacts completely, if required&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. How to engineer for product market fit
&lt;/h2&gt;

&lt;p&gt;Objective for engineering is to create a software to test your product theories quickly. &lt;br&gt;
Objective for engineering is &lt;strong&gt;not&lt;/strong&gt; to support millions of imaginary users.&lt;/p&gt;

&lt;p&gt;As your understanding of problem space changes, the shape of your product will change drastically. This will result in running many experiments and iterations of developing product and testing with users.&lt;/p&gt;

&lt;p&gt;Your aim, through these iterations, should be to test out your hypotheses about market as precisely as possible.&lt;/p&gt;

&lt;p&gt;When market rejects your product you should have conclusive answer to following questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why my product failed?&lt;/li&gt;
&lt;li&gt;What should be my next move to improve things?&lt;/li&gt;
&lt;li&gt;What does not work in this market?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should have business answers to all these questions and none of the answers point to the flaws in your engineering. Using market feedback to learn engineering practices is prohibitively expensive!&lt;/p&gt;

&lt;p&gt;Therefore, it is crucial to map product specifications for each iteration precisely to code. So that product failures reveal the lack of understanding of market rather than poor engineering.&lt;/p&gt;

&lt;p&gt;You should not end up in a position that your product is so bad that user could not use it at all. Leaving you without answers: whether your insight of market is right or if user could try the product to produce reliable data points.&lt;/p&gt;
&lt;h3&gt;
  
  
  Write throw away code
&lt;/h3&gt;

&lt;p&gt;Do not carry any tech debt between product iterations. Write throw away functional code that is loosely coupled such that it is easy to delete large parts of code. If you do not remove unused code between iterations it'll get harder and harder to map new product specifications to code, which greatly diminishes the quality of your product testing.&lt;/p&gt;

&lt;p&gt;Steer clear of object oriented programming. OOP has an inherent disadvantage at this stage of product. OOP requires developers to create mappings and relations within application data. However, it cannot be adequately done because real relations in the problem space are yet to be discovered.&lt;/p&gt;

&lt;p&gt;Avoid creating complex data mappings in your data graphs and API contracts. These inaccurate mappings get in the way when you want to change things quickly and many of previously created relationships are proven incorrect. &lt;/p&gt;

&lt;p&gt;When stuck with bad data mappings or inheritance structures, developers start to cut corners and start to project their code's poor representation of world back on to the world, which results in disastrous user testing and inconclusive understanding of problem space.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Bonus: Read &lt;a href="https://kentcdodds.com/blog/aha-programming" rel="noopener noreferrer"&gt;Avoid Hasty Abstractions&lt;/a&gt; by Kent C. Dodds.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Golden rule: Engineering chases Product Spec, and Product Spec chases Market&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xmfjq9qxcxmlvjay74j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xmfjq9qxcxmlvjay74j.png" alt="Engineering chase Product spec chase market" width="800" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This relationship should never be inverted, otherwise you are in trouble!&lt;/p&gt;
&lt;h3&gt;
  
  
  Use commonplace technology
&lt;/h3&gt;

&lt;p&gt;New technology is unproven and adds needless uncertainty in your quest to product market fit. Your success should not hinge on whether a new technology or framework succeeds, it is simply foolish. &lt;/p&gt;

&lt;p&gt;Using commonplace technology is awesome! You will not have to work on unproven things to make things barely work. Librating your attention span to focus just on finding your product market fit.&lt;/p&gt;

&lt;p&gt;Additionally, you'll reap following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Support: 

&lt;ul&gt;
&lt;li&gt;Plethora of libraries and packages to choose from&lt;/li&gt;
&lt;li&gt;Your questions have many hits on StackOverflow/GPTs&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Access to large talent pool for hiring&lt;/li&gt;
&lt;li&gt;Standardized engineering patterns to follow&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Follow best practices
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Your job as an engineer is write maintainable code that serves business needs.&lt;/li&gt;
&lt;li&gt;Your job is &lt;strong&gt;not&lt;/strong&gt; to use all the features of programming languages. Many features tend to be bad features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhd6k2gy4d8olbc60lui.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhd6k2gy4d8olbc60lui.jpeg" alt="Good parts small subset of all parts" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Q. Why do bad parts exist in programming languages and frameworks?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A. Because they are created by humans!&lt;/em&gt; Humans tend to do mistakes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create features that do not deliver on promise&lt;/li&gt;
&lt;li&gt;Do not solve any real problem&lt;/li&gt;
&lt;li&gt;Lead to bad programming patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Language builders go through their own journey of product market fit. In this journey, just like yours, they create messes.&lt;/p&gt;

&lt;p&gt;Unfortunately, none of the bad parts can be removed due to backwards compatibility - all the code written using bad parts need to be supported.&lt;/p&gt;

&lt;p&gt;Fortunately, you can choose not to use any of the bad parts and steer clear of the mess and focus on what really matters - your quest of product market fit.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Advice for good engineering
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a) Constantly learn&lt;/strong&gt; - In 2008, Douglas Crockford wrote "Javascript - The Good Parts", which is a valuable resource. However, as languages and frameworks grow they continue to add several new bad parts. The onus is on you to keep separating the good parts from the bad ones and only use the good parts in you business software. &lt;/p&gt;

&lt;p&gt;To do so, keep learning from good/authentic resources. Focus on finding out how things actually work and use those principles.&lt;/p&gt;

&lt;p&gt;Use side projects to experiment with new technologies to identify and validate rising stars, but only use proven ones in software of business significance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b) Do not do speculative coding&lt;/strong&gt; - Do not abstract because you may need it in at other time. Abstract functionality only when 2nd usage is encountered. You will avoid writing extra code and your abstractions will be better mapping of real world. &lt;br&gt;
Do not create columns in database because you may need them some day. Spoiler alert - you won't!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;c) Refactor your code&lt;/strong&gt; - After any sprint, ensure that you have mapped real world to code. Additionally, ensure you are not projecting relations of your code or data on to the world - that is you are not trying to fit market to your product. &lt;/p&gt;

&lt;p&gt;Clean the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for good mapping of world/problem-space to the code.&lt;/li&gt;
&lt;li&gt;to ensure you are using only good parts of languages and frameworks.&lt;/li&gt;
&lt;li&gt;so that it is easy read, because you'll need to remove a lot of it in for the next iteration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall simplicity is your friend when you want to move fast. Therefore, slow down and simplify passionately before starting the next sprint!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;d) Readable code is better than Optimal code&lt;/strong&gt; - In programming world it is a common advice that readable code is better than optimal code. Though for an early stage startup this relationship is super amplified. Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Readable/maintainable code &amp;gt;&amp;gt;&amp;gt;&amp;gt; Optimal code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no point in optimizing a code that will not last more than one week. Therefore, optimization efforts are waste of precious resources - time and energy.&lt;/p&gt;

&lt;p&gt;Readable and loosely coupled code provides you best leverage on your scarce resources. You can iterate fast and stay focused on what really matters - product market fit!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>startup</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Good parts for good business</title>
      <dc:creator>Udayan Maurya</dc:creator>
      <pubDate>Sun, 05 May 2024 23:49:23 +0000</pubDate>
      <link>https://dev.to/udayanmaurya/good-parts-for-good-business-5a4p</link>
      <guid>https://dev.to/udayanmaurya/good-parts-for-good-business-5a4p</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Why bad parts exist?&lt;/li&gt;
&lt;li&gt;Bad psychology:&lt;/li&gt;
&lt;li&gt;Use all features&lt;/li&gt;
&lt;li&gt;Feel swag in writing cryptic/magic code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Change title to: &lt;br&gt;
Engineering for Product market fit&lt;/p&gt;

&lt;p&gt;&lt;a href="https://excalidraw.com/#json=4uUA0hRxIpvuSSymw_S5v,nnQBwwkFyUBy4KfDHb3FFg" rel="noopener noreferrer"&gt;https://excalidraw.com/#json=4uUA0hRxIpvuSSymw_S5v,nnQBwwkFyUBy4KfDHb3FFg&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Typescript Generics - What are they doing</title>
      <dc:creator>Udayan Maurya</dc:creator>
      <pubDate>Mon, 13 Nov 2023 02:56:25 +0000</pubDate>
      <link>https://dev.to/udayanmaurya/typescript-generics-what-are-they-doing-1j6i</link>
      <guid>https://dev.to/udayanmaurya/typescript-generics-what-are-they-doing-1j6i</guid>
      <description>&lt;p&gt;Generics are usually considered most confusing part of Typescript. In this post I'll try to explain intuition behind generics and hopefully make them easier to use!&lt;/p&gt;

&lt;h2&gt;
  
  
  Common beginner confusions
&lt;/h2&gt;

&lt;p&gt;(1) Generics will do some type interpretation at runtime&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No&lt;/strong&gt; - Typescript is only available at compile time and not runtime!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(2) Generics map to input arguments&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No&lt;/strong&gt; - Generic types can map to any input arguments as designed by developer.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;someFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;U&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Type of a -&amp;gt; T and type of b -&amp;gt; U -- NO!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(3) Generics make my code look ugly 😖&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No magic bullet to solve this. However, if you leverage typescript's inference properly you can minimize this problem.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What problem generics solve
&lt;/h2&gt;

&lt;p&gt;Let's say we have an identity function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;identityFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we want:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Function should accept any type of input&lt;/li&gt;
&lt;li&gt;Function's return type should match input's type&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How can we type this function to achieve both objectives?&lt;/p&gt;

&lt;h4&gt;
  
  
  Naive solution
&lt;/h4&gt;

&lt;p&gt;Statically type the input to be &lt;code&gt;unknown&lt;/code&gt; type. &lt;code&gt;unknown&lt;/code&gt; is top type in typescript's type system and every other type can be assigned to &lt;code&gt;unknown&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;identityFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this approach fails to achieve second objective that is type of function's return value is always &lt;code&gt;unknown&lt;/code&gt; regardless of the type of input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;identityFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//  ^? unknown&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;identityFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;785&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//  ^? unknown&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So ideally we want the type of function's input to be inferred when we use the function.&lt;/p&gt;

&lt;p&gt;Typescript has such inference mechanism as we use it all the time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;strBasic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//  ^? string&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;numBasic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7856&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//  ^? number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there was a technique to leverage this Typescript's inference mechanism to infer the type of a function's input at usage we can solve our problem.&lt;/p&gt;

&lt;p&gt;And that technique is &lt;strong&gt;Generic types&lt;/strong&gt; 🎉&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generic types deploys typescript's inference mechanism to infer type of a function's input at usage 🔥&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Generic definition and breakdown
&lt;/h2&gt;

&lt;p&gt;So if we redefine the identity function using generic type it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;identityFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Generic definition
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxxjp6zki213ue15kw02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxxjp6zki213ue15kw02.png" alt="generic definition" width="800" height="52"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Generic function will infer the type of input at usage. But we still need a way to reference that type in our function body while defining the function. &lt;br&gt;
This type cannot be any static type such as &lt;code&gt;any&lt;/code&gt; or &lt;code&gt;unknown&lt;/code&gt; it needs to be dynamically inferred. Therefore, we need a special typescript variable &lt;code&gt;T&lt;/code&gt; to store this dynamic type.&lt;/p&gt;
&lt;h4&gt;
  
  
  Generic assignment and inference
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkl40qi4giso383wglwm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkl40qi4giso383wglwm.png" alt="Generic inference" width="800" height="40"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like static type assignment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="c1"&gt;// v1 is assigned type string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we use &lt;code&gt;:&lt;/code&gt; to assign type of input.&lt;/p&gt;

&lt;p&gt;Additionally, in case of generics, type of input gets inferred at this step and is stored in variable &lt;code&gt;T&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Type inference only happens at the level of arguments within parentheses &lt;code&gt;()&lt;/code&gt; and no where else in function body.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image by &lt;a href="https://pixabay.com/users/qimono-1962238/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=2125169" rel="noopener noreferrer"&gt;Arek Socha&lt;/a&gt; from &lt;a href="https://pixabay.com//?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=2125169" rel="noopener noreferrer"&gt;Pixabay&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
    </item>
    <item>
      <title>How to structure Typescript project</title>
      <dc:creator>Udayan Maurya</dc:creator>
      <pubDate>Sat, 18 Feb 2023 03:37:08 +0000</pubDate>
      <link>https://dev.to/udayanmaurya/how-to-ts-for-client-side-application-36ig</link>
      <guid>https://dev.to/udayanmaurya/how-to-ts-for-client-side-application-36ig</guid>
      <description>&lt;p&gt;Typescript is an amazing tool to provide static type safety to you large Javascript applications. Type definitions can act as documentation for new team members, provide useful hints about what can break the software, and have an addictive code completion!&lt;/p&gt;

&lt;p&gt;However, Typescript is no magic wand and to derive all the aforementioned benefits Typescript project should be structured properly. Otherwise, Typescript can be nightmarish. More like: Where is this TS error coming from !?, why is this not assignable!!, man TS is a headache this would be shipped by now with just JS 🤦&lt;/p&gt;

&lt;p&gt;In this blog I'll describe how to structure Typescript project for a RESTful client-side web application for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Readable and maintainable code&lt;/li&gt;
&lt;li&gt;Pivotable codebase&lt;/li&gt;
&lt;li&gt;True type safety!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;We will use a sample HR application to discuss all the examples.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application has 2 pages&lt;/li&gt;
&lt;li&gt;Page 1: List of all employees 
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1nfscasf3eo928pvnai3.png" alt="Employees list page" width="572" height="608"&gt;

&lt;ul&gt;
&lt;li&gt;API to fetch the data: GET &lt;code&gt;/employees&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Response schema
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;department&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Page 2: Employee profile details page &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhrcm0x2rnrzwl5wzxl49.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhrcm0x2rnrzwl5wzxl49.png" alt="Employee Profile page " width="654" height="696"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API to fetch the data: GET &lt;code&gt;/employees/{empId}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Response schema
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;department&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;emailAddress&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common Typescript problems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Problem 1: Schema mapping
&lt;/h3&gt;

&lt;p&gt;Unfortunately, many large projects suffer from this problem.&lt;/p&gt;

&lt;p&gt;In a client side application it is common to describe the response schema as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;EmployeeShortResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;department&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Bad: Equivalent to NoSql schema design&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;EmployeeLongResponse&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;EmployeeShortResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;emailAddress&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;employeesAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;EmployeeShortResponse&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/employees&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;employeesDetailsAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;EmployeeLongResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/employees/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Problems with this approach
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Extending API responses results in a document DB like schema design of the application data.&lt;/li&gt;
&lt;li&gt;The schema does not provide much value on client side.&lt;/li&gt;
&lt;li&gt;Schema creates synthetic relationship between independent REST APIs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For instance if &lt;code&gt;employeesAPI&lt;/code&gt; is updated to return one more property: &lt;code&gt;region&lt;/code&gt;.&lt;br&gt;
The &lt;code&gt;EmployeeShortResponse&lt;/code&gt; will update like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;EmployeeShortResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;department&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This in turn will automatically update the response type of &lt;code&gt;employeesDetailsAPI&lt;/code&gt; while there is no guarantee that response of &lt;code&gt;employeesDetailsAPI&lt;/code&gt; has changed 😳&lt;/p&gt;

&lt;p&gt;Mapping relations of application data on client side results in confusing, hard to track and unhelpful type errors. Front end application has no means to enforce the relations in application data. For best separation of concerns application data relations should be managed only at one place in the system: &lt;em&gt;database of the application&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 2: Expanding API response type beyond API
&lt;/h3&gt;

&lt;p&gt;Let's say, in our example, we have a component to display "Favorite Pokemon" defined like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// EmployeeLongResponse defined as in Problem 1&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;FavoritePokemonProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;EmployeeLongResponse&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;favoritePokemon&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FavoritePokemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;FavoritePokemonProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Favorite&lt;/span&gt; &lt;span class="na"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example usage:&lt;/span&gt;
&lt;span class="c1"&gt;// apiResponse.favoritePokemon = 'pikachu'&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FavoritePokemon&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But really, who has only one favorite pokemon? So the application requirements change to return list of favorite pokemons in API response for GET &lt;code&gt;/employees/{empId}&lt;/code&gt;. Response schema updates like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;department&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;emailAddress&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
 &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example usage:&lt;/span&gt;
&lt;span class="c1"&gt;// New apiResponse.favoritePokemon = ['pikachu', 'squirtle']&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FavoritePokemon&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To this change &lt;code&gt;FavoritePokemon&lt;/code&gt; component will not throw any type error. However, at runtime &lt;code&gt;FavoritePokemon&lt;/code&gt; will render something like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvy81n7s9jfbtpbzpvo3t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvy81n7s9jfbtpbzpvo3t.png" alt="pikachusquirtle" width="514" height="52"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that is a shock no one wants ⚡💦😵‍💫&lt;/p&gt;

&lt;h3&gt;
  
  
  How to structure Typescript code
&lt;/h3&gt;

&lt;p&gt;Let's first breakdown the purpose of client side web application. Client side application needs to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Receive data from server&lt;/li&gt;
&lt;li&gt;Consume the data and display meaningful UI&lt;/li&gt;
&lt;li&gt;Manage state of user interactions &lt;/li&gt;
&lt;li&gt;Capture permanent changes to application data and send it to server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhl48uj9pg4cynn06xgr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhl48uj9pg4cynn06xgr.png" alt="Machine design" width="800" height="684"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the purpose of typescript is to &lt;strong&gt;establish a contract in the program which determines the set of values that can be assigned to a variable&lt;/strong&gt;. PS highly recommended read &lt;a href="https://ivov.dev/notes/typescript-and-set-theory" rel="noopener noreferrer"&gt;TypeScript and Set Theory&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Most important thing, from code design perspective, is to identify what are appropriate places to establish this contract.&lt;/p&gt;

&lt;p&gt;Few principles to keep in mind to get most out of typescript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follow the flow of data across application&lt;/li&gt;
&lt;li&gt;Keep isolated pisces of code isolated&lt;/li&gt;
&lt;li&gt;Typescript helps in gluing the isolated pisces, that is inform users when they try to flow data across incompatible pisces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnnx8hfiwdul3zr36u9jp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnnx8hfiwdul3zr36u9jp.png" alt="Types around machine" width="800" height="611"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 1: [Solution] No NoSQL on Frontend
&lt;/h3&gt;

&lt;p&gt;Through typescript code we should &lt;strong&gt;not&lt;/strong&gt; emphasize relationships in application data. REST APIs are meant to be independent and we should keep them that way.&lt;br&gt;
Following this principle the example of HR application will update like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;EmployeeShortResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;department&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Redundantly define all response types!&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;EmployeeLongResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;department&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;emailAddress&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;employeesAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;EmployeeShortResponse&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/employees&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;employeesDetailsAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;EmployeeLongResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/employees/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now there won't be any surprises in working with response of one API while other API's responses may change. Though this approach involves redundant typing, it adheres to the purpose of client side application. Generally, redundancy in code is not a bad thing if it results in more useful abstractions. Refer &lt;a href="https://kentcdodds.com/blog/aha-programming" rel="noopener noreferrer"&gt;AHA Programming&lt;/a&gt; by &lt;a href="https://kentcdodds.com/" rel="noopener noreferrer"&gt;Kent C. Dodds&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 2: [Solution] Isolated pisces isolated
&lt;/h3&gt;

&lt;p&gt;Flow of data on client side: Once data is received from server it will flow through state, props and utility functions. All these artifacts should be typed independently. So that whenever they are used with incompatible data typescript can inform us: Type safety!&lt;/p&gt;

&lt;p&gt;So now we will define &lt;code&gt;FavoritePokemon&lt;/code&gt; component like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// EmployeeLongResponse defined as in Problem 1&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;FavoritePokemonProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FavoritePokemon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;FavoritePokemonProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Favorite&lt;/span&gt; &lt;span class="na"&gt;Pokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example usage:&lt;/span&gt;
&lt;span class="c1"&gt;// apiResponse.favoritePokemon = 'pikachu'&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FavoritePokemon&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now as the &lt;code&gt;/employees/{empId}&lt;/code&gt; API changes to respond with a list of pokemons:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;department&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;emailAddress&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
 &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example usage:&lt;/span&gt;
&lt;span class="c1"&gt;// New apiResponse.favoritePokemon = ['pikachu', 'squirtle']&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FavoritePokemon&lt;/span&gt; &lt;span class="nx"&gt;pokemon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;apiResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;favoritePokemon&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/&amp;gt;  /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typescript will yell at us right away with error: &lt;code&gt;'string[]' is not assignable to type 'string'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now as a developer we can decide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do we want to expand &lt;code&gt;FavoritePokemon&lt;/code&gt; so it work with both &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;string[]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Or do we want to create a totally different component display multiple pokemons.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Writing type definitions along the flow of data in application results in following benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;REST APIs remain independent, the way they are meant to be!&lt;/li&gt;
&lt;li&gt;Easier to follow code. Type definitions won't result in rabbit holes.&lt;/li&gt;
&lt;li&gt;No need to manage data relations on client side. Client side code was never meant to do that.&lt;/li&gt;
&lt;li&gt;Better type safety and more meaningful type errors!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Title image by &lt;a href="https://pixabay.com/users/joshuaworoniecki-12734309/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=5310589" rel="noopener noreferrer"&gt;Joshua Woroniecki&lt;/a&gt; from &lt;a href="https://pixabay.com//?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=5310589" rel="noopener noreferrer"&gt;Pixabay&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>TypeScript: type vs interface</title>
      <dc:creator>Udayan Maurya</dc:creator>
      <pubDate>Mon, 23 Jan 2023 00:56:20 +0000</pubDate>
      <link>https://dev.to/udayanmaurya/type-vs-interface-45o7</link>
      <guid>https://dev.to/udayanmaurya/type-vs-interface-45o7</guid>
      <description>&lt;p&gt;Let's resolve this forever outstanding question:&lt;br&gt;
Should I use &lt;code&gt;type&lt;/code&gt; or &lt;code&gt;interface&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spoiler:&lt;/strong&gt; &lt;em&gt;It's called "TypeScript" not "InterfaceScript"&lt;/em&gt; 😉&lt;/p&gt;
&lt;h2&gt;
  
  
  Why a resolution is needed?
&lt;/h2&gt;

&lt;p&gt;Typescript &lt;a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; is itself vague about which one should be used. However, vagueness is not helpful while creating a reliable software. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Most programming languages contain good parts and bad parts. I discovered that I could be a better programmer by using only the good parts and avoiding the bad parts. After all, how can you build something good out of bad parts?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Douglas Crockford&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the face of ambiguity, refuse the temptation to guess.&lt;br&gt;
There should be one-- and preferably only one --obvious way to do it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Zen of Python&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  type - Good, interface - Bad
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. &lt;code&gt;interface&lt;/code&gt; is mutable
&lt;/h3&gt;

&lt;p&gt;Redeclaring an interface appends to existing definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MyInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MyInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MyInterface&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyName&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123456789&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This opens a can of worms (bugs)! In a project of significant size. Types are declared in several files, and some types are declared globally. However, if interface definition can be extended in any file, users cannot be really sure what the type is going to be at usage. Also, these bugs can be extremely challenging to locate.&lt;/p&gt;

&lt;p&gt;On the other hand &lt;code&gt;type&lt;/code&gt;s can only be created once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Error: Duplicate identifier 'MyType'.&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MyType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MyType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mutability of &lt;code&gt;interface&lt;/code&gt;s is a &lt;em&gt;bad feature&lt;/em&gt;: Value proposition of Typescript is to provide guard rails to validate types of variables in your program. Utility of Typescript is compromised if a programmer cannot be sure of what the type of interface is going to be at usage.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;code&gt;interface&lt;/code&gt; has unusual syntax
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;type&lt;/code&gt;s naturally extend the syntax of declaring variables in JavaScript while &lt;code&gt;interface&lt;/code&gt;s have their own syntax which causes needless complexity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Variable creation rule&lt;/span&gt;
&lt;span class="c1"&gt;// Construct Name = Value; &lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Types creation rule&lt;/span&gt;
&lt;span class="c1"&gt;// Construct Name = Value; &lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MyType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="c1"&gt;// Interface creation rule&lt;/span&gt;
&lt;span class="c1"&gt;// Construct Name {Value}; 🤔&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MyInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;code&gt;extends&lt;/code&gt; on double duty
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;extends&lt;/code&gt; in Typescript has dual purpose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extending an interface: More properties can be added to an &lt;code&gt;interface&lt;/code&gt; using &lt;code&gt;extends&lt;/code&gt; keyword.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Bear&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;honey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Conditional type checking: For inputs to a &lt;code&gt;Generic&lt;/code&gt; &lt;code&gt;extends&lt;/code&gt; keyword is used to validate type at usage. Additionally, for conditional types &lt;code&gt;extends&lt;/code&gt; keyword functions as a condition checker for a type being subset of another type.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Works!!&lt;/span&gt;
&lt;span class="nf"&gt;firstChar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Error: Argument of type 'string[]' is not assignable to parameter of type 'string'.&lt;/span&gt;
&lt;span class="nf"&gt;firstChar&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dual usage for keyword &lt;code&gt;extends&lt;/code&gt; is a source of lots of confusion. It makes it difficult for new users to adapt advanced TypeScript features.&lt;br&gt;
However, we can do better! By &lt;strong&gt;not&lt;/strong&gt; using &lt;code&gt;interface&lt;/code&gt; at all we can remove this confusion as &lt;code&gt;extends&lt;/code&gt; will be left with only one usage.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PS: Special thanks to Iván Ovejero for writing &lt;a href="https://ivov.dev/notes/typescript-and-set-theory" rel="noopener noreferrer"&gt;TypeScript and Set Theory&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;code&gt;interface&lt;/code&gt; only works with objects
&lt;/h3&gt;

&lt;p&gt;Now what's up with that 🤷‍♂️. In my opinion this is the origin of problem. &lt;code&gt;interface&lt;/code&gt; was designed to make it easier to work with objects, which resulted in creation of two almost similar features and endless conversation of "type vs interface". &lt;br&gt;
Software languages, libraries and frameworks follow principles of evolution. Like in any evolutionary system many species and initiatives take place to address specific needs and fill certain gaps. &lt;br&gt;
However, not all initiatives successfully manage to address real problems, and sometimes can have detrimental effects on overall structure of program. Unfortunately, unsuccessful experiments cannot be removed from a language due to backwards compatibility.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is rarely possible for standards committees to remove imperfections from a language because doing so would cause the breakage of all of the bad programs that depend on those bad parts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Douglas Crockford&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But we as developers can separate &lt;em&gt;good parts&lt;/em&gt; from &lt;em&gt;bad parts&lt;/em&gt; and only use &lt;em&gt;good parts&lt;/em&gt; to make our softwares great!&lt;/p&gt;

</description>
      <category>web</category>
      <category>browser</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Event Driven Architecture for Redux</title>
      <dc:creator>Udayan Maurya</dc:creator>
      <pubDate>Sun, 25 Jul 2021 17:10:46 +0000</pubDate>
      <link>https://dev.to/udayanmaurya/event-driven-architecture-for-redux-53ak</link>
      <guid>https://dev.to/udayanmaurya/event-driven-architecture-for-redux-53ak</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article elaborates one of the &lt;a href="https://redux.js.org/style-guide/style-guide#model-actions-as-events-not-setters" rel="noopener noreferrer"&gt;Redux best practices&lt;/a&gt;. This best practice has helped me a ton in writing better code and avoiding problems. Many thanks to the Redux team!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redux provides powerful way to store state data for client-side applications. Any part of application can send data, through actions, to be stored in redux then data becomes available to entire application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptjg5ooml8orkmtn0enz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptjg5ooml8orkmtn0enz.png" alt="Redux basics" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, with great power comes great responsibilities! Poorly designed actions/reducers give away most of redux advantages, and application becomes difficult to understand and debug. While well designed actions/reducers helps in keeping the store logic maintainable and scalable. &lt;/p&gt;

&lt;h2&gt;
  
  
  What data to store
&lt;/h2&gt;

&lt;p&gt;We have been told that storage is cheap. However, we still cannot store everything. Additionally, memory is actually very expensive. &lt;br&gt;
While designing data storage systems we need to be mindful of what data is worth storing to solve our problems. Generally relevant &lt;em&gt;events&lt;/em&gt; result in creation of valuable data. While absence of events does not generate any useful data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; There is no point in keep recording footages of a football stadium when no game is being played.&lt;/p&gt;

&lt;p&gt;Similarly, in the world of client-side applications. Useful data, which is required throughout a session of application, is generated when events (user, Web-API, Web-Sockets, etc.) occur. Therefore, designing state tracking system based out of important events result in more maintainable, intuitive, and scalable  system.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setter Actions (Bad Practice)
&lt;/h2&gt;

&lt;p&gt;This a very prevalent approach in many redux applications. Developers create many actions to set state in redux store.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4d7enihdg39i7vczskn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4d7enihdg39i7vczskn.png" alt="Setter Actions" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This architecture results in following problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Developers need to be careful while designing event handlers and split payload properly as per setter actions.&lt;/li&gt;
&lt;li&gt;Dispatch many actions when important events occur. In turn overwhelming actions log, which makes time travel debugging difficult.&lt;/li&gt;
&lt;li&gt;Number of actions explodes when more and more data is required to be stored from same event.&lt;/li&gt;
&lt;li&gt;Due to developer oversight, residual actions can keep lingering in codebase when data requirements reduce.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Again, expanding on example &lt;a href="https://redux.js.org/style-guide/style-guide#model-actions-as-events-not-setters" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Play around with code &lt;a href="https://codepen.io/udayanmaurya/pen/bGWrmPd" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Objective:&lt;/strong&gt; For a food ordering application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer can order any number of pizzas&lt;/li&gt;
&lt;li&gt;Customer can order any number of cokes&lt;/li&gt;
&lt;li&gt;Once customer selection is complete order is sent (main &lt;em&gt;event&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For setter actions: reducer logic look like (1) and action creators like (2)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;orderSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;order&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;pizzaOrdered&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;cokeOrdered&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;reducers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// (1) Reducer logic &lt;/span&gt;
    &lt;span class="na"&gt;setPizzaOrdered&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pizzaOrdered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;setCokeOrdered&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cokeOrdered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;setPizzaOrdered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCokeOrdered&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// (2) Action creators &lt;/span&gt;
  &lt;span class="na"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;orderReducer&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;orderSlice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Send order event handler looks like (3)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sendOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// (3) Send Order event handler&lt;/span&gt;
    &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;setPizzaOrdered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pizza&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;setCokeOrdered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coke&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(3) is another &lt;a href="https://redux.js.org/style-guide/style-guide#avoid-dispatching-many-actions-sequentially" rel="noopener noreferrer"&gt;bad practice&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And action log looks like (4)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6zhu7r1y8xxwwfriun6n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6zhu7r1y8xxwwfriun6n.png" alt="Setter Log" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In sizable application setters action log explodes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem 1:&lt;/strong&gt; Adding fries to menu&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New setter action/reducer (setFriesOrdered) needs to be created in (1) and (2).&lt;/li&gt;
&lt;li&gt;One more action needs to be dispatched in (3), send order event.&lt;/li&gt;
&lt;li&gt;(4) Action log will increase to show one more action &lt;em&gt;order/setFriesOrdered&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Problem 2:&lt;/strong&gt; Removing coke from menu&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setter action/reducer (setCokeOrdered) should be deleted in (1) and (2). However, this deletion is not necessary. Therefore, developers have tendency to miss out deletion. Also, in large teams they hesitate, thinking someone else might be using this action. Resulting in bloated codebase.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;setCokeOrdered&lt;/em&gt; action needs to be removed in (3). Application needs to be aware of changing actions. All the imports need to be adequately removed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Event Based Actions (Do this)
&lt;/h2&gt;

&lt;p&gt;Deriving actions/reducers based out of application events improves the design of redux store significantly. Primarily, due to the fact that &lt;em&gt;data worth storing originates from events&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwjgszwbma1481o53yb81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwjgszwbma1481o53yb81.png" alt="events" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This architecture has following advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dispatch only one action per event, resulting in intuitive separation of concerns between application code and redux code.&lt;/li&gt;
&lt;li&gt;To store more data from an event: developers need to increase payload for the existing action, while reducer manages the internal state of redux.&lt;/li&gt;
&lt;li&gt;To store lesser data from an event: developers need to decrease payload for the existing action, while reducer manages the internal state of redux. No need to manage actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Play around with code &lt;a href="https://codepen.io/udayanmaurya/pen/dyWRvVR" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For food order example: reducer logic looks like (5) and action creator looks like (6)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;orderSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;order&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;pizzaOrdered&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;cokeOrdered&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;reducers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// (5) Reducer logic&lt;/span&gt;
    &lt;span class="na"&gt;orderPlaced&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pizzaOrdered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pizza&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cokeOrdered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coke&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;orderPlaced&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// (6) Action creator&lt;/span&gt;
  &lt;span class="na"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;orderReducer&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;orderSlice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Send order event handler looks like (7)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sendOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// (7) Send Order event handler&lt;/span&gt;
    &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;orderPlaced&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;pizza&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;coke&lt;/span&gt;&lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And action log looks like (8)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg00i2ow84pr10tmyit3k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg00i2ow84pr10tmyit3k.png" alt="Event action log" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem 1:&lt;/strong&gt; Adding fries to menu&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;orderPlaced&lt;/em&gt; reducer in (5) needs to be adjusted per expanded requirement.&lt;/li&gt;
&lt;li&gt;Payload in (7) needs to increase with fries information.&lt;/li&gt;
&lt;li&gt;Action log remains same!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Problem 2:&lt;/strong&gt; Removing coke from menu&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;orderPlaced&lt;/em&gt; reducer in (5) needs to be adjusted per reduced requirement.&lt;/li&gt;
&lt;li&gt;Payload in (7) needs to remove coke's information.&lt;/li&gt;
&lt;li&gt;Action log remains same!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Mindset change
&lt;/h2&gt;

&lt;p&gt;When I started using redux I used to create setter type actions. Upon reading this &lt;a href="https://redux.js.org/style-guide/style-guide#model-actions-as-events-not-setters" rel="noopener noreferrer"&gt;best practice&lt;/a&gt; I had following apprehensions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setters provide me with granular access to redux state and I can dispatch as many actions from wherever in the application per my needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Resolution&lt;/em&gt;: Actions are only required when events, important to the application, occur.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setters give me flexibility in adding/removing actions from event handlers as per my needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Resolution&lt;/em&gt;: Setters are actually tedious because you need to import actions, add them in event handlers and update reducers per changing logic. Managing state changes in event based reducer is easier because you only need to adjust reducer logic and payload.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>redux</category>
      <category>vue</category>
    </item>
    <item>
      <title>Frontend Abstractions</title>
      <dc:creator>Udayan Maurya</dc:creator>
      <pubDate>Thu, 08 Jul 2021 07:25:45 +0000</pubDate>
      <link>https://dev.to/udayanmaurya/frontend-abstractions-4pca</link>
      <guid>https://dev.to/udayanmaurya/frontend-abstractions-4pca</guid>
      <description>&lt;p&gt;Abstract thinking facilities us in solving many problems and solving them efficiently. Ability to say A is like B, helps us cheaply export solutions developed in A to solve B. This further refines the abstract pattern and makes it more robust and reusable (evolution!).&lt;/p&gt;

&lt;p&gt;To solve problems in astronomy ancient Greeks developed abstract patterns to study geometry of triangles, Trigonometry. Now trigonometry helps us in solving problems ranging from construction of buildings to measuring current in electrical circuits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstraction in Programming:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the primary objectives of programming is to come up with a code to mimic a real world process as precisely as possible. More precision results in bug free code which preforms adequately in most of the circumstances encountered in the process.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example&lt;/em&gt;: Compare two dates to find out which is one earlier than other.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Program 1:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write dates in string format "YYYY-MM-DD hh:mm:ss"&lt;/li&gt;
&lt;li&gt;Do string comparison date1 &amp;lt; date2 to find the answer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Such trivial string comparison is a proxy model for underlying process. Therefore, it has tendency to break when program and process depart from one another (at Y-10k).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Program 2:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get dates as date object of your favorite programming language&lt;/li&gt;
&lt;li&gt;Do required comparison per your programming language&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we are very precise by doing the date comparison more closely to required process. Besides any technical machine issue (floating points!) mostly we will get right answer.&lt;/p&gt;

&lt;p&gt;Abstract patterns in programming provide us with tools/mental models, which are very generic and versatile to precisely model many applications of value. Good abstractions are hard to develop and evolve over a long period of time of combined effort.&lt;/p&gt;

&lt;p&gt;In my opinion following are the most prominent abstractions in world of &lt;em&gt;Frontend/UI Development&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Event&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As history has it, Netscape failed to get other programming languages to work in browser environment. To create a more dynamic web, existing programming languages lacked vital responsiveness to user interactions. &lt;/p&gt;

&lt;p&gt;To address the problem Netscape came up with event driven architecture (most powerful aspect of JavaScript to date).&lt;br&gt;
User interactions occur independently of program and act as triggers to various functions of program therefore abstracting these interactions as events for program helped in developing adequate response mechanism for ad-hoc user requests. &lt;/p&gt;

&lt;p&gt;Events proved to be incredibly powerful abstraction and is the major driving force for success of JavaScript in web development (both on client and server side). To help machines communicate over unreliable network &lt;em&gt;events&lt;/em&gt; provide necessary decoupling, to allow programs to work independently on different machines and respond to requests as and when needed. &lt;/p&gt;

&lt;p&gt;Read: &lt;a href="https://dev.to/udayanmaurya/event-driven-architecture-for-redux-53ak"&gt;How events help in designing Redux store&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. State&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In frontend world, next major development happened on February 18, 2005 with publication of &lt;a href="https://www.semanticscholar.org/paper/Ajax%3A-A-New-Approach-to-Web-Applications-Garrett/c440ae765ff19ddd3deda24a92ac39cef9570f1e" rel="noopener noreferrer"&gt;Ajax: A New Approach to Web Applications&lt;/a&gt;. In pursuit of developing more responsive web and enhancing user experience it became ample clear that client side of applications need to share more workload.&lt;/p&gt;

&lt;p&gt;Additionally, to deliver more intuitive experience to users, applications needed to respond conditionally based on what users expect per their state within application usage interface.&lt;/p&gt;

&lt;p&gt;More predictable state management was required to develop more complex and richer client-side. Predictable state management has been the core value proposition of all three major frontend projects: Angular, React and Vue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Effect&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Amazing people in React’s development team have discovered the next major abstraction for frontend developer’s toolkit: Effect (useEffect). &lt;/p&gt;

&lt;p&gt;Together with state, effects enable developer to develop complex applications with much ease. Hopefully ending the era of complex lifecycle management rules and flowchart diagrams, developers needed to put on their walls to be productive!&lt;/p&gt;

&lt;p&gt;Consider following behavior for a conventional select all checkbox scenario (3 checkboxes: Red, Green, and Blue, and 1 Select-All checkbox):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When Select-All is checked all other checkboxes should get checked.&lt;/li&gt;
&lt;li&gt;When Select-All is unchecked all other checkboxes should get unchecked.&lt;/li&gt;
&lt;li&gt;If checking any Red, Green, and Blue results in a state that all of Red, Green, and Blue are checked, then Select-All should get checked automatically.&lt;/li&gt;
&lt;li&gt;When all checkboxes are checked then unchecking any of Red, Green, and Blue should uncheck Select-All.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following are two approaches to solve this problem:&lt;/p&gt;

&lt;p&gt;a. Using only events associated with checking and conditionally performing automatic checking of boxes, which were not clicked.&lt;br&gt;
b. Using effect after any checking event to complete the automatic checking of other boxes.&lt;/p&gt;

&lt;p&gt;Select-All click event function is common in both solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On check – Check Select-All, Red, Green, and Blue&lt;/li&gt;
&lt;li&gt;On uncheck – Un-check Select-All, Red, Green, and Blue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A. Events abstraction only&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/udayanmaurya/embed/yLMQdzr?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Click event for Red checkbox:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We need to overwrite default checking behavior (only check/uncheck the clicked box).&lt;/li&gt;
&lt;li&gt;Conditionally test state of other boxes and perform actions accordingly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi25f4n6c4l8dxnm9jbbv.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi25f4n6c4l8dxnm9jbbv.JPG" alt="Red checkbox - event based" width="413" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Though this code solves the problem it has following issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is hard to maintain: Developer will experience some cognitive stress when reading and understanding this code.&lt;/li&gt;
&lt;li&gt;Developer will need to be careful regarding which checkbox (Red, Green, or Blue) they are coding for and adjust code accordingly – again cognitive stress and prone to error.&lt;/li&gt;
&lt;li&gt;Developers will need to be very careful when adding new checkbox or removing any checkbox.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;B. Using Effect with Events&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/udayanmaurya/embed/Vwbapww?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Click event for Red checkbox:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Default checking behavior (only check/uncheck) Red when Red is clicked.&lt;/li&gt;
&lt;li&gt;If using libraries like Formik we get this behavior for free without writing any click event function!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2ro8vnxzdse7ic0cnks.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2ro8vnxzdse7ic0cnks.JPG" alt="Red checkbox - default" width="421" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Effect to follow the click event&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test state of all the checkboxes if all are checked, check the Select-All box.&lt;/li&gt;
&lt;li&gt;Else uncheck the Select-All box.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2rgrzyicn8xhlvuzoq2.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2rgrzyicn8xhlvuzoq2.JPG" alt="Select All Effect" width="406" height="160"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Beauty of this solution: this program helps us mimic the problem statement more precisely. On any of interaction with checkboxes, user expects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select-All gets checked when all Red, Green and Blue are checked&lt;/li&gt;
&lt;li&gt;Select-All gets un-checked when any of Red, Green and Blue are un-checked&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is exactly what Effect has helped us code, thereby increasing precision of our program. &lt;br&gt;
Adding and removing checkboxes in this code is a breeze we just need to add the new checkbox state in the effect function and select-all click event function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion – Memoir&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While developing a complex form for a client, I was having a lot of challenges fixing legacy code. Also, making changes always resulted in major headaches. Based on many interactions with product owners expected requirements will sound like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When I click this checkbox, these other fields should appear.&lt;/li&gt;
&lt;li&gt;When user has filled these fields, these other fields should be disabled.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reading (obsessing, actually!) these requirements over and over, it struck me that all the posterior behaviors are in fact effects! And, modeling them as such results in more intuitive, maintainable  and bug free code!&lt;/p&gt;

&lt;p&gt;Through this article I wanted to highlight the benefits of effectively using effects. Might as well have called titled it Effectively useEffect, or something. However, I took a longer route to place effects on similar footing as other well know major UI abstractions, again this is my own opinion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommendation:&lt;/strong&gt; While designing your UIs try to word users’ expectations in these 3 buckets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What event users will fire when using your application.&lt;/li&gt;
&lt;li&gt;Maintaining which states are crucial to your business/user experience.&lt;/li&gt;
&lt;li&gt;What effects are expected after certain state changes or events.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These steps have helped me in writing much better code, hopefully they will help you too.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>vue</category>
      <category>angular</category>
    </item>
  </channel>
</rss>
