<?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: Ricardo Paul</title>
    <description>The latest articles on DEV Community by Ricardo Paul (@ricardopaul).</description>
    <link>https://dev.to/ricardopaul</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%2F285593%2F6a634d31-f94c-43fe-b916-0ab0e7414723.jpeg</url>
      <title>DEV Community: Ricardo Paul</title>
      <link>https://dev.to/ricardopaul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ricardopaul"/>
    <language>en</language>
    <item>
      <title>Typescript : Type guards simply explained.</title>
      <dc:creator>Ricardo Paul</dc:creator>
      <pubDate>Sat, 11 Sep 2021 18:46:45 +0000</pubDate>
      <link>https://dev.to/ricardopaul/typescript-type-guards-simply-explained-oik</link>
      <guid>https://dev.to/ricardopaul/typescript-type-guards-simply-explained-oik</guid>
      <description>&lt;p&gt;Today we're exploring a functionality in Typescript called &lt;strong&gt;type guards&lt;/strong&gt;, if the name sounds strange to you,know however this is a technique that you probably have used in plain Javascript if you've been coding in that language for a while.&lt;/p&gt;

&lt;p&gt;Type guards is a technique used in Typescript to get information about the type of a variable (to know what the type is) and this often happens within a conditional block like if/else. To achieve this, typescript makes use of some built-in javascript operators like &lt;code&gt;typeof&lt;/code&gt;, &lt;code&gt;instance of&lt;/code&gt;, the &lt;code&gt;in&lt;/code&gt; operator which is used to determine if an object contains a property.&lt;/p&gt;

&lt;p&gt;So this is enough theory already, let's go through some examples.&lt;br&gt;
Oh just before, type guards mostly come to use when we're dealing with a function whose parameter has a &lt;strong&gt;union type&lt;/strong&gt; as type annotation.&lt;br&gt;
If you're not familiar with Union Types, you can take it literally as a union of types, e.g:&lt;br&gt;
&lt;code&gt;Type UnionOfTypes = string | number&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's go through a failing code example to see where Type guards come in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;StringOrNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="c1"&gt;//our union type&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;processAmount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;StringOrNumber&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;amount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;//error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the snippet above, our function &lt;code&gt;processAmount()&lt;/code&gt; takes an argument &lt;code&gt;amount&lt;/code&gt; which can be either a string or a number.&lt;br&gt;
In the function body, we just assume that &lt;code&gt;amount&lt;/code&gt; is a number and try to add 10 to it. The compiler gets confused, it cannot add 10 (number) to &lt;br&gt;
amount (string | number) and thus signals us that there's an error:&lt;br&gt;
&lt;code&gt;Operator '+' cannot be applied to types 'StringOrNumber' and 'number'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Ok, so what are we doing wrong? Well, we are not narrowing the type of the argument &lt;code&gt;amount&lt;/code&gt;. We need a way to tell the compiler that it should treat &lt;br&gt;
our variable as a number and let us perform our operation. &lt;strong&gt;That's where type guards come in.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;StringOrNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;processAmount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;StringOrNumber&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&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;amount&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time, by using the &lt;code&gt;typeof&lt;/code&gt; operator within a conditional block, we tell the typescript compiler to treat &lt;code&gt;amount&lt;/code&gt; as a number within the scope and now our code can compile without errors.&lt;/p&gt;

&lt;p&gt;That is Type guards in a nutshell, remember that there are other operators used to perform Type guards and narrowing in Typescript like &lt;code&gt;instanceof&lt;/code&gt; and &lt;code&gt;in&lt;/code&gt;. I'll cover these in a future blog post.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>typeguards</category>
      <category>javascript</category>
    </item>
    <item>
      <title>What problem does Typescript solve ?</title>
      <dc:creator>Ricardo Paul</dc:creator>
      <pubDate>Sat, 04 Sep 2021 20:59:55 +0000</pubDate>
      <link>https://dev.to/ricardopaul/what-problem-does-typescript-solve-5fka</link>
      <guid>https://dev.to/ricardopaul/what-problem-does-typescript-solve-5fka</guid>
      <description>&lt;p&gt;If you're thinking about learning Typescript but not sure about the values it brings to the table, or you're already learning it and need to understand better its purpose, here I try to give a brief overview about what Typescript is.&lt;/p&gt;

&lt;p&gt;When we are writing javascript, or other dynamically typed languages, we have to keep information about the type of a variable in our head and hope that we use the value properly. Let's see a quick example.&lt;/p&gt;

&lt;p&gt;Consider how many operations we can run on a &lt;code&gt;message&lt;/code&gt; variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//we can try to get message length: &lt;/span&gt;
&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;

&lt;span class="c1"&gt;//we may attempt to call message with an argument:&lt;/span&gt;
&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But wait, what is &lt;code&gt;message&lt;/code&gt; exactly? What is the Type of the message variable? Is it a string? an array or a function? How do we even know &lt;code&gt;message&lt;/code&gt; takes an argument and the argument is a string?&lt;/p&gt;

&lt;p&gt;Well, in javascript, if we have doubts about the type - maybe we fail to remember it, maybe we are working with a codebase we're not quite familiar with yet - we basically rely on the Javascript runtime to throw a &lt;code&gt;TypeError&lt;/code&gt; which will suggest to us that we were wrong about the type of the variable.&lt;/p&gt;

&lt;p&gt;So you can imagine if &lt;code&gt;message&lt;/code&gt; was declared as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Typescript&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the first line of code would work, but the second would throw a TypeError telling us &lt;code&gt;message is not a function&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is because javascript is a dynamically typed language, it checks the type of &lt;code&gt;message&lt;/code&gt; at runtime (while the program is running) and thus cannot tell you if there's a problem with your code before you run it.&lt;/p&gt;

&lt;p&gt;Now if you're working with a small codebase you may think that's not a problem not having everything typed, but things get messy pretty quickly with little bugs here and there as your codebase starts to become larger. It would be great if we could solve that problem. That's where typescript comes in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;static type checking&lt;/strong&gt;&lt;br&gt;
Typescript is a static type checker for javascript, it is called static because it runs before your code runs and it is a type checker because it tells you&lt;br&gt;
    ahead of time if there's something wrong with your code as it relates to how you attempt to use different types.&lt;br&gt;
    Coming back to our example earlier, when we tried to call message with an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Typescript&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;
&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//(Error) This expression is not callable.&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Typescript would show a red squiggly line under message and would tell us&lt;br&gt;
    &lt;code&gt;This expression is not callable.   Type 'String' has no call signatures.&lt;/code&gt;&lt;br&gt;
So we can infer from this message that our variable is not a function but a string, attempting to call it will break our program.&lt;/p&gt;

&lt;p&gt;At a high level this is what Typescript does, it adds a type system to javascript to help us developers catch bugs in our program early on, in the example above we relied on Typescript Type Inference ability so we didn't have to explicitly tell typescript that &lt;code&gt;message&lt;/code&gt; is a string, we'll learn more about explicit types in a later post and how typescript deals with them.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
