<?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: s</title>
    <description>The latest articles on DEV Community by s (@s44dy).</description>
    <link>https://dev.to/s44dy</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%2F590571%2Fc7aa170c-4e24-484d-a6e2-4d287bd0a355.JPG</url>
      <title>DEV Community: s</title>
      <link>https://dev.to/s44dy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/s44dy"/>
    <language>en</language>
    <item>
      <title>type vs interface in TypeScript</title>
      <dc:creator>s</dc:creator>
      <pubDate>Fri, 09 Apr 2021 19:10:17 +0000</pubDate>
      <link>https://dev.to/s44dy/type-vs-interface-in-typescript-35i6</link>
      <guid>https://dev.to/s44dy/type-vs-interface-in-typescript-35i6</guid>
      <description>&lt;p&gt;When I first started working with TypeScript, I quickly found myself questioning the use of &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;interface&lt;/code&gt; in regards to providing type safety to my code. What is the difference between the two? When should I use one over the other? I decided it was time to get some answers.&lt;/p&gt;

&lt;p&gt;After a bit of research, I realized that the two are more similar today than they have ever been. However, there are still some differences that are worth mentioning. Let’s talk about it!&lt;/p&gt;

&lt;h3&gt;
  
  
  types and interfaces at a high level
&lt;/h3&gt;

&lt;p&gt;Before we take a look at the similarities and differences between a &lt;code&gt;type&lt;/code&gt; and an &lt;code&gt;interface&lt;/code&gt;, let’s first get a good mental model of what the two were intended to achieve in the first place.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;type&lt;/code&gt; keyword in TypeScript is a way for you to provide type aliases to your variables, objects, and functions. These aliases essentially &lt;em&gt;describe&lt;/em&gt; what your data is going to look like. You can describe what your data is going to look like by using the core types (e.g. &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt; etc.) or by creating your own custom types. Let’s take a look at a few examples.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ehj9d5iv6ln8cj3m5er.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ehj9d5iv6ln8cj3m5er.png" alt="1_MKAKdf6Gco0bcRhNr3pgjg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;interface&lt;/code&gt; keyword in TypeScript on the other hand is a way for you to describe &lt;strong&gt;only&lt;/strong&gt; what your objects are going to look like. Let’s see an example of &lt;code&gt;interface&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67zhfletiltbgvs5op0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67zhfletiltbgvs5op0g.png" alt="1_hSlmeCXEy1x37EZ9E9smLQ"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right off the bat, you might think that the &lt;code&gt;type&lt;/code&gt; keyword is the more flexible choice of the two when it comes to type safety. Hold that thought. Let’s take a look now at how the two are similar.&lt;/p&gt;

&lt;h3&gt;
  
  
  Similarities
&lt;/h3&gt;

&lt;p&gt;Let’s say I wanted to provide some type safety for an object, one of the most common use cases for using a &lt;code&gt;type&lt;/code&gt; or an &lt;code&gt;interface&lt;/code&gt;. Turns out they both work pretty much the same, and the syntax is almost identical.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F44iatgimtabmszm93vhn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F44iatgimtabmszm93vhn.png" alt="1_TPjtC18Ro3IhSSro3ZXoYw"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What if I wanted to combine object properties from one &lt;code&gt;type&lt;/code&gt; or &lt;code&gt;interface&lt;/code&gt; into another? We can do that too for both.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F10m7jy8j3dapyxso11on.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F10m7jy8j3dapyxso11on.png" alt="1_ARdjVoUQbBugTD8MlvenrA"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjwguegx24ggi9dlavfmk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjwguegx24ggi9dlavfmk.png" alt="1_yNQhRWm2m2F8esP1HPw1Yg"&gt;&lt;/a&gt; &lt;em&gt;I personally prefer the extends syntax as it feels a bit more natural to me.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Want to provide some type safety to your functions? This is also possible with both a &lt;code&gt;type&lt;/code&gt; and an &lt;code&gt;interface&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gx5733qewnx6pmfwvob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gx5733qewnx6pmfwvob.png" alt="1_B1WDG4pKhygWDnPN8vCCEA"&gt;&lt;/a&gt; &lt;em&gt;I personally prefer using a type here since it reads a bit cleaner, but totally up to you!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A quick note about the above. Earlier I mentioned that an interface can &lt;strong&gt;only&lt;/strong&gt; describe what an object is going to look like, yet here I am describing what a function is going to look like with an &lt;code&gt;interface&lt;/code&gt;. The reason this is possible is because a function technically &lt;em&gt;is&lt;/em&gt; an object under the hood.&lt;/p&gt;

&lt;p&gt;Finally, if you wanted to create a class that adheres to a certain type safety contract provided by a &lt;code&gt;type&lt;/code&gt; or an &lt;code&gt;interface&lt;/code&gt;, you would be able to do so just fine with both.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zf0gacwfbvrqkz811yv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zf0gacwfbvrqkz811yv.png" alt="1_0FTHZgh-R80TUJ355AwURA"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That covers most of the similarities of &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;interface&lt;/code&gt;, let’s now take a look at the differences.&lt;/p&gt;

&lt;h3&gt;
  
  
  Differences
&lt;/h3&gt;

&lt;p&gt;Starting with &lt;code&gt;type&lt;/code&gt;, there are a few things that are possible that you wouldn’t be able to do with an &lt;code&gt;interface&lt;/code&gt;. For example, If you wanted to setup a type alias for just a single core type (e.g. string, number, boolean etc.), this is something that is possible with &lt;code&gt;type&lt;/code&gt; but not with &lt;code&gt;interface&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fop7uwnpwny4wsw85yfh0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fop7uwnpwny4wsw85yfh0.png" alt="1_9msX1PR0rC_REWxd3eouAw"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another possibility with &lt;code&gt;type&lt;/code&gt; is that you are able to create a bit more custom type aliases by using unions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbalanxh1crdkuimpg39j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbalanxh1crdkuimpg39j.png" alt="1_QlcaARr_2JgnLPry7FEsoQ"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This pretty much sums up the unique offerings of &lt;code&gt;type&lt;/code&gt;. While there may be some more advance features that &lt;code&gt;type&lt;/code&gt; offers that I missed, this should cover the most common use cases.&lt;/p&gt;

&lt;p&gt;Moving on to &lt;code&gt;interface&lt;/code&gt;, there is a unique feature called “declaration merging” that you aren’t able to do with a &lt;code&gt;type&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft0zhxxxvuqq52hdjuap1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft0zhxxxvuqq52hdjuap1.png" alt="1_pbV0jRO22ETpb0-_obqmBw"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping up
&lt;/h3&gt;

&lt;p&gt;To summarize, both &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;interface&lt;/code&gt; provide some nice type safety options for you to choose from depending on your needs. When it comes to describing object shapes which is the majority of my use cases, I prefer to go with an &lt;code&gt;interface&lt;/code&gt;. I personally like that an &lt;code&gt;interface&lt;/code&gt; was designed specifically with this purpose in mind, and is very intentional about it.&lt;/p&gt;

&lt;p&gt;However anytime I need to provide type safety for a function, or need a bit more customization for a type alias such as the case with unions, I prefer to go with &lt;code&gt;type&lt;/code&gt;. Both are lovely options for providing type safety, and you really can’t go wrong with using either one!&lt;/p&gt;

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