<?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: Taric Ov</title>
    <description>The latest articles on DEV Community by Taric Ov (@taricov).</description>
    <link>https://dev.to/taricov</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%2F582471%2F73e81e96-1bb4-4e98-9581-669f6e2dcd66.jpeg</url>
      <title>DEV Community: Taric Ov</title>
      <link>https://dev.to/taricov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taricov"/>
    <language>en</language>
    <item>
      <title>TypeScript (2): Handling Nullable Types (5 quick ways)</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Sun, 03 Mar 2024 16:44:30 +0000</pubDate>
      <link>https://dev.to/taricov/typescript-2-handling-nullable-types-5-quick-ways-5dd2</link>
      <guid>https://dev.to/taricov/typescript-2-handling-nullable-types-5-quick-ways-5dd2</guid>
      <description>&lt;p&gt;Null-able types are all about typing a variable value that can either hold a valid value of a specific type or be NULL or UNDEFINED. &lt;/p&gt;

&lt;p&gt;How to handle them in typescript? so writing robust and error-free TypeScript code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare Null-able Types: [using the union operator (&lt;code&gt;|&lt;/code&gt;)]:
&lt;/li&gt;
&lt;/ol&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;name&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="kc"&gt;null&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;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Checking for Nullable Values: [use type guards]
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!==&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&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;ol&gt;
&lt;li&gt;Optional typing: [use Optional chaining]:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Assigning Nullable Values: [assigned null or undefined explicitly]:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
   &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Nullish Coalescing Operator (&lt;code&gt;??&lt;/code&gt;):&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;- It returns the right-hand operand if the left-hand operand is null or undefined.&lt;/em&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;let&lt;/span&gt; &lt;span class="nx"&gt;result&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;let&lt;/span&gt; &lt;span class="nx"&gt;nullableValue&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="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;// Using nullish coalescing operator&lt;/span&gt;
   &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nullableValue&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Default Value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Default Value"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By understanding how to handle nullable types in TypeScript, you can write more robust and error-resistant code that gracefully handles nullable values.&lt;/p&gt;

&lt;p&gt;Article on Linkedin: &lt;a href="https://www.linkedin.com/posts/taricov_typescript-developercommunity-softwareengineering-activity-7170098531318284288-hoIj?utm_source=share&amp;amp;utm_medium=member_desktop"&gt;https://www.linkedin.com/posts/taricov_typescript-developercommunity-softwareengineering-activity-7170098531318284288-hoIj?utm_source=share&amp;amp;utm_medium=member_desktop&lt;/a&gt; &lt;/p&gt;

</description>
      <category>typescript</category>
      <category>tutorial</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>(1) TypeScript: Create Optional Types From Old Types?</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Sat, 20 Jan 2024 15:58:36 +0000</pubDate>
      <link>https://dev.to/taricov/1-typescript-create-optional-types-from-old-types-420m</link>
      <guid>https://dev.to/taricov/1-typescript-create-optional-types-from-old-types-420m</guid>
      <description>&lt;p&gt;Here's a quick tip on how to use already-existing types to create a new type with  optional properties without changing the original type:&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;type&lt;/span&gt; &lt;span class="nx"&gt;ToptionalProps&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&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="nx"&gt;K&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 Interface:&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Tuser&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="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="nl"&gt;email&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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ToptionalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ToptionalProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Tuser&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;// Equivalent to: type ToptionalUser = { id?: number; name?: string; email?: string };&lt;/span&gt;

&lt;span class="c1"&gt;// ==== And you can play around and Pick and Omit like so: =====&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;TnameAndIdUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Tuser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;TomittedOptionalProps&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&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="nx"&gt;K&lt;/span&gt;&lt;span class="p"&gt;]&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;ToptionalUser2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TomittedOptionalProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TnameAndIdUser&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ToptionalUser2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;newuser@gmail.com&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 'email' does not exist in type 'TpickedOptionalProps&amp;lt;x&amp;gt;';&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;With this trick, you can now easily create a type where all properties of the original type become optional.&lt;/p&gt;

&lt;p&gt;Ts Playground: &lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://www.typescriptlang.org/play?#code/C4TwDgpgBAKg9mYBLOA7AhgGwAoCcEDOAPDAHxQC8UA3lANoDSUSqUA1hCHAGawC6AfgBcsRnygBfANwAoGQHp5UAKIAPdAFswmaAElUwCLm7oAxhCFyWh42egwArgSM0ZUZgBMRqBxoBGRrLuGBoWUATAuCwA5kFQEBroSJgiEVGosTIScqCQsAjIaFgAqs64lPmIKBg4+GDEjmWksgpKygCODkgAblgQBlDAcCK59gXVJWUVtEgewlA+-oELmhDzaTFS8YnJ65Exki2KlBSnUACCqB5QXA5Qpuis2uggUOj4DldvX9hIpmzfa4AeQ0SGAUEwSA44WGJ1OFBy4HsIQglw8ug8pRcVBBYJITiMABooAAiBJJTAk0gyUb5UHAQweIFVIq1QgkchUWiMZisDhcXgwQQiGBiQ6IvLwFk1LG4ABMFXg9MZzMKNTw7JgKLRGNlzTkpjQEQWEAA7rKReNWbKFVzyckRCSCOgPCTssdlLh8OUAOT2zA+qAeOAQAgLODgiCqJDGliDJFQH0wMB-DhM6VYDX1IiqUg+qRAA" rel="noopener noreferrer"&gt;
      typescriptlang.org
    &lt;/a&gt;
&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Do We Really Hate Bugs? A Call For Peace… CringyAI And Moving Things #2</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Tue, 20 Jun 2023 00:35:38 +0000</pubDate>
      <link>https://dev.to/taricov/do-we-really-hate-bugs-a-call-for-peace-cringyai-and-moving-things-2-5fln</link>
      <guid>https://dev.to/taricov/do-we-really-hate-bugs-a-call-for-peace-cringyai-and-moving-things-2-5fln</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mSZOz-d7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qexdscqtg21n179vpfew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mSZOz-d7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qexdscqtg21n179vpfew.png" alt="Image description" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is the &lt;strong&gt;2nd edition&lt;/strong&gt; of a light-weight tech-comic newsletter ⎯ &lt;strong&gt;CringyAI and Moving Things&lt;/strong&gt; shares thought-provoking ideas about tech and AI .. for more visit: &lt;a href="https://bit.ly/cringyai"&gt;https://bit.ly/cringyai&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6wT7h-7L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssbub4kjtozalvesdsh7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6wT7h-7L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssbub4kjtozalvesdsh7.jpg" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While bugs really move and infect plants, skin, and code .. they have much of a role in our lives that should make us take them seriously, … I mean differently! 😅&lt;/p&gt;

&lt;p&gt;Of all sorts of bugs 🐞 the one in the code could really make you lose a lot of money 🤣 and you can not kill them by using pesticides, but rather by having a peaceful-mind that never runs outta patience, digging deep and reviewing the  codebase that you earlier celebrated its release … what the bug!&lt;/p&gt;

&lt;p&gt;With AI, we reached the point where we are, now, developing AI that is capable of killing real bugs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"AI-controlled robotic laser can target and kill cockroaches" - newscientist.com&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you ask me, I couldn't let this get past my imagination 👉 This is StarWars In The Field lol 😂&lt;br&gt;
but how about a bug in the AI? &lt;br&gt;
could we have a robot w/ laser? but it has a bug in their codebase … re-appearing in the news headlines again but this time it reads:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Uncontrolled AI robot chasing framers with a green laser beam" - thatisinmymind.me&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;but wait? what about the poor farmers.. and how could they self-defend themselves against the out-of-control robot?&lt;/p&gt;

&lt;p&gt;for that we should continue with what the news could read:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"…and while farmers are making their way out of the field, they keep releasing more bugs to distract the raged robot." 😂&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's a fair fight, bugs vs bugs and we live in the margin.&lt;/p&gt;

&lt;p&gt;But seriously, if we could ever take anything seriously in this publication 😆 .. why we do not enjoy bugs?&lt;/p&gt;

&lt;p&gt;We all know a bug could be the feature u r about to discover, it could be your app's competitive edge, or it could be your best selling point.&lt;/p&gt;

&lt;p&gt;Is it just because they r not intended? Or just cuz they feel off?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cuz I know as everybody knows that not all bugs r hated, like..&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;- ..imagine a switch button that toggles on/off but it fills the whole width of your screen so the switcher travels onChange from edge to edge .. wouldn't that be playful and inviting for more session duration avgs and lesser bounce rates .. pllllllllllz 🙃&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How about a website that is built w/ unintentional components, full of bugs.. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; -  ..man, I'd stay there, forever, adventuring for 👉 from where the sidebar could show and come in for me 😂😂&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wait! ✋&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; -  …how about an app for developers that rewards (The Best Generated Bug) and we r battling 🙃🤣🤣&lt;/p&gt;

&lt;p&gt;That's enough for today 😁&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>ai</category>
      <category>deeplearning</category>
      <category>datascience</category>
    </item>
    <item>
      <title>How About A Game.. That Plays You? *CringyAI And Moving Things #1</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Fri, 16 Jun 2023 13:43:39 +0000</pubDate>
      <link>https://dev.to/taricov/how-about-a-game-that-plays-you-cringyai-and-moving-things-1-15de</link>
      <guid>https://dev.to/taricov/how-about-a-game-that-plays-you-cringyai-and-moving-things-1-15de</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is the 1st edition of a &lt;em&gt;light-weight tech-comic newsletter&lt;/em&gt; ⎯ &lt;strong&gt;CringyAI and Moving Things&lt;/strong&gt; shares thought-provoking ideas about tech and AI .. for more visit: &lt;a href="https://bit.ly/cringyai"&gt;https://bit.ly/cringyai&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GAqnlBkG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w240gqmllwyr51kg0pb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GAqnlBkG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w240gqmllwyr51kg0pb.png" alt="Image description" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How about a game where you can free-talk to other NPC (non-player character) and they argue and discuss your choices/decisions in the game 🫤&lt;/p&gt;

&lt;p&gt;So the characters (NPCs) could be for example chatgpt-based trained agents or better? &lt;strong&gt;DRL models&lt;/strong&gt; (deep reinforcement learning) 🧐&lt;/p&gt;

&lt;p&gt;&lt;em&gt;That makes it an open-ended game where you could easily get stuck&lt;/em&gt; lol 👽&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>deeplearning</category>
      <category>chatgpt</category>
    </item>
    <item>
      <title>TypeScript: Any…? Does &lt;any&gt; Have Any Benefits in Your Code?</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Wed, 14 Jun 2023 12:30:00 +0000</pubDate>
      <link>https://dev.to/taricov/typescript-any-does-have-any-benefits-in-your-code-2a5o</link>
      <guid>https://dev.to/taricov/typescript-any-does-have-any-benefits-in-your-code-2a5o</guid>
      <description>&lt;h5&gt;
  
  
  The &lt;code&gt;&amp;lt;any&amp;gt;&lt;/code&gt; type in TypeScript is a dangerous "escape hatch" from the type system. 
&lt;/h5&gt;

&lt;p&gt;We all know that Typescript is a powerful tool for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;type safety, and&lt;/li&gt;
&lt;li&gt;catching errors early in the development process. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, there is one type that can be detrimental to your codebase if used carelessly - the infamous &lt;code&gt;any&lt;/code&gt; type. &lt;/p&gt;

&lt;h2&gt;
  
  
  "Any..?" vs "I like to be specific about things!"
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Imagine&lt;/em&gt; that you are at the grocery and you go over the cheese section:&lt;/p&gt;

&lt;p&gt;_ "do you have cheese today guyz??"&lt;br&gt;
= "definitely!!" - says the seller&lt;br&gt;
_ "gimme some"&lt;br&gt;
= "of what?"&lt;br&gt;
_ "any..!"&lt;/p&gt;

&lt;p&gt;and you repeat that in the fruits, meats, beauty… etc sections .. what you will end up with?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Things you have never desired and you may have paid (incurred) high cost for them.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;let's see some of both &lt;strong&gt;UNdesired&lt;/strong&gt; results and how high it could cost you using &lt;code&gt;&amp;lt;any&amp;gt;&lt;/code&gt; in your codebase.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Pitfalls of Using the &lt;code&gt;any&lt;/code&gt; Type:
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;any&lt;/code&gt; may seem tempting at times, but it comes at a cost. Here are some reasons why you should think twice before reaching for the &lt;code&gt;any&lt;/code&gt; type:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Type Safety Leakage&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Accessing members on an &lt;code&gt;any&lt;/code&gt;-typed value bypasses TypeScript's type checking, creating a potential source of bugs and reducing the benefits of static typing.&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="err"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// No type checking, potential runtime error&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Loss of IntelliSense&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When using &lt;code&gt;any&lt;/code&gt;, you lose the benefits of TypeScript's powerful IntelliSense, making code exploration and auto-completion less effective.&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="err"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="c1"&gt;// No IntelliSense, decreased productivity&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Lack of Documentation&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;any&lt;/code&gt; type erases valuable information about the expected data shape, making it harder for other developers (including your future self) to understand and maintain the code.&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="err"&gt; &lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;processResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Missing documentation about the expected structure of the data&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Other developers need to rely on external documentation or guesswork&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Compiler Warnings and Errors&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The use of &lt;code&gt;any&lt;/code&gt; suppresses valuable compiler warnings and errors that can help catch potential issues before they become runtime bugs.&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="err"&gt; &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// No compile-time warning, potential runtime error&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Any Alternatives…? I mean Any Alternative to ?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Instead&lt;/em&gt; of resorting to the &lt;code&gt;any&lt;/code&gt; type, consider these alternatives to ensure type safety and maintainable code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Define Specific Types&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Create your types 😑 types that you know and want to retrieve .. Use interfaces or types that accurately represent the shape of your data, providing clarity and enhancing the understanding of your codebase, for example:&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="err"&gt; &lt;/span&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&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="err"&gt; &lt;/span&gt;&lt;span class="nx"&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="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;email&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="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use Union Types&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nothing feels warmer than a Union 😄 even if it's other ppl's uions .. so why stay in the cold? lol&lt;/p&gt;

&lt;p&gt;You really could combine multiple types using union types (&lt;code&gt;|&lt;/code&gt;) to express the flexibility of a variable without sacrificing type safety:&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="err"&gt; &lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getUserById&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="c1"&gt;//null btw never felt warm to meeeee 😀&lt;/span&gt;
&lt;span class="err"&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;result&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;!==&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="err"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Type inference works, preserving type safety&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Leverage Generics&lt;/strong&gt;: 💪&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have been talking about Generics for a while now and how magical they are to give your code flexibility. (to even split 😜)&lt;/p&gt;

&lt;p&gt;Use generics to create reusable components that work with different data types while maintaining type safety…&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="err"&gt; &lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchAndProcessData&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;url&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;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// Type inference works, preserving type safety&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;processData&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;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;For maintainability … use more specific types, union types, and generics.&lt;/li&gt;
&lt;li&gt;For bugs … use &lt;code&gt;&amp;lt;any&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;…for me, I know bugs as interesting creatures or JIRA issues 😆&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Typescript: Typing Your API Requests - Generics Hacks #4</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Wed, 14 Jun 2023 07:13:08 +0000</pubDate>
      <link>https://dev.to/taricov/typescript-typing-your-api-requests-generics-hacks-4-2klp</link>
      <guid>https://dev.to/taricov/typescript-typing-your-api-requests-generics-hacks-4-2klp</guid>
      <description>&lt;p&gt;Are you ready to take your REST API requests to the next level? Buckle up, because… Ah! 😑 why go boring? while i can call u guyzzzzz 🤣&lt;/p&gt;

&lt;p&gt;Btw, do you know? Along this short series, TypeScript and typing cases had come to a closer place to us, and are about to become that handy that do a lot of magic by a few tricks! 🙂&lt;/p&gt;

&lt;p&gt;This is not generics-related tut but it's extending on the last Generics Hacks article.. we will use TypeScript generics to provide a  type-safe response coming from REST API requests whatever they were.&lt;/p&gt;




&lt;h2&gt;
  
  
  TypeScript's UNKNOWN = The New Way of Knowing Things. 
&lt;/h2&gt;

&lt;p&gt;We'll be using &lt;code&gt;fetch&lt;/code&gt; API in this example, and will be using dummyjson.com to fetch dummy data for demonstration:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notes will precede code blocks … you can check out the code first and then read the notes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Setting some variables for making API requests.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// STEP 1&lt;/span&gt;
&lt;span class="c1"&gt;// setting variables&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://dummyjson.com&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;endPoint&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="c1"&gt;// u could use /posts or /products for other data types&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;This is the function where we request data…&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Promise leverage the use of one of the primitive types in Typescript &lt;code&gt;unknown&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;unknown&lt;/code&gt; is way safer than any that mist typing and make &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;unknown&lt;/code&gt; is so useful for APIs when you need to perform type checking before you use them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In contrast to any which means disable type checking &lt;code&gt;unknown&lt;/code&gt; is a Typescript real and capable type, it's just &lt;br&gt;
the least effective among the more-specific others.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The real power of unknown typing value is that you can use type guards to narrow the type and get more accurate type-checking on narrowed types.&lt;br&gt;
&lt;/p&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="c1"&gt;// STEP 2&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;GetAllItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;url&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;endpoint&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;endpoint&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;json&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;🔥 &lt;strong&gt;A TRICK&lt;/strong&gt; 🔥: now we declare an example user object w/ some/all properties. We will use that obj. to prototype that as our new base type User for that API call result
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// STEP 3&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userExample&lt;/span&gt; &lt;span class="o"&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="dl"&gt;''&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="dl"&gt;''&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="dl"&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;type&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;userExample&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Here we goooo.. one more step before sending our request:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We are making a type-checker function based on the base-type &lt;code&gt;User&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The checker will take a user as a param to check on its type&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And will return the result narrowed using the Type-gaurd operator &lt;code&gt;is&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So if user passes the checker and evaluates to =&amp;gt; true&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;That will enforce our typing for &lt;code&gt;intellisense&lt;/code&gt; and type-safe use for all users&lt;br&gt;
&lt;/p&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="c1"&gt;// STEP 4&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// defaulting to false for effective checking&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;checking&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
  &lt;span class="c1"&gt;// iterating over keys from the `userExample` as they r the same keys for `User`&lt;/span&gt;
  &lt;span class="k"&gt;for&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;t&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userExample&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
   &lt;span class="nx"&gt;checking&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;checking&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;false&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// if only one key evaluetes false 👆 user fails the test&lt;/span&gt;
  &lt;span class="c1"&gt;// otherwise it passes and return true 👇 and pass the success status to all the array result of users&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;checking&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Now we call our API endpoint and return:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// STEP 5&lt;/span&gt;
&lt;span class="nx"&gt;SendReq&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;SendReq&lt;/span&gt;&lt;span class="p"&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;User&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;allUsers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;GetAllItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;endPoint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// allUsers as `any` here to bypass the linter and we r about to check the type ourselves&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;allUsers&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// now user is typed and can be used safely &lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;allUsers&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="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// in case isUser(user) checking fails&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;➕&lt;strong&gt;…and intellisense is working&lt;/strong&gt; 🟢&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RazDdX6e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj2ymciuexujfedor7jo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RazDdX6e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj2ymciuexujfedor7jo.png" alt="Image description" width="800" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🟢🟢 Check out the full example code in the Typescript playground (try it urself)&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://www.typescriptlang.org/play?#code/PTAEGUBUFEAVQIwCgSgM4FMAuWCWA7Ac1ADcBDAJ1zICMAbDNJAYwHt80tQBXCugLk5UioALygARAAscABzT8QAE24BbVQE8AVmnYA6NqokBuFu06gM+JbFYEs-dFmHFxE4N0wU0ElGG6gbNx0SjyYoMCyrJxooKwUEbIUrCrMWLEAZvFxWFIYCUpkWGSgWBqyjEhVqFBwoABMSGRoGvjMoBncbXjsoADi2ACCdHQAklgYqmgAFEigPHyOQgSEADSW1lH2S84rSACUjrDJqriYADxdANb4rADu+AB8oADec4HmXBSMURwYYqAyHcyLguBlsMwpNNeHRQABqDZKLb4LD7UzzNgcLg6XriIEgr4-cwYPQ4-DTNHvb5YXj4UBkvSefKxJAAXyqfggMHgAGYzFiwvloAAPMiqWQMAFvea4JSOADk8tW7wyuG8WAAcmKMAqle86M1Ndrdcr2WUKqAAKpeAHmjCsDKCigisUSjAczm1eAAFk5uX+kIwzCu+VAd1wI1KZBDgKdgNiZDpskoYtKrECeWDcTpoNids5idC4cj1NppTyoG+aGCXHwlGSdwwoU8K3L-0g5QwAFpCGReKFWBUKEVsgADM6jzm6UC4R1MhLJtCYPMVwPB0OFyzkOjcIqMNNiZ7Obju1C5IphiOwqxZCjMf6sXilcqt28zlEYEZnKzhTd2rtoGQ4KCh02RkJG85MJ03S4L0ZzWvk0JeI4iYaIccZnFaNrShEYBKBgGR9nQeAiFg6aEXQ4RvgR4JpLgJABpmVx7PMDBcGuzEiOIFGYO8qCgvkRStqwDEJCGGiZCcbagKO84uuKDCjvGbYaJW0mAao-ziZkY4IRQk7zLe0yYhYXAOqAADyNBaEGWB6NpSFCqKCkYPs+w4RmQaca4pTvk67wyhk0wAIQcSs+wBfMlbYGWPHuvM7LzPxjrsHQqnsFpGCqRg24nhMmTgeEgC8G4AYjtxoREYrv8+VYHxYCsP6FDhuEoKgIuy6AtY0U0hQdLHv8RWAOI7nWhO16ncMw95Lk4RSeAe4Gwv6gIUMOqlVjWcRzl4TDzKWvWecGezsp63KgAArEg4BWEoABKGAAI4Uk0LRtB0XR0b0V3WHdj0HEcJzfucemgAAPqA+DBHQzxvKgtx3GGAYLXET6DLAoyIsiXCbnt+D8JywN2tm6BBlNTCw5Dz4Wr0FV0O8JlYyMekKKhAL4q1AxYMMYwTFM0J8Os122PY+y4YCjPbcpo6oUpeTfAeNDlM0VWgHQ9gbl1jZqbQj5cGRB1XNJhOPt4n4MTtHwCvOrPi8yADaAAMAC6oDvLOoCOQkABknszmgeke25ryRSZrAMHodCsIQAeRbD9wYXmnahJuzCJqAND-EyoSAeCaUu1F3VlgtTPGHnoCJQX+0QyMJf8XSKctX7XgB-rr4gpRbJAA" rel="noopener noreferrer"&gt;
      typescriptlang.org
    &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;🟢🟢 Check out the full example code gist:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>#1 What am I building this week? 🥳 D-Notes - Chrome Extension App</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Tue, 06 Jun 2023 15:42:24 +0000</pubDate>
      <link>https://dev.to/taricov/what-am-i-building-this-week-d-notes-chrome-extension-app-13ci</link>
      <guid>https://dev.to/taricov/what-am-i-building-this-week-d-notes-chrome-extension-app-13ci</guid>
      <description>&lt;p&gt;I build something new every week.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H9a4izJu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/saqtgtemzf6o84csj1sr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H9a4izJu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/saqtgtemzf6o84csj1sr.png" alt="Image description" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I would like to share w/ you guyz about my recent build for this week.&lt;/p&gt;

&lt;p&gt;D-Notes 👉 a note-taking extension app to work on top of Daftra ERP system (Popular in GCC and North Africa)&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;I touch on users' pain when it comes to get things done w/ efficiency ⚙️ as I directly contact them on a daily basis for custom programing requests, while most of their requests r all about how to automate things or make things easier by developing custom workflows/features/tools.&lt;/p&gt;

&lt;p&gt;Of all types of end-users, there're accountants and auditors how operate for longer time on ERPs especially in the Financial modules, Taxation and so on. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Pain
&lt;/h2&gt;

&lt;p&gt;They would love to have their own crap drawers 📦 to throw in them things that r not currently urgent or meant for do-it-now 👀 but they still wanna find them 🔍 when needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Approach
&lt;/h2&gt;

&lt;p&gt;This tool will enable ERP operators especially accountants and auditors to have their side marginal draft (crap drawer) where they could scratch reminders/comments/todos/tasks for when they want to refer back later .. all that while:&lt;/p&gt;

&lt;p&gt;1) They don't have to disrupt their workflow w/ external tools 🤓&lt;br&gt;
2) All in one screen 💻 and on the same screen, and&lt;br&gt;
3) The note is attached to where it was taken (per page) 🔗&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3wW-dNx---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9y8kl4h9ahyq1ezr57aw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3wW-dNx---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9y8kl4h9ahyq1ezr57aw.png" alt="Image description" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My Final Solution
&lt;/h2&gt;

&lt;p&gt;A free ✋ and open-source 😇 extension app so users could use it or even further customize it as per their biz needs 🔥 building on the base feature that relies on the previously settled approach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rYIr02rx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svlldb2l2s75w9rgmmay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rYIr02rx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svlldb2l2s75w9rgmmay.png" alt="Image description" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Main Goal
&lt;/h2&gt;

&lt;p&gt;I wish I could provide a better user-experience and far way more efficient workflow + documentation? never been easy 😞 and good documentation is rare gold 😂&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MAjAjZmG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5gptm82pz5zstv77ceu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MAjAjZmG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5gptm82pz5zstv77ceu.png" alt="Image description" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;just if u ask 🙂 why an extension app? cuz it's a cloud-based ERP system and most users use the browser&lt;/em&gt; 🤺&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Tech Stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;TypeScript&lt;/li&gt;
&lt;li&gt;VueJs&lt;/li&gt;
&lt;li&gt;ViteJs&lt;/li&gt;
&lt;li&gt;UnoCSS&lt;/li&gt;
&lt;li&gt;Appwrite&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Status-Quo:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Notes on development/deployment...
&lt;/h3&gt;

&lt;p&gt;1) D-Notes has been finished and tested 💪&lt;/p&gt;

&lt;p&gt;2) It was developed on chrome manifest V2 so I tried to deploy to Chrome ex store, the app got the review to upgrade to V3 (it's mandatory now 😏) &lt;/p&gt;

&lt;p&gt;3) It’s working locally if u load it manually and spin a dev server (yarn/pnpm dev) ✋ then u will be able to connect and use it normally ✅&lt;/p&gt;

&lt;p&gt;4) Upgrading to v3 for deploying is WIP rn 🚧 &lt;/p&gt;

&lt;p&gt;5) and will build a landing page for more info ℹ️ &lt;/p&gt;

&lt;p&gt;6) [Repeated] It’s a Free and Open-source 🤩&lt;/p&gt;

&lt;p&gt;7) Read the README in the linked repo below for more info if u intend to use it as a base for ur next Chrome ex app .. I will update it w/ how to connect to ur database, as a dev, screenshots and a how-to-use guide will be available ✅&lt;/p&gt;

&lt;p&gt;🔗 repo: &lt;a href="https://lnkd.in/dHefQzcU"&gt;https://lnkd.in/dHefQzcU&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;8) I will be doing a tut in Arabic and English on how to integrate the tool in your daily workflow (let’s add this to the todos)&lt;/p&gt;

&lt;p&gt;9) Any PR of any type r welcomed ❤️&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;...me while resolving the migration to V3 issues and finishing off README.md, I am Parallelly 🫱 working on the second tool that will be coming next week 👇 &lt;/p&gt;

&lt;h3&gt;
  
  
  D-invoices
&lt;/h3&gt;

&lt;p&gt;I have started this week-run new build .. another tool in the bundle 🤘 D-invoices where typically users will be able to manage invoices 🧾 in a better and more convenient way. (will be finished in a couple of days and will post the finished deployed product) &lt;/p&gt;

&lt;p&gt;...wish me luck 🤞&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>softwareengineering</category>
      <category>extensions</category>
      <category>erp</category>
    </item>
    <item>
      <title>Typescript: How to use Generics to make your own Type Templates?</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Thu, 01 Jun 2023 13:20:29 +0000</pubDate>
      <link>https://dev.to/taricov/typescript-how-to-use-generics-to-make-your-own-type-templates-4abp</link>
      <guid>https://dev.to/taricov/typescript-how-to-use-generics-to-make-your-own-type-templates-4abp</guid>
      <description>&lt;p&gt;After we talked about types utilities the last post, let's extend and see more advanced usage of generics in typescript and we could build our own type templates that can be reused across your projects:&lt;/p&gt;

&lt;p&gt;let's say that we will type a social tweet/post data…&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;postProps&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="nl"&gt;relativeTime&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="nl"&gt;tweet&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="nl"&gt;retweets&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="nl"&gt;likes&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="nl"&gt;comments&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="nl"&gt;userImg&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="nl"&gt;liked&lt;/span&gt;&lt;span class="p"&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="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;likedUsers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="nl"&gt;img_src&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="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="o"&gt;&amp;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;p&gt;we are familiar now w/ interfaces in typescript and to put it in simple quick words, interfaces are type contracts that an entity/object should conform to.&lt;/p&gt;

&lt;p&gt;Now..&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;type&lt;/span&gt; &lt;span class="nx"&gt;readOnlyProps&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&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="nx"&gt;P&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;Here we go.. This is a type template of a &lt;code&gt;readonly&lt;/code&gt; that converts any type props into readonly mode using the keyword &lt;code&gt;readonly&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We use &lt;code&gt;&amp;lt;T&amp;gt;&lt;/code&gt; as our parameter of props.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And then we use this syntax &lt;code&gt;[P in keyof T]&lt;/code&gt; to loop through props keys and re-print them w/ the prepended keyword &lt;code&gt;readonly&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;T[P]&lt;/code&gt; is so obviously bringing in the value of the key for each prop&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we can use this template, 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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;readOnlyPostPorps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;readOnlyProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;postProps&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;//readOnlyPostPorps is our new type&lt;/span&gt;
&lt;span class="c1"&gt;//readOnlyProps is our type template/converter we defined b4&lt;/span&gt;
&lt;span class="c1"&gt;//postProps us our original interface&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  As you see how generics:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;makes things handy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;generates from already existing types,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;prevents us from repeating/re-typing props (reusability)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;opens possibilities for more complex flows&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now..&lt;/p&gt;

&lt;p&gt;Try out the example and play around w/ the template and see what you can come up w/ new:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/typescript-typing-hacks-w-generics-l6u9o6"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;till next time.. keep ur type tight.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Typescript: Type Utilities for the goodness and reusability</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Wed, 31 May 2023 22:48:33 +0000</pubDate>
      <link>https://dev.to/taricov/typescript-type-utilities-for-the-goodness-and-reusability-323i</link>
      <guid>https://dev.to/taricov/typescript-type-utilities-for-the-goodness-and-reusability-323i</guid>
      <description>&lt;p&gt;How to use Pick/Omit/Exclude/etc.. to do cool stuff..&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fecdh9p146dpsyozz36un.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fecdh9p146dpsyozz36un.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The pick is a utility .. utility type that allows you to select specific properties from an object type and create a new type with only those properties .. cool?&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;// Define a User interface&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&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="nl"&gt;age&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="nl"&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="nl"&gt;email&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="nl"&gt;single&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;span class="nl"&gt;job&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="nl"&gt;degree&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;// Now we r sliccccing 😀&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserBasicInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Pick&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;age&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserBasicInfo&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;Taric Ov&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: 'Taric Ov', age: 28 }&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//dev-to-uploads.s3.amazonaws.com/uploads/articles/0pyy0r737d6rqeyd9u79.png)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We have an interface &lt;code&gt;User&lt;/code&gt; that defines their properties.&lt;/li&gt;
&lt;li&gt;We used the &lt;code&gt;Pick&amp;lt;User, 'name' | 'age'&amp;gt;&lt;/code&gt; type to create a new type called &lt;code&gt;PersonBasicInfo&lt;/code&gt; that only includes the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; properties from the whole &lt;code&gt;User&lt;/code&gt; interface.&lt;/li&gt;
&lt;li&gt;We then created an object of the type &lt;code&gt;UserBasicInfo&lt;/code&gt; called &lt;code&gt;newUser&lt;/code&gt; with the selected properties.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;got it? if yes? that sounds great cuz u know what? you now got Omit/Exclude as well 😮💪 congrats 😉&lt;/p&gt;

&lt;p&gt;if still need more explanation? try it in code and follow the comments 😊&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/typescript-type-utilities-b03y4r"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So..&lt;/p&gt;

&lt;p&gt;Omit is also a utility type that does the opposite of Pick. It allows you to exclude specific properties from an object type and create a new type without those properties.&lt;/p&gt;

&lt;p&gt;— The terms “omit” and “exclude” can be used interchangeably in the context of utility types in TypeScript. Both &lt;code&gt;Omit&lt;/code&gt; and &lt;code&gt;Exclude&lt;/code&gt; are utility types that provide similar functionality but with slight differences.&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;// Omitting &lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&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="nl"&gt;age&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="nl"&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="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserWithoutAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;address&lt;/span&gt;&lt;span class="dl"&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;//Excluding numbers from a literal&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;5&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;ExcludedNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;5&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;// ExcludedNumbers: 2 | 4&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What About the other utility types in TypeScript?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Partial: This utility type creates a new type that makes all properties of the original type &lt;code&gt;T&lt;/code&gt; 👉 optional.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Required: This utility type creates a new type that makes all properties of the original type &lt;code&gt;T&lt;/code&gt; 👉 required.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Readonly: This utility type creates a new type that makes all properties of the original type &lt;code&gt;T&lt;/code&gt; 👉 read-only.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Record: This utility type creates a new type with keys of type &lt;code&gt;K&lt;/code&gt; and values of type &lt;code&gt;T&lt;/code&gt; 👉 It's often used to define dictionaries or mappings.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These utility types, along with &lt;code&gt;Omit&lt;/code&gt; and &lt;code&gt;Exclude&lt;/code&gt;, provide powerful ways to manipulate and transform types and even to do cool tricks in TypeScript.. and that's what we are to explore in the next tutorial.&lt;br&gt;
till then.. keep ur types tight 💪&lt;/p&gt;

&lt;p&gt;Next: Type Templates 🤘(…out the oven)&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Generics Hacks (1): ReadOnly in Typescript w/ 3 Examples</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Sat, 20 May 2023 16:23:22 +0000</pubDate>
      <link>https://dev.to/taricov/generics-hacks-1-readonly-in-typescript-w-3-examples-1kaf</link>
      <guid>https://dev.to/taricov/generics-hacks-1-readonly-in-typescript-w-3-examples-1kaf</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XkeP67ar--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuvi966u6okaejr4sfwb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XkeP67ar--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuvi966u6okaejr4sfwb.png" alt="Image description" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;readonly&lt;/code&gt; is a keyword in TypeScript that we use when we want to make a property UNchangeable, so the value of the property cannot be changed after it has been set.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) How to use the &lt;code&gt;readonly&lt;/code&gt; keyword on a property of a type?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&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="nl"&gt;age&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;const&lt;/span&gt; &lt;span class="nx"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// name property will not change 'cuz it was assigned to be readonly&lt;/span&gt;
  &lt;span class="nx"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sarah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;❌&lt;/span&gt;
  &lt;span class="nx"&gt;person1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;✅&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2) How to use the &lt;code&gt;readonly&lt;/code&gt; keyword on an interface?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt; 
  &lt;span class="nx"&gt;make&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="nl"&gt;model&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;readonlyCar1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Readonly&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;make&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tesla&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Model S&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// This will not compile because the make property is readonly&lt;/span&gt;
&lt;span class="nx"&gt;readonlyCar1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;make&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Toyota&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;❌&lt;/span&gt;
&lt;span class="c1"&gt;// This will not compile because the model property is readonly&lt;/span&gt;
&lt;span class="nx"&gt;readonlyCar1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Camry&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;❌&lt;/span&gt;
&lt;span class="err"&gt;🟢&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;make&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tesla&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Model S&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;car2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;make&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Toyota&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// OK ✅&lt;/span&gt;
&lt;span class="nx"&gt;car2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Camry&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// OK ✅&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3) How to leverage generics and use the &lt;code&gt;readonly&lt;/code&gt; keyword dynamically?
&lt;/h2&gt;

&lt;p&gt;This is a normal static interface 👇&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;postProps&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="nl"&gt;relativeTime&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="nl"&gt;tweet&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="nl"&gt;retweets&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="nl"&gt;likes&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="nl"&gt;comments&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="nl"&gt;userImg&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="nl"&gt;likedUsers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="nl"&gt;img_src&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="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="o"&gt;&amp;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;p&gt;Let's say now that we need to have a read-only version of this interface, now we have two scenarios:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You already know the type: then you could do it manually 🤚 one by one putting thereadonlykeyword before each property.&lt;/li&gt;
&lt;li&gt;You don't know the type or you know it and it has tons of properties: then you use generics: 🤓
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;readOnlyProps&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="o"&gt;=&lt;/span&gt; 
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&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="nx"&gt;K&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;What that means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;T&lt;/code&gt; is representing avariable for the type that you will use&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;K&lt;/code&gt; is representing the key of each property in theT type&lt;/li&gt;
&lt;li&gt;you can think of it as an iterator that iterates over the passed types (postProps in this case)&lt;/li&gt;
&lt;li&gt;e.g. first iteration:
&amp;gt; Input 👉 readonly [name]: postProps[name] 
&amp;gt; output 👉 readonly name: string;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you can create your new type and use it:&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;// your new type:&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;readOnlyPostPorps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;readOnlyProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;postProps&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;// you could use the new type now:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;readOnlyPostPorps&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;JSX&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Element&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;your&lt;/span&gt; &lt;span class="nx"&gt;JSX&lt;/span&gt; &lt;span class="nx"&gt;goes&lt;/span&gt; &lt;span class="nx"&gt;here&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;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Try this live example:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/typescript-typing-hacks-w-generics-pm0d4m"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Any good in using Read-Only? looks like it does only one thing.
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Prevent bugs 🐞 by preventing developers from accidentally changing the value of a property.&lt;/li&gt;
&lt;li&gt;Improved maintainability 🔧 Code will become easier to maintain thanks to the first point (preventing bugs)&lt;/li&gt;
&lt;li&gt;Improved readability 👊 Code will become easier to read and understand by clearing out what can be changed and what cannot.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>vue</category>
    </item>
    <item>
      <title>TypeScript: Generics… How about a pizza?</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Mon, 15 May 2023 00:07:48 +0000</pubDate>
      <link>https://dev.to/taricov/typescript-generics-how-about-a-pizza-1b3c</link>
      <guid>https://dev.to/taricov/typescript-generics-how-about-a-pizza-1b3c</guid>
      <description>&lt;p&gt;You love pizza, you eat pizza, you order pizza but they deliver “Generics”… you ordered sometin’?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rkdJ2A6d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihxbr8fzd0nyw10sv7q0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rkdJ2A6d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihxbr8fzd0nyw10sv7q0.png" alt="Image description" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why you might choose to use TypeScript?
&lt;/h2&gt;

&lt;p&gt;There are too many perks and benefits you can get from integrating typescript into your projects, but there are 2 reasons in specific that really concern and affect the whole development process and leverage the developer experience:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Code &lt;strong&gt;Reliability&lt;/strong&gt;: ⚓️&lt;br&gt;
TypeScript is a statically typed language that can help catch errors and bugs at compile time before your code is even run. This can lead to more reliable and predictable code, especially in larger projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code &lt;strong&gt;Maintainability&lt;/strong&gt;: 🔧&lt;br&gt;
TypeScript’s type annotations can make your code easier to understand and navigate, especially for developers who are new to a codebase. Additionally, TypeScript’s features like interfaces and type aliases can help you create reusable, composable code.&lt;br&gt;
Put that in mind as you go in your Typescript journey and you will appreciate it. By the way, I will not talk much about any typing concepts in this article except Generics cuz i stumbled upon its (What is that???) and really want to demystify it so you can understand it better after i struggled a while.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What are generics?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Let’s first bring Pizza! 🍕
&lt;/h3&gt;

&lt;p&gt;So imagine we got a pizza, a full-rounded pizza, and you told the pizzeria “Don’t cut it into pieces, i need it as a whole” Now you take it home, and in your kitchen, you got geometric shapes of cutters, e.g. triangles/rectangles/circles/stars/squares; all of these cutters you use to cut your pizzas to have fun sharing star-shaped pizzas w/ your pals.&lt;/p&gt;

&lt;p&gt;But today, you have a big party and you may want to cut the pizza in a more formal yet free way so that you could cut smaller slices— or even cut each slice in a half and each half in a half Lol — so all can eat and enjoy .. NOW you know that you will have to bring a knife this time instead of those pre-defined specific shapes.&lt;/p&gt;

&lt;p&gt;And here's the bottom line: Generics is the knife that you use to custom-cut pizzas .. that’s it.&lt;/p&gt;

&lt;p&gt;It’s a tool you would use whenever you are uncertain about what types(shapes) you will use in a function(pizza)&lt;/p&gt;

&lt;h3&gt;
  
  
  After eating your pizza: How do generics work? 🤔
&lt;/h3&gt;

&lt;p&gt;Generics use a special type variable called &lt;code&gt;T&lt;/code&gt; and there are 2 points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It’s variable .. you know variables very well like const &lt;code&gt;oneVariable&lt;/code&gt; or &lt;code&gt;var anotherVariable;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;but the only difference, it’s a type variable, which means it’s only and all like if you do this: &lt;code&gt;let variable2 = typeof "some string"&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;T&lt;/code&gt; here is just a convention, you could call it &lt;code&gt;K&lt;/code&gt; it doesn’t matter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now, this &lt;code&gt;T&lt;/code&gt; is a type variable and it’s generic, and Oxford Dictionary says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generic: means general, not specified .. got it?! (yes oxford said that)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A generic type variable is like a parameter in functional programming that you could replace with arguments like so:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(x, y){ //x and y are parameters to place-hold for arguments
return x + y
}

add(3,4) //3 and 4 are arguments to test the function

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Example:
&lt;/h3&gt;

&lt;p&gt;Let’s say you want to write ONE function that adds any 2 things together and &lt;strong&gt;&lt;em&gt;still keep your code type-safe&lt;/em&gt;&lt;/strong&gt; .. hmm&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember ✋ we are talking all about types.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before I learn Generics, I’d do this I would you could write a function for each type like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addStr(a: string, b: string): string {
  return a + b;
}
function addNum(a: number, b: number): number {
  return a + b;
}

// Calling the functions 👇👇👇👇👇

addStr(1, 2); // 3
addNum("a", "b"); // "ab"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But now and while I’ve been learning how to use a knife, I’d write it like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add&amp;lt;T&amp;gt;(a: T, b: T): T {
  return &amp;lt;any&amp;gt;a + &amp;lt;any&amp;gt;b; 
//Type &amp;lt;any&amp;gt; here is just for escaping typescript infering unknown-ability
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function now can add any 2 things together, as long as both are the same type. For example, you could call it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add(1, 2); // 3
add("a", "b"); // "ab"
add({name: "John"}, {age: 10}); // {name: "John", age: 10}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and if you imagined that you could do this &lt;code&gt;add(2, "4")&lt;/code&gt; .. now you are getting that error:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ERROR: Argument of type ‘4’ is not assignable to parameter of type ‘2’&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Meaning that your types are safe even when you are not sure of the inputs and their type.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/typescript-generics-basics-kn61u8"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  So Why generics? I thought you know by now!
&lt;/h2&gt;

&lt;p&gt;You know by now why, right? but let’s list the benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Generics allow you to write code that is more 🔄 &lt;strong&gt;reusable&lt;/strong&gt;,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generics allow you to write code that is more ✅ &lt;strong&gt;type-safe&lt;/strong&gt;,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The compiler checks for the types of arguments you pass to the function and make sure they are &lt;strong&gt;compatible&lt;/strong&gt;, avoiding errors in ⚡️ &lt;strong&gt;Compile time&lt;/strong&gt; and even before running your app ⚡️&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The compiler infers the type of &lt;code&gt;T&lt;/code&gt; and this enables auto-completion to suggest only the available right options ✋&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;example: When you call the add function, as previously shown, with two numbers, the compiler will infer that the type of &lt;code&gt;T&lt;/code&gt; is &lt;code&gt;number&lt;/code&gt;, thus the auto-completer will complain when you use any other types other than numbers. MAGIC ha!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That’s it for today, till next time.. see ya 👊😉&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>generics</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>New Feature: React introduces its new experimental hook — useOptimisitcState()</title>
      <dc:creator>Taric Ov</dc:creator>
      <pubDate>Sun, 14 May 2023 23:53:18 +0000</pubDate>
      <link>https://dev.to/taricov/new-feature-react-introduces-its-new-experimental-hook-useoptimisitcstate-3e1d</link>
      <guid>https://dev.to/taricov/new-feature-react-introduces-its-new-experimental-hook-useoptimisitcstate-3e1d</guid>
      <description>&lt;p&gt;React has recently added an experimental hook to the family &lt;code&gt;useOptimisticState()&lt;/code&gt; that will allow us to manage optimistic state updates in a more functional way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9z7a7z0f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urqzc57sf76knh1vqsf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9z7a7z0f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urqzc57sf76knh1vqsf6.png" alt="Image description" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Opimistic! What does that mean?
&lt;/h2&gt;

&lt;p&gt;Optimistic state is a technique that allows updating the UI with new data before the data has been fully fetched from the server 🧐&lt;br&gt;
This can help to improve the user experience by making the UI more responsive.&lt;/p&gt;
&lt;h2&gt;
  
  
  Assumed syntax:
&lt;/h2&gt;

&lt;p&gt;As we have got used to ReactJs hooks’ syntactical building .. this is a mere assumed syntax for the new hook till we have more information about it.&lt;/p&gt;

&lt;p&gt;The useOptimisticState hook, here, will take 2 arguments: a function to fetch the data from the server, and a function to update the UI with the data. The fetch function should return a promise that resolves with the data. The update function should take the data as an argument and update the UI accordingly.&lt;/p&gt;
&lt;h2&gt;
  
  
  Example: Hook’s parts ( — The Breakdown)
&lt;/h2&gt;

&lt;p&gt;const [state, setState] = useState('');&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fetchData = async () =&amp;gt; {
  const data = await fetch('https://example.com/data');
  return data.json();
};

const updateUI = (data) =&amp;gt; {
  setState(data.name);
};

const [pending, updated] = useOptimisticState(fetchData, updateUI); 

//then use the result in JSX as follows:
{pending &amp;amp;&amp;amp; !updated ? "Saving.." : ""}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the previous example, we have 2 normal functions and one magical HOOK:&lt;/p&gt;

&lt;p&gt;The fetchData function will fetch the data from the server.&lt;br&gt;
The updateUI function will update the UI with the data.&lt;br&gt;
The useOptimisticState hook will the do the trick.&lt;/p&gt;
&lt;h2&gt;
  
  
  What happens behind the scenes? (4 steps — a, b, c, and d)
&lt;/h2&gt;

&lt;p&gt;a. The new hook will call the fetchData function.&lt;/p&gt;

&lt;p&gt;b. While at the same time, it will also update the UI accordingly.&lt;/p&gt;

&lt;p&gt;The server stays no change and never gets updated with the new data .. However, the UI gets updated 👉 assuming that the data query (POST/PUT/DELETE..etc) will succeed on the server-side (that’s why called optimistic? haa!)&lt;/p&gt;

&lt;p&gt;c. If the request to the server is a success ✅ the data gets updated on the server, and here the server is just matching the previously updated UI 🔥 the UI keeps its new state/update (no changes happen on the client-side)&lt;/p&gt;

&lt;p&gt;d. While if the fetchData function fails ❌ the hook will roll back the UI to its previous state ⏪&lt;/p&gt;
&lt;h2&gt;
  
  
  Do we need That new hook to go Optimistic?
&lt;/h2&gt;

&lt;p&gt;Optimistic State, as stated previously, is a technique used to allow the user interface to respond more effectively, thus improving the user experience.&lt;/p&gt;

&lt;p&gt;By the way, It’s all about a matter of choice and the way you choose to go with your application .. and even you can implement the technique by yourself whether by using other tools available in the market or doing manually from scratch .. I mean you do not need useOptimisticState() hook to implement the optimistic effect in our applications.&lt;/p&gt;

&lt;p&gt;Of those tools that have been around for a while: &lt;em&gt;[Quick Examples]&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Use a &lt;strong&gt;library&lt;/strong&gt; like &lt;code&gt;react-query&lt;/code&gt; or &lt;code&gt;swr&lt;/code&gt;:&lt;br&gt;
These libraries provide a number of features for managing asynchronous requests, including optimistic state.&lt;br&gt;
Use a &lt;strong&gt;custom&lt;/strong&gt; hook.&lt;br&gt;
A custom hook is a function that takes some state and returns a new state by creating a hook that takes the current state and the response from the server, and returns a new state.&lt;br&gt;
You can also manage optimistic state &lt;strong&gt;manually&lt;/strong&gt;.&lt;br&gt;
To do this, you will need to keep track of the current state of the application, and update the state based on the response from the server.&lt;br&gt;
Here’s an example on how to implement optimistic state update with a fake server:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/tutorial-optimisticstate-simulating-optimistic-state-updates"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>hooks</category>
      <category>feature</category>
    </item>
  </channel>
</rss>
