<?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: Thiago Temple</title>
    <description>The latest articles on DEV Community by Thiago Temple (@thitemple).</description>
    <link>https://dev.to/thitemple</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%2F63919%2Fc3445c81-50ff-44f2-a0b0-104af3834aa6.jpg</url>
      <title>DEV Community: Thiago Temple</title>
      <link>https://dev.to/thitemple</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thitemple"/>
    <language>en</language>
    <item>
      <title>TypeScript and the Power of the Unknown</title>
      <dc:creator>Thiago Temple</dc:creator>
      <pubDate>Mon, 15 Jul 2019 16:37:16 +0000</pubDate>
      <link>https://dev.to/thitemple/typescript-and-the-power-of-the-unknown-2ed1</link>
      <guid>https://dev.to/thitemple/typescript-and-the-power-of-the-unknown-2ed1</guid>
      <description>&lt;p&gt;TypeScript is a language that is moving forward really fast and sometimes is hard to keep up with it, there are so many features being released constantly, that we may be missing some really important gems. I think that the &lt;code&gt;unknown&lt;/code&gt; type is one of those.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is it?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;unknown&lt;/code&gt; type is a basic TypeScript type that was &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type"&gt;introduced in version 3.0 of the language&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And by basic, I mean that you can use it to define a simple variable or parameter, as you can see in the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;movie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;movie&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;h2&gt;
  
  
  Why is it there?
&lt;/h2&gt;

&lt;p&gt;The quick explanation is: to represent all types that &lt;em&gt;at the moment of the declaration&lt;/em&gt;, the developer is not sure (or does not know) how that value will be used, or more specifically, how its members (if any) will be accessed.&lt;/p&gt;

&lt;p&gt;It is a complement to &lt;code&gt;any&lt;/code&gt; but it acts in a more type-safe way. Meaning that until the moment that you are really using the value of type &lt;code&gt;unknown&lt;/code&gt; it won't type check it, but as soon as your code starts to make assumptions about it, the compiler will begin to type-check its use.&lt;/p&gt;

&lt;p&gt;How so? Let's see an example to make it more clear.&lt;/p&gt;

&lt;p&gt;Let's take that &lt;code&gt;printMovie&lt;/code&gt; function from above. That function works fine because, &lt;code&gt;console.log&lt;/code&gt; expects a value of type &lt;code&gt;any&lt;/code&gt; for its first argument, and &lt;code&gt;unknown&lt;/code&gt; can be assigned to another &lt;code&gt;unknown&lt;/code&gt; value or &lt;code&gt;any&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's explore a little more on the example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;formatMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movie&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="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`The movie title is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;movie&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;formatMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movie&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;The above snippet will &lt;em&gt;not&lt;/em&gt; compile 💣 because we are now making an assumption about the &lt;code&gt;movie&lt;/code&gt; argument, we're trying to pass it down to another function &lt;code&gt;formatMovie&lt;/code&gt; that is expecting an argument of type &lt;code&gt;string&lt;/code&gt;. When we try to call that function and pass an &lt;code&gt;unknown&lt;/code&gt; value where a &lt;code&gt;string&lt;/code&gt; was expected, then the compiler will yield an error stating exactly that.&lt;/p&gt;

&lt;p&gt;Now you have a choice, either type the first &lt;code&gt;movie&lt;/code&gt; argument as a &lt;code&gt;string&lt;/code&gt; or something else that you can turn in to a &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;printMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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="s2"&gt;`The movie title is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and its rating is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rating&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is another example that will fail 💣 at compile-time because we are assuming that &lt;code&gt;movie&lt;/code&gt; is an object with the properties &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;rating&lt;/code&gt;. Again, at this point, we are able to type it accordingly, at least with an inline type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;printMovie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;title&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;rating&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  And what about any?
&lt;/h2&gt;

&lt;p&gt;Well, I am a strong advocate against the abuse of the &lt;code&gt;any&lt;/code&gt; type in TS 🙅. The main idea of &lt;code&gt;any&lt;/code&gt; is to represent types that can actually be anything. We &lt;em&gt;cannot&lt;/em&gt; know what those types would be at compile-time, my experience is that people reach for &lt;code&gt;any&lt;/code&gt; too soon instead of taking the time and write the appropriate types.&lt;/p&gt;

&lt;p&gt;If it's possible to write the types, even if those types would be of different structures or values, then we should write them. There are a few ways to do so, using &lt;a href="https://www.typescriptlang.org/docs/handbook/generics.html"&gt;generics&lt;/a&gt; or &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types"&gt;union types&lt;/a&gt; for instance, but the point is that they can be typed. And I don't want to over-extend myself in this article explaining how to type them.&lt;/p&gt;

&lt;p&gt;The point is, &lt;code&gt;any&lt;/code&gt; is for when you &lt;em&gt;know&lt;/em&gt; that the type in question can't be specified and &lt;em&gt;unknown&lt;/em&gt; is for when you don't know yet what it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  When should you use it?
&lt;/h2&gt;

&lt;p&gt;I'd say there are a few use cases for the &lt;code&gt;unknown&lt;/code&gt; type. I certainly reach for it in these scenarios:&lt;/p&gt;

&lt;p&gt;1) When migrating from a JavaScript file&lt;/p&gt;

&lt;p&gt;When migrating from a JavaScript codebase, one will probably (maybe) do that on a file per-file basis. Let's say you open a given file, rename it to &lt;code&gt;.ts&lt;/code&gt; and start seeing some errors because of missing types. One thing you could do is something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;Movie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can create type aliases for the types in that file and then apply the given type whenever it's needed such as in functions &lt;code&gt;function printMovie(movie: Movie)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After you have that completed you can come back to the &lt;code&gt;Movie&lt;/code&gt; type and complete it with the needed properties. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The strategy applied here will be the same for the other scenarios as well.&lt;/em&gt; 🔁&lt;/p&gt;

&lt;p&gt;2) When using a third party lib that does not have type definitions.&lt;/p&gt;

&lt;p&gt;You can start creating a definition file (.d.ts) and begin filling it with &lt;code&gt;unknown&lt;/code&gt; types for the things you are unsure of and not using at the moment, and type the things that you are using, and maybe make a pull request for that later on. 😀&lt;/p&gt;

&lt;p&gt;3) When consuming an API that is not implemented.&lt;/p&gt;

&lt;p&gt;If you are consuming an API to get some data and use that data, chances are you know the types already so make it safe 🔒and type it already. If you're not sure of its use or you don't know how data should be sent, then that's a good use of &lt;code&gt;unknown&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  And that's it, let's put what not know right now to use
&lt;/h4&gt;

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