<?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>Forem: Ana Liza Pandac</title>
    <description>The latest articles on Forem by Ana Liza Pandac (@analizapandac).</description>
    <link>https://forem.com/analizapandac</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%2F98492%2Fd09619a0-a17e-44d4-b754-dbe7df36b103.jpeg</url>
      <title>Forem: Ana Liza Pandac</title>
      <link>https://forem.com/analizapandac</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/analizapandac"/>
    <language>en</language>
    <item>
      <title>Learning TypeScript with React - Part 2 (The what, why and how of interfaces)</title>
      <dc:creator>Ana Liza Pandac</dc:creator>
      <pubDate>Sat, 06 Jul 2019 13:19:50 +0000</pubDate>
      <link>https://forem.com/analizapandac/learning-typescript-with-react-part-2-the-what-why-and-how-of-interfaces-1033</link>
      <guid>https://forem.com/analizapandac/learning-typescript-with-react-part-2-the-what-why-and-how-of-interfaces-1033</guid>
      <description>&lt;p&gt;⚡TL;DR: &lt;em&gt;Understanding what are interfaces, why use them and how to use them can help you in writing more reusable code and creating awesome applications.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/analizapandac/learning-typescript-with-react-part-1-3ohn"&gt;previous article&lt;/a&gt;, we talked about types, what is it, how to use them and why use them. If you haven't read it yet, I recommend that you read it first.&lt;/p&gt;

&lt;p&gt;In this post, we'll get to know what is an interface, why should you care about it, and how to use it to write more reusable code.&lt;/p&gt;

&lt;p&gt;Let's begin.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is an interface?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Interfaces are the core way in TypeScript to compose multiple type annotations into a single named annotation.&lt;/p&gt;

&lt;p&gt;It’s a way of creating a new type that describes an object’s property names and their types. Remember the types that we covered before (number, boolean, string, etc.)? &lt;strong&gt;An interface is the same as those types.&lt;/strong&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why should you care about interfaces?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;For me, interfaces are one of the best features of the TypeScript language. Let me show you just how awesome it is.&lt;/p&gt;

&lt;p&gt;Let's start with the following example.&lt;/p&gt;

&lt;p&gt;You have a function called &lt;code&gt;renderVideo&lt;/code&gt; which accepts &lt;code&gt;video&lt;/code&gt; as a parameter in which you annotated with all the properties that should be present in a video object.&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;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;videoId&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;title&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;description&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;thumbnailURL&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;renderVideo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;videoId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;thumbnailURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="k"&gt;void&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;Even though the parameter annotation is quite long, the code above is okay, the function parameter and return value was annotated correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However&lt;/strong&gt;, imagine if you added more functions which accepts a video.&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;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;videoId&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;title&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;description&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;thumbnailURL&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;renderVideo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;videoId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;thumbnailURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="k"&gt;void&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getVideoSummary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;videoId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;thumbnailURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="k"&gt;void&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;span class="c1"&gt;// another function has a `video` parameter&lt;/span&gt;
&lt;span class="c1"&gt;// another one&lt;/span&gt;
&lt;span class="c1"&gt;// and another one&lt;/span&gt;
&lt;span class="c1"&gt;// *shouts* "DJ Khaled"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Aaahhhh. I don't know about you but to me, the code above doesn't look good.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are a lot of long annotations which are duplicated several times. The code is harder to read as well.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now imagine again (we're imagining a lot here) that you need to do either one or more of the following changes to a video:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add the date it was published as a new property&lt;/li&gt;
&lt;li&gt;rename &lt;code&gt;thumbnailURL&lt;/code&gt; to &lt;code&gt;imageURL&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;change the type of &lt;code&gt;videoId&lt;/code&gt; from &lt;code&gt;string&lt;/code&gt; to a number&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Applying these new changes to the code above means you need to change all the video annotations in a lot of places.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's a lot of work and we don't want that so how can we solve this? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple, by using an interface.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;How to create an interface?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We can convert this long video annotation into an interface called &lt;code&gt;Video&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;To create an interface, use the &lt;strong&gt;&lt;code&gt;interface&lt;/code&gt;&lt;/strong&gt; keyword followed by a name that represents the object, in this case &lt;strong&gt;&lt;code&gt;Video&lt;/code&gt;&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;name should start with a capital letter&lt;/em&gt; just like how we create React components and a set of curly braces where you declare all of the object's properties and methods and its types.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is how you create the &lt;code&gt;Video&lt;/code&gt; interface.&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Video&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;videoId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;thumbnailURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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;h3&gt;
  
  
  &lt;strong&gt;How do you use an interface?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Since an interface is a custom type, we can annotate it to a variable just like a normal type.&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;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;Video&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;By using the &lt;code&gt;Video&lt;/code&gt; interface, we can rewrite the previous example to this:&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Video&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;videoId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;thumbnailURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&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;renderVideo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Video&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&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;getVideoSummary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Video&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&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;// another function that uses a video&lt;/span&gt;
&lt;span class="c1"&gt;// another one&lt;/span&gt;
&lt;span class="c1"&gt;// and another one&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;videoId&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;title&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;description&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;thumbnailURL&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="nf"&gt;renderVideo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;video&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 looks wayyyyy better than the previous one right?&lt;/p&gt;

&lt;p&gt;It looks cleaner and in one glance, we can see right away that the parameter is a type of &lt;code&gt;Video&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Most of all, we can add, remove or rename a property or change a property type only in one place, the interface declaration. Awesome!&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 If you use VS code, which I highly recommend you should, you can see all the property names and types of an interface by holding the &lt;code&gt;ctrl/cmd&lt;/code&gt; key and hovering over the interface name.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frm64kjn21s83qhptjvdm.gif" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frm64kjn21s83qhptjvdm.gif"&gt;&lt;/a&gt;&lt;br&gt;
Pretty cool right?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;How are interfaces used in the app?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Aside from using the &lt;code&gt;Video&lt;/code&gt; interface, I created another interface to represent how the Youtube API response is structured.&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;YoutubeVideo&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;videoId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nl"&gt;snippet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;publishedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;thumbnails&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;medium&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nl"&gt;channelTitle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;channelId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;YoutubeAPIResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;YoutubeVideo&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;This interface is used in the &lt;code&gt;fetchChannelVideos&lt;/code&gt; function.&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchChannelVideos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FetchVideosArguments&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;Video&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;shouldUseDefaultVideos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;searchQuery&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;requestURL&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;const&lt;/span&gt; &lt;span class="na"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;YoutubeAPIResponse&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;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requestURL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Since an interface is a type, it means the TS compiler also does error-checking on the annotated data.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By creating an interface for the API response structure and annotating it to the variable, we made sure that we don't accidentally access a property that does not exist.&lt;/p&gt;

&lt;p&gt;So if we try to access &lt;code&gt;comments&lt;/code&gt;, which is not included in the API response, the TypeScript compiler will give us an error.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Writing Reusable Code with Interfaces&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now that we covered the what, why and how of interfaces, let's talk about how we can use it to write more reusable code. &lt;/p&gt;

&lt;p&gt;Let's say you have a component that renders comments from a Youtube user and the channel.&lt;/p&gt;

&lt;p&gt;First we write it like this:&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="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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&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;Channel&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&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;renderUserComment&lt;/span&gt;&lt;span class="p"&gt;:&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="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ReactNode&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;comment&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="cm"&gt;/* return user comment */&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;user&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="s2"&gt;Ana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ana.png&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;channel&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="s2"&gt;Tedx Talks&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tedxtalks.png&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;renderChannelComment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ReactNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;comment&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="cm"&gt;/* return channel comment */&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;renderUserComment&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello there&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;renderChannelComment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi, thanks for watching&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;There are two separate interfaces to represent a user and a channel as well as separate functions to render these comments even though they do the same thing, rendering a comment with the author.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;How can we rewrite this to remove the duplicated code?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Although we can use a &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types" rel="noopener noreferrer"&gt;union type&lt;/a&gt; to indicate that the comment author can be a &lt;code&gt;User&lt;/code&gt; or a &lt;code&gt;Channel&lt;/code&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;renderComment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;author&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="nx"&gt;Channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ReactNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;comment&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="cm"&gt;/* return comment */&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;In this scenario, it's better to create one interface to represent the two types of users.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's rewrite it using a more generic interface called &lt;code&gt;Author&lt;/code&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Author&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="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&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;renderComment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ReactNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;comment&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="cm"&gt;/* return comment */&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;user&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="s2"&gt;Ana&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ana.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;github&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;analizapandac&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;channel&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="s2"&gt;Tedx Talks&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tedxtalks.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;channelId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;UCsT0YIqwnpJCM-mx7-gSA4Q&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;renderComment&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello there&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;renderComment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hi, thanks for watching&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;Even though &lt;code&gt;user&lt;/code&gt; and &lt;code&gt;channel&lt;/code&gt; represents two different types of data, &lt;strong&gt;they satisfy the requirements to be considered an &lt;code&gt;author&lt;/code&gt;&lt;/strong&gt; which is a &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;avatar&lt;/code&gt; so the &lt;code&gt;renderComment&lt;/code&gt; function works for both objects.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The examples above are very basic but the point here is that we can use a generic interface to represent different objects which can work with different functions. This is one way to use interfaces to write more reusable code.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Closing Thoughts&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Aside from learning about the what, why and how of interfaces, I hope that I have also shown you a glimpse of the power of interfaces and how you can use it to create amazing applications. &lt;/p&gt;

&lt;p&gt;There's a lot more to interfaces especially on how to use it together with the other TypeScript features such as classes and write even better code.&lt;/p&gt;

&lt;p&gt;I'll continue learning the more advanced TS concepts, creating new exciting projects using them and sharing them with you. &lt;/p&gt;

&lt;p&gt;I hope you'll do the same 😊&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Project Github Repo&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here's the source code of the app: &lt;a href="https://github.com/analizapandac/tedflix" rel="noopener noreferrer"&gt;https://github.com/analizapandac/tedflix&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can clone it and play with the source code on your local if you want.&lt;/p&gt;

&lt;p&gt;You can see the deployed app at &lt;a href="https://tedflix.netlify.com/" rel="noopener noreferrer"&gt;https://tedflix.netlify.com/&lt;/a&gt;.&lt;/p&gt;



&lt;p&gt;Here's the previous article:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/analizapandac" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F98492%2Fd09619a0-a17e-44d4-b754-dbe7df36b103.jpeg" alt="analizapandac"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/analizapandac/learning-typescript-with-react-part-1-3ohn" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Learning TypeScript with React - Part 1&lt;/h2&gt;
      &lt;h3&gt;Ana Liza Pandac ・ Jul 1 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#typescript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;






&lt;p&gt;Let me know in the comments below if you have any feedback and suggestions or pointers on where to go next. Thank you in advance.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Learning TypeScript with React - Part 1</title>
      <dc:creator>Ana Liza Pandac</dc:creator>
      <pubDate>Mon, 01 Jul 2019 18:30:29 +0000</pubDate>
      <link>https://forem.com/analizapandac/learning-typescript-with-react-part-1-3ohn</link>
      <guid>https://forem.com/analizapandac/learning-typescript-with-react-part-1-3ohn</guid>
      <description>&lt;p&gt;⚡TL;DR: &lt;em&gt;Understanding what are types, type annotations, why use them and how to use them can help you catch errors during development while also enhancing code quality and readability.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Two weeks ago, I decided to pick up TypeScript so I started learning it by creating &lt;a href="https://tedflix.netlify.com/" rel="noopener noreferrer"&gt;TEDFlix&lt;/a&gt;. Built together with React, it fetches the videos from the official &lt;a href="https://www.youtube.com/channel/UCsT0YIqwnpJCM-mx7-gSA4Q%20target=" rel="noopener noreferrer"&gt;Tedx Talks&lt;/a&gt; Youtube channel for you to watch. Why TEDFlix? It sounds fun and honestly, I ran out of ideas 😛&lt;/p&gt;

&lt;p&gt;In this two-part series, I wanted to share what I learned from doing this project. &lt;/p&gt;

&lt;p&gt;Before diving into it, let me give you a short definition of what is TypeScript (TS) taken from the official TS website.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, let me also give you a high level overview of the app’s features and how the React side is structured. &lt;/p&gt;

&lt;p&gt;It is a one page app where &lt;em&gt;it has a video player, a list of related talks and a search bar&lt;/em&gt; which are all built as separate React components. On page load, it fetches a default list of talks from a JSON file instead of fetching from Youtube to avoid exceeding the daily usage quota of the Youtube API when the page is loaded by many users. The app only communicates with Youtube when searching for talks.&lt;/p&gt;

&lt;p&gt;Okay, let's start.&lt;/p&gt;

&lt;h3&gt;
  
  
  First, the project setup
&lt;/h3&gt;

&lt;p&gt;I generated the project using Create React App with TypeScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;create-react-app my-app-name &lt;span class="nt"&gt;--typescript&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;How did I use TypeScript in this app?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Since I’m a TS beginner, I get started by learning the syntax and features of the language first &lt;em&gt;specifically the types, type annotations and interfaces&lt;/em&gt;. In this first post, I'm gonna be talking about types and annotations only. &lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;What is a type?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Since TypeScript is a typed language, it means we can specify/annotate the type of the variables, function parameters and object properties. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From my own understanding, a type is a symbol/representation of all the properties and methods that a variable has access to.&lt;/strong&gt; It means if we added a type of &lt;strong&gt;&lt;code&gt;number&lt;/code&gt;&lt;/strong&gt; to a variable, the TS compiler knows that &lt;em&gt;the variable has and can only access all the properties and methods that a number has&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i49hdck7llsm53xf68q.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i49hdck7llsm53xf68q.gif" width="800" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A type is annotated to the variable, parameter or property using this format &lt;strong&gt;&lt;code&gt;:type&lt;/code&gt;&lt;/strong&gt;. &lt;br&gt;
For example, &lt;code&gt;let name: string&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are a lot of available types in TS however, in this app, these are the ones that I've used:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Primitive Types&lt;/strong&gt;  - number, string, boolean&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Arrays&lt;/strong&gt; -  by adding &lt;code&gt;[]&lt;/code&gt; after the type (e.g. &lt;code&gt;let names: string[]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Video&lt;/strong&gt; - an interface or a custom type I created to represent a video object's properties&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;See &lt;a href="https://basarat.gitbooks.io/typescript/docs/types/type-system.html" rel="noopener noreferrer"&gt;here&lt;/a&gt; for a more comprehensive list of types.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Why do we care about annotating types?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Types are one of the best forms of documentation you can have.&lt;/strong&gt; This is a very helpful to the next developer who has to read your code &lt;em&gt;which could also be the future you&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It helps the TypeScript compiler help you.&lt;/strong&gt; If you annotate a variable as a number and a few lines later assigned it a value which is a string, the compiler will give you the following error.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&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: cannot assign a `string` to a `number`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Even if we remove the &lt;code&gt;:number&lt;/code&gt; annotation from variable &lt;code&gt;x&lt;/code&gt;, the same error will be given by the compiler because &lt;strong&gt;TypeScript is smart&lt;/strong&gt;*. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*If you declare a variable with a value without specifying the type, &lt;strong&gt;TypeScript will figure out the type of the variable based on the initial value assigned to it.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;type inference&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;"So if type inference is present then why do I have to bother annotating the variables?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type inference does not work all the time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An example scenario is when you delay the initialization of a variable as shown below.&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;let&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// a few lines later&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;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// The compiler will NOT give an error even &lt;/span&gt;
&lt;span class="c1"&gt;// if we assign a non-numeric value to `age`&lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ana&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;If you delay the variable initialization without annotating a type to it, the variable will automatically have a type of &lt;strong&gt;&lt;code&gt;any&lt;/code&gt;&lt;/strong&gt; associated with it since the TS compiler does not have any idea what kind of properties &lt;code&gt;age&lt;/code&gt; will have later on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variables with a type of &lt;code&gt;any&lt;/code&gt; should be avoided as much as possible.&lt;/strong&gt; It defeats the purpose of the idea behind the TS type system where we want to catch as many errors as possible during development. Why? Because, the compiler cannot do error checking on types of &lt;code&gt;any&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember, use types to help the TS compiler help you.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;How is type annotation used in the app?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Aside from using types on the variable declarations, I also used annotations in the function declarations.&lt;/p&gt;

&lt;p&gt;A function can be annotated in the following ways:&lt;/p&gt;
&lt;h4&gt;
  
  
  1. Arrow function
&lt;/h4&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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// or&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&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="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  2. Non-arrow function
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nx"&gt;number&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;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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In both of these examples, the compiler will give an error if the function returns a string or any type that is not a number &lt;em&gt;since it is expecting a number&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For fetching the channel's videos, I created a function called &lt;strong&gt;&lt;code&gt;fetchChannelVideos&lt;/code&gt;&lt;/strong&gt; which accepts a boolean flag indicating whether to fetch the default videos from the JSON file and a search query string.  Both of these are optional parameters (by adding &lt;strong&gt;&lt;code&gt;?&lt;/code&gt;&lt;/strong&gt; after the property name) which are represented as an interface. I will explain later what an interface is but for now let’s take a closer look on how the function is annotated.&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FetchVideosArguments&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;shouldUseDefaultVideos&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="nx"&gt;searchQuery&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&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;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchChannelVideos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
 &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FetchVideosArguments&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;Video&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
 &lt;span class="nx"&gt;shouldUseDefaultVideos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nx"&gt;searchQuery&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;On the left side of the assignment operator (&lt;code&gt;=&lt;/code&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchChannelVideos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FetchVideosArguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;Video&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;em&gt;we are annotating the variable &lt;strong&gt;&lt;code&gt;fetchChannelVideos&lt;/code&gt;&lt;/strong&gt; that was declared &lt;strong&gt;not the function assigned&lt;/strong&gt;&lt;/em&gt;. We are telling the TS compiler that this variable will have a function assigned to it with these types of arguments and return value.&lt;/p&gt;

&lt;p&gt;While the right part of the &lt;code&gt;=&lt;/code&gt; is the function assigned.&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="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
 &lt;span class="nx"&gt;shouldUseDefaultVideos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nx"&gt;searchQuery&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To annotate the function itself, we have to specify its arguments and their types and the return value as well. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;So why didn't I annotate the function to assigned to &lt;strong&gt;&lt;code&gt;fetchChannelVideos&lt;/code&gt;&lt;/strong&gt;?&lt;/em&gt; Because again, TypeScript is smart.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Seeing that I assigned the function to a variable that was annotated, it is able to infer that the function will have the same argument names and types and the return value as well, otherwise it will give an error if I add or specify different argument names or return a different value. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbxsv7ywhbr9oekyhn87.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbxsv7ywhbr9oekyhn87.gif" width="800" height="120"&gt;&lt;/a&gt;&lt;em&gt;*The function arguments and return value is inferred&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However&lt;/strong&gt;, if I’m exporting the function directly without assigning it to a variable then I have to annotate it like below.&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchChannelVideos&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
 &lt;span class="nx"&gt;shouldUseDefaultVideos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nx"&gt;searchQuery&lt;/span&gt;
&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;FetchVideosArguments&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;Video&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;// function body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Okay, now that we have an idea about the what, why and how of type annotations, there's one last question.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Where do we use types?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The answer is: &lt;strong&gt;everywhere&lt;/strong&gt;. By using TypeScript, every data in your application will have a type associated with it whether you like it or not.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Closing Thoughts&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I have only touched the basics so far but I’ve already seen how helpful it is, seeing the errors the compiler gives while writing the code instead of discovering them later when running the app saved me a lot of time.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Learning Resources&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you want to learn more and join me on this journey, here are some available resources that can help you get started.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html" rel="noopener noreferrer"&gt;TypeScript in 5 Minutes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://basarat.gitbooks.io/typescript/" rel="noopener noreferrer"&gt;TypeScript Deep Dive&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" rel="noopener noreferrer"&gt;Official TypeScript Documentation&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;I deployed the app in Netlify: &lt;a href="https://tedflix.netlify.com/" rel="noopener noreferrer"&gt;https://tedflix.netlify.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will share the Github repo of the project soon.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;*Update: 06/07*&lt;/strong&gt;&lt;br&gt;
Last part about interfaces is up 🎉&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/analizapandac" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F98492%2Fd09619a0-a17e-44d4-b754-dbe7df36b103.jpeg" alt="analizapandac"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/analizapandac/learning-typescript-with-react-part-2-the-what-why-and-how-of-interfaces-1033" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Learning TypeScript with React - Part 2 (The what, why and how of interfaces)&lt;/h2&gt;
      &lt;h3&gt;Ana Liza Pandac ・ Jul 6 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#typescript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#react&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;






&lt;p&gt;Let me know in the comments below if you have any feedback and suggestions or pointers on where to go next. Thank you in advance.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using CSS grid, flexbox and multi-columns layout to recreate the Daily Prophet</title>
      <dc:creator>Ana Liza Pandac</dc:creator>
      <pubDate>Mon, 17 Sep 2018 08:19:27 +0000</pubDate>
      <link>https://forem.com/analizapandac/using-css-grid-flexbox-and-multi-columns-layout-to-recreate-the-daily-prophet-7hd</link>
      <guid>https://forem.com/analizapandac/using-css-grid-flexbox-and-multi-columns-layout-to-recreate-the-daily-prophet-7hd</guid>
      <description>&lt;p&gt;Recently, I posted an &lt;a href="https://dev.to/analizapandac/creating-a-copy-of-red-onions-daily-prophet-using-css-flexbox-1klo"&gt;article&lt;/a&gt; about how I created a copy of &lt;a href="http://redonion.se/cssgrid/"&gt;Red Onion’s The Daily Prophet&lt;/a&gt; using CSS flexbox. And since I started learning CSS grid, I decided to implement it again by using the two layout modules along with the multi-columns layout.&lt;/p&gt;

&lt;p&gt;However, before we dive right into it, let me make this clarification first cause I think it’s really important.&lt;/p&gt;

&lt;p&gt;The next day after posting my previous article, I stumbled upon a &lt;a href="https://www.smashingmagazine.com/2018/08/flexbox-display-flex-container/"&gt;post&lt;/a&gt; from Smashing Magazine about flexbox and I realized there’s one important aspect of the &lt;strong&gt;flex-direction&lt;/strong&gt; that I completely misunderstood. &lt;strong&gt;I thought the rows will always start from left to right. And guess what, not always.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The rows and columns are displayed depending on the writing mode that you’re working with, not the physical dimensions of the screen. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So if you’re working with English, it uses the default writing mode which is &lt;strong&gt;horizontal-tb&lt;/strong&gt; which means content flows horizontally from left to right, vertically from top to bottom. The next horizontal line is positioned below the previous line.&lt;/p&gt;

&lt;p&gt;However if you’re working with other languages that uses a different writing mode like Hebrew and Arabic which are &lt;strong&gt;written right to left then your flex items would start at the right&lt;/strong&gt;. Or if the writing mode is top-to-bottom, then your row items would be displayed like a column.&lt;/p&gt;

&lt;p&gt;Okay now that’s done, let’s start.&lt;/p&gt;

&lt;p&gt;Since I've mostly talked about flexbox before, let's take a closer look on the usage of the grid layout.&lt;/p&gt;

&lt;h1&gt;
  
  
  Using the grid layout and flexbox together
&lt;/h1&gt;

&lt;p&gt;How do I decide when to use grid or flexbox? Actually, it's simple.&lt;/p&gt;

&lt;p&gt;I make use of CSS grid when dealing with &lt;em&gt;two-dimensional sections&lt;/em&gt; (both row and column, for example the main stories section).&lt;/p&gt;

&lt;p&gt;Flexbox for simple &lt;em&gt;one-dimensional sections&lt;/em&gt; (only row OR column, like the publication name section) and for the alignment of text and elements.&lt;/p&gt;

&lt;h1&gt;
  
  
  Dividing columns
&lt;/h1&gt;

&lt;p&gt;To specify the number (and the widths) of columns in a grid container, you have to use the &lt;strong&gt;grid-template-columns&lt;/strong&gt; property.&lt;/p&gt;

&lt;p&gt;Let’s take a look at the main story section. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QgmkrUIx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/o3cru9rjgi8ydtc4ffp6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QgmkrUIx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/o3cru9rjgi8ydtc4ffp6.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the story’s left content takes three-fourth (3/4) of the section while the right part takes one-fourth (1/4) of it.&lt;/p&gt;

&lt;p&gt;At first I declared &lt;strong&gt;grid-template-columns&lt;/strong&gt; with a value of &lt;strong&gt;1fr 250px&lt;/strong&gt; but when I made the screen smaller, the left part’s content gets squished. The reason is that the right part will always take 250px of the grid parent while the left part will take the remaining available space since I declared it with a &lt;strong&gt;&lt;code&gt;fr&lt;/code&gt;&lt;/strong&gt; value (fraction of the free space).&lt;/p&gt;

&lt;p&gt;So I modified it to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"story--main"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"section--left"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"section--right"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

.story--main {
  display: grid;
  grid-template-columns: 3fr 1fr;
}

.story--main .section--left {}
.story--main .section--right {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3fr 1fr&lt;/strong&gt; means the left part will always take three-fourth of the available space and one-fourth will be given to the right part. This also means that whenever you resize the viewport, the columns will automatically adjust their widths depending on the size of the grid parent.&lt;/p&gt;

&lt;h1&gt;
  
  
  Making the page responsive
&lt;/h1&gt;

&lt;p&gt;The previous flexbox exercise that I made is not responsive but this time, I took care of the responsiveness by using media queries and manipulating the value of grid-template-columns on specific viewport sizes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.parent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="na"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1fr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="na"&gt;gap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;768px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1fr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the example above, on iPad and desktop view, the columns would be displayed in this manner.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jF4I_jME--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/gtad3mjx9jd09fje89nl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jF4I_jME--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/gtad3mjx9jd09fje89nl.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While on mobile, since I specified it as &lt;strong&gt;repeat(2, 1fr)&lt;/strong&gt;, it would be displayed like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xsyMxpnE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/3xvxl69cp2oqshzwg4yn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xsyMxpnE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/3xvxl69cp2oqshzwg4yn.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  repeat()
&lt;/h3&gt;

&lt;p&gt;Instead of writing &lt;strong&gt;grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr;&lt;/strong&gt; which is very repetitive, you can use the &lt;strong&gt;repeat()&lt;/strong&gt; function so &lt;strong&gt;repeat(6, 1fr)&lt;/strong&gt; will repeat the specified width 6 times thus producing the same result.&lt;/p&gt;

&lt;p&gt;I’ve also used &lt;strong&gt;repeat(auto-fill, minmax(min, max))&lt;/strong&gt; in some of the sections for automatically calculating the number of repetitions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Actually, I’m confused about what is the difference between auto-fill and auto-fit. If anyone knows, please let me know.:D&lt;/em&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Using multi-columns layout for sections that only contains text
&lt;/h1&gt;

&lt;p&gt;For some sections that only contains text separated by columns, instead of using grid or flexbox I decided to use another existing layout module called multi-columns layout to produce the same effect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"paragraphs_wrapper"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Veteran Captain Oliver Wood did not seem too anxious about the hurricane threat.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Ducimus qui blanditiis praesentium voluptatum delenit.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

.paragraphs_wrapper {
  column-count: 2;
}

.paragrahps_wrapper {
  p {
    // to ensure that the paragraph will not be broken into two columns
    page-break-inside: avoid;
    break-inside: avoid;
  }
}

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

&lt;/div&gt;



&lt;p&gt;Which produces this result.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tXGmfAVL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hvulkw7o44xdclnv81h6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tXGmfAVL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hvulkw7o44xdclnv81h6.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Using all of these layout modules together helped me understand what kind of scenarios they are most useful of and how to utilize each of its strength to make even better pages. I'm so excited to the possibilities.&lt;/p&gt;

&lt;p&gt;Thanks for reading and you can check the page &lt;a href="https://codepen.io/analizapandac/pen/EeeXpp"&gt;@CodePen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Have any feedback/comments, please let me know.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;*The Daily Prophet copyrights reserved to &lt;a href="http://redonion.se/en/home/"&gt;Red Onion&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>flexbox</category>
      <category>cssgrid</category>
      <category>webdev</category>
    </item>
    <item>
      <title>I created a copy of Red Onion's Daily Prophet using CSS Flexbox</title>
      <dc:creator>Ana Liza Pandac</dc:creator>
      <pubDate>Sat, 15 Sep 2018 10:53:14 +0000</pubDate>
      <link>https://forem.com/analizapandac/creating-a-copy-of-red-onions-daily-prophet-using-css-flexbox-1klo</link>
      <guid>https://forem.com/analizapandac/creating-a-copy-of-red-onions-daily-prophet-using-css-flexbox-1klo</guid>
      <description>&lt;p&gt;If you have ever seen &lt;a href="http://redonion.se/cssgrid/" rel="noopener noreferrer"&gt;Red Onion’s Daily Prophet page&lt;/a&gt; then you’d know that it's a really awesome and creative layout which is built using CSS Grid. It’s similar to an old-style print layout which seems quite challenging to replicate on the web. Creating a clone of it using flexbox instead of css grid would be a really good challenge if you want to learn how to use the flexbox layout. (&lt;em&gt;I guess that’s what my senior also thought when he sent me the link.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;Although flexbox and css grid have been around for awhile now, I haven’t really used them that much since I mostly relied on the existing css frameworks most of the time (I know).&lt;/p&gt;

&lt;p&gt;So I decided to this exercise to find out if it's really complicated as it seems and here's what I've learned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Seeing in boxes
&lt;/h2&gt;

&lt;p&gt;The page layout seems very complicated when you first look at it but what I found is that it’s easier if I start looking at each section as a box. This is what the page looks like in boxes.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F7gb2hsh0c147gqemi5so.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F7gb2hsh0c147gqemi5so.png" title="Daily Prophet's sections as seen in boxes" alt="Daily Prophet's sections as seen in boxes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Inline and blocked elements
&lt;/h2&gt;

&lt;p&gt;Some of the boxes are displayed inline to each other while others are displayed in blocks. By default when a flexed parent box has several children, those children are displayed beside each other in a row.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Figvz7gvuslim29vm9u1e.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Figvz7gvuslim29vm9u1e.png" title="Items displayed inline" alt="Items displayed inline"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to display each child in it’s own block, you can use the &lt;strong&gt;flex-direction&lt;/strong&gt; property as &lt;strong&gt;column&lt;/strong&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1tgngg2z64e2t04egg44.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1tgngg2z64e2t04egg44.png" title="Items displayed in blocks" alt="Items displayed in blocks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping overlapping elements
&lt;/h2&gt;

&lt;p&gt;One of the most important thing to also consider is how the page will look like in smaller devices (&lt;em&gt;or the page responsiveness&lt;/em&gt;). By default when using flexbox,items will not be wrapped into a new line. So as the screen gets smaller, some of the content will not be visible. In order to fit them to the new viewport size, you have to declare the &lt;strong&gt;flex-wrap&lt;/strong&gt; property as &lt;strong&gt;wrap&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.parent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;.child&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;
  
  
  Centering things inside an element
&lt;/h2&gt;

&lt;p&gt;If I need to vertically and horizontally center a text inside the element, in the past, I would’ve use positioning on the parent and child element.&lt;/p&gt;

&lt;p&gt;But with flexbox, you can use &lt;strong&gt;justify-content&lt;/strong&gt; and &lt;strong&gt;align-items&lt;/strong&gt; for this exact behaviour.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"exclusive-story__marker"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;exclusive&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

.exclusive-story__marker {
    display: flex;
    align-items: center;
    justify-content: center;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Additional learnings
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use substitute fonts when the original font is not available
&lt;/h3&gt;

&lt;p&gt;The original page is using &lt;strong&gt;Headline One HPLHS&lt;/strong&gt; for the story headlines but I can’t find the exact font on Google fonts so I decided to use serif instead since it looks appropriate for newspaper headlines as well. It would be nice to have the exact same font but it’s not necessary at the moment. My goal is to learn how to use flexbox not making a pixel perfect replica of a page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid using height on divs to prevent overlapping content on smaller screens
&lt;/h3&gt;

&lt;p&gt;Seeing the page sections in terms of boxes is a very helpful approach but it also made me think in heights. Yes, heights. I started assigning heights to the boxes that seems to have a very specific height like the publication name section and the exclusive story section.&lt;/p&gt;

&lt;p&gt;But when I decreased the size of the viewport, the text and images inside it overlaps the flex container so I decided to remove those height declarations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lastly...
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using object-fit:cover to crop an image and display the center&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the first-letter pseudo-code to make the first letter of the paragraph bigger than the rest of the sentence just like how it behaves in a newspaper.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, I really liked doing this exercise. I gained more understanding on how flexbox works. There’s still a whole lot of flex properties I haven’t used like flex-basis, order, flex-shrink but for now this is okay, I can learn them as I go on.&lt;/p&gt;

&lt;p&gt;Thanks for reading and you can check the cloned page &lt;a href="https://codepen.io/analizapandac/pen/NLMemQ" rel="noopener noreferrer"&gt;@CodePen&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Have feedback/comments, please let me know.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;*Daily Prophet's copyright reserved to &lt;a href="http://redonion.se/en/home/" rel="noopener noreferrer"&gt;Red Onion&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;P.S. This wouldn't be possible without the encouragement of my senior/mentor Dinesh Pandiyan &lt;a href="https://dev.to/flexdinesh"&gt;@flexdinesh&lt;/a&gt;. Thank you po.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>flexbox</category>
      <category>webdev</category>
      <category>design</category>
    </item>
  </channel>
</rss>
