<?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: Alex Menor</title>
    <description>The latest articles on DEV Community by Alex Menor (@alexmenor).</description>
    <link>https://dev.to/alexmenor</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%2F218023%2F30e0a5a4-3ae3-403b-b988-a75b716787eb.png</url>
      <title>DEV Community: Alex Menor</title>
      <link>https://dev.to/alexmenor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexmenor"/>
    <language>en</language>
    <item>
      <title>Do you really know TypeScript? (4): Type assertions</title>
      <dc:creator>Alex Menor</dc:creator>
      <pubDate>Thu, 06 Jan 2022 18:25:55 +0000</pubDate>
      <link>https://dev.to/alexmenor/do-you-really-know-typescript-4-type-assertions-2644</link>
      <guid>https://dev.to/alexmenor/do-you-really-know-typescript-4-type-assertions-2644</guid>
      <description>&lt;p&gt;In this post (the last of the series 😢) we are going to understand type assertions and compare them against type declarations.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do you use type assertions for?
&lt;/h2&gt;

&lt;p&gt;There are situations where you know more than TypeScript can infer.&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;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="o"&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;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;FileItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="nx"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;extension&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// We know for sure that item&lt;/span&gt;
&lt;span class="c1"&gt;// is also a file&lt;/span&gt;
&lt;span class="nx"&gt;printFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;File&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Golden rule for using assertions
&lt;/h2&gt;

&lt;p&gt;You can only assert from one type to another if either type is a subset of the other. 🧐&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;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;numOfDoors&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;type&lt;/span&gt; &lt;span class="nx"&gt;Airplane&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;numOfEngines&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;car&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;numOfDoors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Conversion of type 'Car' to type 'Airplane' may be a mistake&lt;/span&gt;
&lt;span class="c1"&gt;// because neither type sufficiently overlaps with the other. &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;airplane&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Airplane&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An exception to this rule is when using &lt;code&gt;unknown&lt;/code&gt; or &lt;code&gt;any&lt;/code&gt;.&lt;br&gt;
You can use these to bypass it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;unknown&lt;/code&gt; because is the universal set&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;any&lt;/code&gt; because disables the type checking
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;airplane&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Airplane&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Prefer type declarations to type assertions
&lt;/h2&gt;

&lt;p&gt;This is a mistake that I've seen a lot!&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;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;numOfDoors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="na"&gt;numOfAirbags&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="c1"&gt;// Error: Property 'numOfAirbags' is missing&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car&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;numOfDoors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// No error&lt;/span&gt;
&lt;span class="kd"&gt;const&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;numOfDoors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you use type assertions you are telling TypeScript to get out of the way, with type declarations you are making your intentions clear so it can help you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is &lt;code&gt;as const&lt;/code&gt; a type assertion?
&lt;/h2&gt;

&lt;p&gt;It is not. &lt;br&gt;
Despite having a similar syntax, &lt;code&gt;as const&lt;/code&gt; is used to hint the type system about values being immutable.&lt;/p&gt;

&lt;p&gt;It is very situational, but could be useful for using the values of an array as literals, 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;coolBands&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Oasis&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AC/DC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Foo Fighters&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;

&lt;span class="c1"&gt;// type CoolBands = "Oasis" | "AC/DC" | "Foo Fighters"&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CoolBands&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;coolBands&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or for using the values of an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;coolBandsAndSingers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Oasis&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Liam Gallagher&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AC/DC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brian Johnson&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Foo Fighters&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dave Grohl&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;

&lt;span class="c1"&gt;// type CoolBands = "Oasis" | "AC/DC" | "Foo Fighters"&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CoolBands&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;coolBandsAndSingers&lt;/span&gt;

&lt;span class="c1"&gt;// type CoolSingers = "Liam Gallagher" | "Brian Johnson" | "Dave Grohl"&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CoolSingers&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;coolBandsAndSingers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;CoolBands&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As it is the last post of this series, I also want to go through some topics that couldn't get a post for their own. &lt;/p&gt;

&lt;h2&gt;
  
  
  Don't type everything!
&lt;/h2&gt;

&lt;p&gt;I did it, and probably so did you. &lt;/p&gt;

&lt;p&gt;It is not bad, but can make the code too verbose and therefore harder to read.&lt;/p&gt;

&lt;p&gt;As a rule of thumb, you should type very well:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function and method signatures&lt;/strong&gt; (parameters and return types)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variables and constants when using object literals&lt;/strong&gt;, to take advantage of excess property checking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a TDD like spirit, you should know your input and output types before implementing a function/method, so typing it from the beginning makes it easier for you to implement it.&lt;/p&gt;

&lt;p&gt;Typing return types usually avoids implementation errors, specially if your function has many "paths".&lt;/p&gt;

&lt;h2&gt;
  
  
  Don’t use uppercase variants of primitive types
&lt;/h2&gt;

&lt;p&gt;Probably you noticed that &lt;code&gt;String&lt;/code&gt; or &lt;code&gt;Number&lt;/code&gt; exist and wonder if you should use them as types.&lt;/p&gt;

&lt;p&gt;The answer is no. Just stick to lowercase types for primitives &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;These uppercase variants exist primarily for convenience, 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="c1"&gt;// charAt is not a property of&lt;/span&gt;
&lt;span class="c1"&gt;// the string primitive&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hey&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;charAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;JavaScript wraps the &lt;code&gt;string&lt;/code&gt; primitive in &lt;code&gt;String&lt;/code&gt; under the hood and uses the &lt;code&gt;charAt&lt;/code&gt; method of &lt;code&gt;String&lt;/code&gt; and then throws that object away.&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;// These wrappers don't have behave &lt;/span&gt;
&lt;span class="c1"&gt;// as primitives&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hey&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;It's been a pleasure to write this series and I wish you a very productive experience with TypeScript 🙂&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources to go deeper
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.goodreads.com/en/book/show/48570456-effective-typescript"&gt;Effective TypeScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/"&gt;TypeScript docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Do you really know TypeScript? (3): Types and interfaces</title>
      <dc:creator>Alex Menor</dc:creator>
      <pubDate>Sun, 02 Jan 2022 10:52:25 +0000</pubDate>
      <link>https://dev.to/alexmenor/do-you-really-know-typescript-3-types-and-interfaces-13k3</link>
      <guid>https://dev.to/alexmenor/do-you-really-know-typescript-3-types-and-interfaces-13k3</guid>
      <description>&lt;p&gt;One of the things that you will do the most with Typescript is to define the shape of objects with &lt;code&gt;type&lt;/code&gt; or &lt;code&gt;interface&lt;/code&gt;. For that reason, understanding both well will make your TypeScript better quickly.&lt;/p&gt;

&lt;p&gt;Let's see their main differences aside of the syntax, common patterns and surprising behaviours.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;type&lt;/code&gt; can be used for more things
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;interface&lt;/code&gt; is used just to define the shape of objects, &lt;code&gt;type&lt;/code&gt; has other use cases.&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;Pet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cat&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;Dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CoolNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.1416&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;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Interface merging
&lt;/h2&gt;

&lt;p&gt;You should be aware of this one.&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;DesktopFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;icon&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;DesktopFile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;extension&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;// Error: Property 'icon' is missing in type '{ extension: string; }' &lt;/span&gt;
&lt;span class="c1"&gt;// but required in type 'DesktopFile'.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DesktopFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;extension&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pdf&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can be surprising that you can redeclare an interface and merge them!&lt;/p&gt;

&lt;p&gt;This is also known as "interface augmenting" and can be desirable in some situations but is definitely unusual in other languages.&lt;/p&gt;

&lt;p&gt;Note that using &lt;code&gt;Type&lt;/code&gt; would result in an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Discriminated union
&lt;/h2&gt;

&lt;p&gt;Also known as "tagged union", is a frequent pattern in TypeScript.&lt;/p&gt;

&lt;p&gt;It can be weird if you are used to polymorphism using classes, but since TypeScript's types disappear at runtime, you need to do things a bit different.&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;File&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;file&lt;/span&gt;&lt;span class="dl"&gt;'&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;extension&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;Folder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;folder&lt;/span&gt;&lt;span class="dl"&gt;'&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;filesInside&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;DesktopItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Folder&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DesktopItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;file&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// TypeScript knows that the properties&lt;/span&gt;
    &lt;span class="c1"&gt;// of the type File are defined here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be used like &lt;code&gt;instanceof&lt;/code&gt; in other languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Union of types vs types of unions
&lt;/h2&gt;

&lt;p&gt;Generally prefer &lt;strong&gt;union of types&lt;/strong&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;type&lt;/span&gt; &lt;span class="nx"&gt;Vehicle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;motorcycle&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;car&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

    &lt;span class="na"&gt;numberOfWheels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; 
    &lt;span class="na"&gt;numberOfAirbags&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;vehicle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Vehicle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;vehicle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;car&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// TypeScript still thinks that&lt;/span&gt;
    &lt;span class="c1"&gt;// numberOfAirbags could be undefined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;If we used union of types instead, like in the "discriminated union" example, TypeScript can be sure that the &lt;code&gt;Car&lt;/code&gt; properties are available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Excess property checking
&lt;/h2&gt;

&lt;p&gt;This is a mechanism that can mess your mental model of structural typing when using &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;interface&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Cat&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;whiskersLength&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="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cat&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;Uxia&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;whiskersLength&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;bestFriend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Nina&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="c1"&gt;// ~~~~~~~~~~~~~~~~~~ Object literal may only specify known properties,&lt;/span&gt;
&lt;span class="c1"&gt;//                    and 'bestFriend' does not exist in type 'Cat'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From a structural typing point of view it is valid as the defined object contains at least the properties declared for &lt;code&gt;Cat&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is excess property checking complaining though. &lt;/p&gt;

&lt;p&gt;Check out this case:&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;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;zipCode&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;randomGuy&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="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;Pedro&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;45420&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Excess property checking quickly points out an error that we could've spent too much time looking for otherwise.&lt;/p&gt;

&lt;p&gt;Note that this check only happens when using object literals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should I use &lt;code&gt;type&lt;/code&gt; or &lt;code&gt;interface&lt;/code&gt; then?
&lt;/h2&gt;

&lt;p&gt;I find &lt;code&gt;type&lt;/code&gt; easier to reason about and more readable. &lt;/p&gt;

&lt;p&gt;One exception would be when extending types:&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;Flyable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&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;Airplane&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Flyable&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Helicopter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Flyable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, as we saw earlier, "interface augmenting" can be unfamiliar to many people.&lt;/p&gt;

&lt;p&gt;Be aware of their differences, try to make your team agree in their uses for the sake of consistency and you will be fine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources to go deeper
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.goodreads.com/en/book/show/48570456-effective-typescript"&gt;Effective TypeScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/"&gt;TypeScript docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Do you really know TypeScript? (2): Being strict</title>
      <dc:creator>Alex Menor</dc:creator>
      <pubDate>Mon, 27 Dec 2021 14:29:10 +0000</pubDate>
      <link>https://dev.to/alexmenor/do-you-really-know-typescript-2-being-strict-2edc</link>
      <guid>https://dev.to/alexmenor/do-you-really-know-typescript-2-being-strict-2edc</guid>
      <description>&lt;h2&gt;
  
  
  Strict settings
&lt;/h2&gt;

&lt;p&gt;The TypeScript transpiler has an overwhelming set of options 🥵, but don't worry, you don't have to know them all.&lt;/p&gt;

&lt;p&gt;Though, you should know these two really well:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;noImplicitAny&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;strictNullChecks&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When &lt;code&gt;noImplicitAny&lt;/code&gt; is enabled all variables must have a known 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;function&lt;/span&gt; &lt;span class="nx"&gt;greet&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello, &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="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you hover over the function (and your IDE has TypeScript capabilities) you will see that it infers that &lt;code&gt;name&lt;/code&gt; is of type &lt;code&gt;any&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can say that &lt;code&gt;name&lt;/code&gt; is implicitly of type &lt;code&gt;any&lt;/code&gt;, and if &lt;code&gt;noImplicitAny&lt;/code&gt; is disabled TypeScript will rightfully complain 🙂&lt;/p&gt;

&lt;p&gt;As we'll see more in depth later, &lt;code&gt;any&lt;/code&gt; bypasses TypeScript's type checks, making values of the &lt;code&gt;any&lt;/code&gt; type assignable to anything.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;any&lt;/code&gt; type should generally be your last resort and if you really need to use it, you have to do so explicitly if &lt;code&gt;noImplicitAny&lt;/code&gt; is enabled.&lt;/p&gt;

&lt;p&gt;Although &lt;code&gt;noImplicitAny&lt;/code&gt; enables you to make the most out of TypeScript, it can be tough to have this setting enabled if you are migrating your codebase from JavaScript, for example.&lt;/p&gt;

&lt;p&gt;As we already mentioned, you can see types in TypeScript as sets of values.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;strictNullChecks&lt;/code&gt; controls if &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; are part of every 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;const&lt;/span&gt; &lt;span class="nx"&gt;jame&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="kc"&gt;null&lt;/span&gt;

 &lt;span class="c1"&gt;// It'll throw "cannot read 'greet' of undefined" at runtime&lt;/span&gt;
 &lt;span class="nx"&gt;jame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is going to throw an error when you execute it. &lt;br&gt;
But, with &lt;code&gt;strictNullChecks&lt;/code&gt; enabled, TypeScript will tell you at compile time instead:&lt;br&gt;
&lt;code&gt;Type 'null' is not assignable to type 'Person'.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are more "strict" settings that modulate how picky TypeScript is and you can turn them all on with &lt;code&gt;strict: true&lt;/code&gt;. &lt;br&gt;
I would advise you to do so, specially if you are starting a project from scratch.&lt;/p&gt;
&lt;h2&gt;
  
  
  Handling type edge cases
&lt;/h2&gt;

&lt;p&gt;Before introducing the empty and universal sets as promised, we have to talk about &lt;code&gt;any&lt;/code&gt;, which is often perceived as the universal set.&lt;/p&gt;

&lt;p&gt;What should I use &lt;code&gt;any&lt;/code&gt; for, then?&lt;/p&gt;

&lt;p&gt;TypeScript is a gradual type system, you can type some parts of your code and leave others untyped. &lt;code&gt;any&lt;/code&gt; enables that, disabling the type checks. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can assign a value of the &lt;code&gt;any&lt;/code&gt; type to anything&lt;/li&gt;
&lt;li&gt;You can assign anything to a variable of the &lt;code&gt;any&lt;/code&gt; type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;any&lt;/code&gt; doesn't fit in the "type as a set of values" model, since a set cannot be a subset and a superset of everything at the same time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// No errors even with strict: true&lt;/span&gt;
&lt;span class="kd"&gt;const&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;
&lt;span class="kd"&gt;const&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;any&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.1416&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Be specially cautious when it comes to using &lt;code&gt;any&lt;/code&gt; as a return type as it can spread to other well typed parts of your code that make use of said function.&lt;/p&gt;

&lt;h3&gt;
  
  
  The universal set
&lt;/h3&gt;

&lt;p&gt;Important points of &lt;code&gt;unknown&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any type is assignable to &lt;code&gt;unknown&lt;/code&gt; because every type is a subset of it.&lt;/li&gt;
&lt;li&gt;But &lt;code&gt;unknown&lt;/code&gt; is not assignable to any type but itself (or &lt;code&gt;any&lt;/code&gt;) because it is not the subset of any other type.&lt;/li&gt;
&lt;li&gt;Attempting to access a property on a value of the type &lt;code&gt;unknown&lt;/code&gt; is an error. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The last point is key, specially when using it as an alternative to &lt;code&gt;any&lt;/code&gt; for edge cases when we really don't know the return type of a function, for example.&lt;br&gt;
When using &lt;code&gt;unknown&lt;/code&gt;, the untyped code doesn't spread as we need to narrow the types in it in order to use it.&lt;/p&gt;

&lt;p&gt;Besides narrowing it with an assertion, some libraries use generics for 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;function&lt;/span&gt; &lt;span class="nx"&gt;query&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;q&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="kd"&gt;const&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;select * from user&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;h3&gt;
  
  
  The empty set
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;never&lt;/code&gt; type is the opposite of &lt;code&gt;unknown&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nothing is assignable to &lt;code&gt;never&lt;/code&gt; because no set is a subset of the empty set.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;never&lt;/code&gt; is assignable to everything, because the empty set is the subset of every set.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The use of &lt;code&gt;never&lt;/code&gt; is not as frequent as &lt;code&gt;unknown&lt;/code&gt; but it does have an use case that I like a lot called &lt;strong&gt;exhaustive type checking:&lt;/strong&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;type&lt;/span&gt; &lt;span class="nx"&gt;SpanishChampionsWinners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Real Madrid&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;Barcelona&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;


&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getChampionsCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;SpanishChampionsWinners&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="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Real Madrid&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="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Barcelona&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default&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;exhaustiveCheck&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`We forgot: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;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;If one day "Atlético de Madrid" wins a Champions title, adding it to the &lt;code&gt;SpanishChampionsWinners&lt;/code&gt; type will make this code complain since no value is assignable to &lt;code&gt;never&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to remember:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Be as strict as possible with your TypeScript settings and know &lt;code&gt;noImplicitAny&lt;/code&gt; and &lt;code&gt;strictNullChecks&lt;/code&gt; well.&lt;/li&gt;
&lt;li&gt;Understand that &lt;code&gt;any&lt;/code&gt; does not fit in the "types as sets" model, being a mechanism to avoid types in parts of your code.&lt;/li&gt;
&lt;li&gt;Try to isolate the untyped parts of your code and be aware of the &lt;code&gt;any&lt;/code&gt; spreading.&lt;/li&gt;
&lt;li&gt;Understand why &lt;code&gt;unknown&lt;/code&gt; is preferable to &lt;code&gt;any&lt;/code&gt; when handling edge cases.&lt;/li&gt;
&lt;li&gt;Get the idea of &lt;code&gt;never&lt;/code&gt; and use it for exhaustive checking.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Resources to go deeper
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.goodreads.com/en/book/show/48570456-effective-typescript"&gt;Effective TypeScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/"&gt;TypeScript docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>typescript</category>
      <category>node</category>
    </item>
    <item>
      <title>Do you really know TypeScript? (1): Thinking in sets</title>
      <dc:creator>Alex Menor</dc:creator>
      <pubDate>Sat, 25 Dec 2021 17:14:29 +0000</pubDate>
      <link>https://dev.to/alexmenor/do-you-really-know-typescript-1-thinking-in-sets-55dm</link>
      <guid>https://dev.to/alexmenor/do-you-really-know-typescript-1-thinking-in-sets-55dm</guid>
      <description>&lt;p&gt;I know many people, including myself, whose first experience with TypeScript was to write annotations in some variables and to add &lt;code&gt;as any&lt;/code&gt; until the transpiler stopped complaining.&lt;/p&gt;

&lt;p&gt;But at some point you realize that you &lt;strong&gt;really&lt;/strong&gt; should dig a little deeper into TypeScript and finally understand those errors.&lt;/p&gt;

&lt;p&gt;It's true that you can technically build the same websites/systems with and without TypeScript, but its benefits are huge:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Catch errors at compile time instead of at runtime&lt;/li&gt;
&lt;li&gt;Autocomplete ("it was &lt;code&gt;user.phone&lt;/code&gt; or &lt;code&gt;user.phoneNumber&lt;/code&gt;?" 🤔)&lt;/li&gt;
&lt;li&gt;Language services (enabling some IDE's refactors for example)&lt;/li&gt;
&lt;li&gt;Better domain modelling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using TypeScript is not only investing in the code's maintainability, it is also investing in the developer's productivity.&lt;/p&gt;

&lt;p&gt;Okay, you are (or were already) convinced that TypeScript is worth learning, what is this series for?&lt;/p&gt;

&lt;p&gt;In this series I will go through many aspects of the language that were not obvious to me even after having already done some projects with it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You will stop wrestling with TypeScript 🤼‍♀️&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Seeing types as sets of values
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// what can we assign to it?&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&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 can assign any value that belongs to a subtype of &lt;code&gt;Bar&lt;/code&gt;.&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%2F4vi9ps43y8t4p48cl1m2.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%2F4vi9ps43y8t4p48cl1m2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One cool thing of this system is that you can use set operators (among others) to create new types.&lt;/p&gt;

&lt;p&gt;For example the union operator: &lt;code&gt;number | string&lt;/code&gt; is the set of all numbers and strings. &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%2Fdnw4kqmr4qub7mfgxxo6.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%2Fdnw4kqmr4qub7mfgxxo6.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Structural typing
&lt;/h2&gt;

&lt;p&gt;To know if a value belongs or not to a type, TypeScript only focuses on the shape that it has.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Person&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="c1"&gt;// We didn't use the "new" keyword&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&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="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;Jame&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;That wouldn't be possible in nominal type systems.&lt;/p&gt;

&lt;p&gt;Let's see a slightly harder 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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;File&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
    &lt;span class="na"&gt;extension&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;DesktopItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;File&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Folder&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;DesktopItem&lt;/code&gt; contains all the objects that either have the properties (name and type) of &lt;code&gt;File&lt;/code&gt; or &lt;code&gt;Folder&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DesktopItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;couldBeFileOrFolder&lt;/span&gt;

&lt;span class="c1"&gt;// should work, right?&lt;/span&gt;
&lt;span class="nx"&gt;givenItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extension&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It doesn't work because only &lt;code&gt;File&lt;/code&gt; has that property and the specific type of &lt;code&gt;item&lt;/code&gt; could be &lt;code&gt;Folder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When we declare the union of two types the result is a type that has the intersection of the properties. In this case objects with a property &lt;code&gt;name&lt;/code&gt; of type &lt;code&gt;string&lt;/code&gt;, because it is the only property that they have in common.&lt;/p&gt;

&lt;p&gt;It's also true the other way around: The intersection of two types results in a type that has the union of the properties.&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="nf"&gt;keyof &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;keyof &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The empty set and the universal set
&lt;/h2&gt;

&lt;p&gt;If we can think of types as sets of values, what types are the empty set and the universal set?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;never&lt;/code&gt; is the empty set and &lt;code&gt;unknown&lt;/code&gt; is the universal set.&lt;/p&gt;

&lt;p&gt;These are both very special and we'll talk about them soon.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to remember
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Types are sets of values&lt;/li&gt;
&lt;li&gt;One value of type &lt;code&gt;Y&lt;/code&gt; is assignable to a type &lt;code&gt;X&lt;/code&gt; if &lt;code&gt;Y&lt;/code&gt; is a subtype of &lt;code&gt;X&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Type compatibility in TypeScript is determined by structural typing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Resources to go deeper
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.goodreads.com/en/book/show/48570456-effective-typescript" rel="noopener noreferrer"&gt;Effective TypeScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/" rel="noopener noreferrer"&gt;TypeScript docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>programming</category>
      <category>productivity</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The known unknowns and learning on demand</title>
      <dc:creator>Alex Menor</dc:creator>
      <pubDate>Fri, 11 Dec 2020 19:10:34 +0000</pubDate>
      <link>https://dev.to/alexmenor/the-unknown-unknowns-and-learning-on-demand-2gi5</link>
      <guid>https://dev.to/alexmenor/the-unknown-unknowns-and-learning-on-demand-2gi5</guid>
      <description>&lt;p&gt;At the moment you put a feet in the cold water of software development, it's likely you scream and back away. You are probably overwhelmed with what it seems a forest of buzzwords.&lt;/p&gt;

&lt;p&gt;While it's true that it's a big and fast evolving field, there are ways to benefit from that, and the how, it's what I want to talk about today.&lt;/p&gt;

&lt;h1&gt;
  
  
  The knowns and the unknowns
&lt;/h1&gt;

&lt;p&gt;We can classify everything in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Known knowns&lt;/strong&gt;: The things we know we know. For a newbie programmer might be to write a little script in python that reverses the input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Known unknowns&lt;/strong&gt;: The things we know we don't know. For the same young programmer as before, may be send data over a network. He knows it can be done, he just doesn't know exactly how yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unknown unknowns&lt;/strong&gt;: The things we don't know we don't know. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first category is vital because it enables us to immediately solve real world problems.&lt;/p&gt;

&lt;p&gt;But I think that the second category is underrated. The known unknowns have a very low entry fee and &lt;strong&gt;it enables us to learn something on demand in order to solve a problem.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Why invest time in the known unknowns
&lt;/h1&gt;

&lt;p&gt;I wish I got everything in the first category, but it's just impossible. Things keep evolving no matter what, there are simply too many things and it's costly in time to learn it all.&lt;/p&gt;

&lt;p&gt;What is possible though, is to have a broad second category, that is: &lt;strong&gt;Have lots of known unknowns.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;It has a very high value for the time it costs and you are probably not going to need to know all of it anyway. &lt;/p&gt;

&lt;p&gt;For example: I never had to implement an event driven architecture, but I am &lt;strong&gt;familiar enough&lt;/strong&gt; with the concept and its use cases.&lt;/p&gt;

&lt;p&gt;Don't get me wrong, the devil is in the details and I would have to invest an important amount of time to learn how to implement it, &lt;strong&gt;but knowing that exists and what problems solves makes you a better developer already.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;The software development field is too big, it's impossible to know every framework, tool or language that exists.&lt;/li&gt;
&lt;li&gt;But that's okay, just start making a breadth-first search: Take a look at &lt;a href="//roadmap.sh"&gt;some roadmaps&lt;/a&gt; for example.&lt;/li&gt;
&lt;li&gt;If you encounter any concept you don't know, spend 5 minutes reading about it and how could be useful to you in the future.&lt;/li&gt;
&lt;li&gt;When a perfect use case comes for something you know that you don't know, invest time to learn it.&lt;/li&gt;
&lt;li&gt;Learn something this way is easier, because there is a real reason behind.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@chalis007?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;胡 卓亨&lt;/a&gt;&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>career</category>
      <category>student</category>
      <category>learn</category>
    </item>
    <item>
      <title>Is "fundamentals first" the best approach to learning web development?</title>
      <dc:creator>Alex Menor</dc:creator>
      <pubDate>Thu, 20 Aug 2020 16:27:34 +0000</pubDate>
      <link>https://dev.to/alexmenor/is-fundamentals-first-the-best-approach-to-learning-web-development-4m29</link>
      <guid>https://dev.to/alexmenor/is-fundamentals-first-the-best-approach-to-learning-web-development-4m29</guid>
      <description>&lt;p&gt;A bit more than a year ago I started learning web development. Coming from a Computer Science background, where the I/O with computers is usually based on a terminal, they were exciting times. &lt;/p&gt;

&lt;p&gt;So I started focusing on fundamentals as most people recommended: HTML, CSS and Javascript. &lt;/p&gt;

&lt;p&gt;As I was doing this in my free time while also studying my degree I got exhausted by the time I got to Javascript and had to put it down for a while.&lt;/p&gt;

&lt;p&gt;When I picked it up again, I progressed much faster through Javascript and started learning React. It was like unlocking a tool I could got creative with. I started enjoying coding.&lt;/p&gt;

&lt;p&gt;Now with some perspective, I wonder what would have happened if I had started the other way around. Actually, a thread from &lt;a href="https://twitter.com/dan_abramov"&gt;Dan Abramov&lt;/a&gt; got me wondering.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Apparently this needs to be said but you can absolutely start with React and skip “JS fundamentals” IF YOU WANT. You’ll be confused about some things for sure (!), but for some people (incl. me) getting something on the screen is the only thing sustaining the initial interest&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My past self would have argued the same, you would get confused about many things along the way, and while it's true, he makes a good point:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;People keep answering: “but they will be confused”. YES they will be confused. The goal is not to eliminate confusion from the learning process, it is to not to lose people who give up because what they’re learning feels too far from what they want to be doing (make an app).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I even got confused going through fundamentals first. Specially with CSS. I remembered the moment I understood where flexbox was useful, like six months after "learning" it. &lt;/p&gt;

&lt;p&gt;You are not going to see the importance of advanced topics of JS and CSS in the beginning, while is a totally worthy effort, you understand why much later. &lt;/p&gt;

&lt;p&gt;The point I want to make is that while it's good for you to master the fundamentals, you realize it much later, which can discourage you to keep learning.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note I didn’t say you never need to go lower level. You do — you will get stuck otherwise. And after you learn it, you’ll be convinced you “should have” learned that thing first, because you have an emotional understanding of why it mattered now.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this is the most important thing for me: You &lt;strong&gt;should master fundamentals,&lt;/strong&gt; that is no deal. The question is &lt;strong&gt;when&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Some people hold that should be in the beginning, perhaps biased by the former point. &lt;/p&gt;

&lt;p&gt;I personally feel that you can set three phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get the basics of HTML, CSS and Javascript right. Don't get burned out.&lt;/li&gt;
&lt;li&gt;Pick up a framework, &lt;strong&gt;but keep in mind&lt;/strong&gt; that you have some gaps to fill later on. &lt;/li&gt;
&lt;li&gt;Master the basics on demand as you make projects.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You get a motivation boost in phase 1 because &lt;strong&gt;you are starting&lt;/strong&gt;, in phase 2 because you are finally &lt;strong&gt;more powerful than before&lt;/strong&gt; and in phase 3 as you &lt;strong&gt;progress doing and getting your hands dirty.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, this is just my opinion. The most important thing is that you keep learning and feeling motivated to do so.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/dan_abramov/status/1295667530800025600?s=20"&gt;This&lt;/a&gt; is the thread I was talking about, thanks for reading 👋🏻.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>healthydebate</category>
      <category>codenewbie</category>
      <category>career</category>
    </item>
    <item>
      <title>The GitHub Student Pack free resources that you shouldn't miss</title>
      <dc:creator>Alex Menor</dc:creator>
      <pubDate>Sat, 02 May 2020 18:51:37 +0000</pubDate>
      <link>https://dev.to/alexmenor/the-github-student-pack-free-resources-that-you-shouldn-t-miss-630</link>
      <guid>https://dev.to/alexmenor/the-github-student-pack-free-resources-that-you-shouldn-t-miss-630</guid>
      <description>&lt;p&gt;Since 2014, GitHub have been providing students with free access to some tools that enable them to practice in a real world environment. In the moment I'm writing this, there are 107 different offers that you can take advantage of. I've been able to try several of them in the past year. Which of them are worth the most? &lt;/p&gt;

&lt;h2&gt;
  
  
  Heroku's free two year &lt;em&gt;dyno&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;I'm very glad I started messing around with deployments of code and &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; was a perfect introduction to the matter. Heroku is a &lt;em&gt;Platform as a service&lt;/em&gt; that allows you to deploy any server side code such as &lt;em&gt;Node, Ruby or Python&lt;/em&gt; from a Git repository, abstracting you from the infrastructure details. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Heroku offers a free plan &lt;em&gt;dyno&lt;/em&gt;: It sleeps after 30 minutes without receiving any request and has limited CPU and memory resources.&lt;/li&gt;
&lt;li&gt;With the GitHub Student Pack offer we got up to 2 years of a free hobby plan, which never sleeps and includes SSL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks to this, is very easy to host a side project in the cloud with no infrastructure costs for a while.&lt;/p&gt;

&lt;h2&gt;
  
  
  Infrastructure as a service with Digital Ocean and Azure
&lt;/h2&gt;

&lt;p&gt;Once I was confident with Heroku IaaS such as AWS got my attention. Sadly you can't find Amazon Web Service between the resources provided by this pack, though, you have other alternatives.&lt;/p&gt;

&lt;h3&gt;
  
  
  Azure
&lt;/h3&gt;

&lt;p&gt;Although the IaaS by Microsoft doesn't lack any of the products that you would expect, I didn't find it very beginner friendly at all (so isn't AWS) and I personally struggled to get the code deployments done or to setup a NoSQL database. When I finally could get everything up and running I realized that I had wasted the 100$ free Azure credits somehow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Digital Ocean
&lt;/h3&gt;

&lt;p&gt;Even though I've read bad things about this service I found it very straightforward and powerful on demand. It's storage uses the AWS S3 Bucket API (which makes things easier if you want to migrate later to the Amazon platform) and I learnt to setup a &lt;em&gt;Docker Swarm&lt;/em&gt; thanks to its &lt;em&gt;Doplets.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can get 50$ in credits for Digital Ocean, and even though it's half the offer of Azure, they lasted me much longer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Free domain: Namecheap, &lt;a href="http://name.com"&gt;name.com&lt;/a&gt; and .tech
&lt;/h2&gt;

&lt;p&gt;A landing page is something that many developers have and a chance to show the world some of your projects or to stand out between candidates for a job offer.&lt;/p&gt;

&lt;p&gt;I tried these three providers but I could only made the student offer work with the later.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You get one .tech domain free for 1 year, I registered &lt;a href="http://alexmenor.tech"&gt;alexmenor.tech&lt;/a&gt; and had no issues yet.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Get into CI/CD with Travis
&lt;/h2&gt;

&lt;p&gt;Last but not least, learning about code tests and continuous integration/deployment have been a game changer for me. Developing with testing in mind is very beneficial (that's going to be another post) and &lt;em&gt;Travis&lt;/em&gt; allows you to test all your code after any push to your repo (between other many things).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normally, you can setup Travis for free in any public repository. With this offer you can also use it within private ones while you are a student.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you know, you learn a lot by getting your hands dirty and I recommend you to take advantage of all this resources to do it with professional tools.&lt;/p&gt;

&lt;p&gt;To get any of this, you just have to confirm your school email in &lt;a href="https://education.github.com/pack"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>github</category>
      <category>serverless</category>
      <category>cloud</category>
      <category>student</category>
    </item>
  </channel>
</rss>
