<?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: Moustafa25MM</title>
    <description>The latest articles on DEV Community by Moustafa25MM (@moustafa25mm).</description>
    <link>https://dev.to/moustafa25mm</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%2F1053660%2F58cf9504-b8f3-4863-af63-9255cbf0390f.png</url>
      <title>DEV Community: Moustafa25MM</title>
      <link>https://dev.to/moustafa25mm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moustafa25mm"/>
    <language>en</language>
    <item>
      <title>What is the difference between Interfaces vs Types in TypeScript?</title>
      <dc:creator>Moustafa25MM</dc:creator>
      <pubDate>Tue, 28 Mar 2023 00:43:11 +0000</pubDate>
      <link>https://dev.to/moustafa25mm/what-is-the-difference-between-interfaces-vs-types-in-typescript-31c6</link>
      <guid>https://dev.to/moustafa25mm/what-is-the-difference-between-interfaces-vs-types-in-typescript-31c6</guid>
      <description>&lt;p&gt;The difference between types and interfaces in TypeScript used to be more clear, but with the latest versions of TypeScript, they’re becoming more similar.&lt;/p&gt;

&lt;p&gt;Interfaces are basically a way to describe data shapes, for example, an object.&lt;/p&gt;

&lt;p&gt;Type is a definition of a type of data, for example, a union, primitive, intersection, tuple, or any other type.&lt;/p&gt;

&lt;p&gt;some Differences in some topics:&lt;/p&gt;

&lt;p&gt;1-Functions:&lt;br&gt;
Both can be used to describe the shape of an object or a function signature. But the syntax differs.&lt;/p&gt;

&lt;p&gt;Interface &lt;br&gt;
interface Point {&lt;br&gt;
  x: number;&lt;br&gt;
  y: number;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;interface SetPoint {&lt;br&gt;
  (x: number, y: number): void;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Type&lt;br&gt;
type Point = {&lt;br&gt;
  x: number;&lt;br&gt;
  y: number;&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;type SetPoint = (x: number, y: number) =&amp;gt; void;&lt;/p&gt;

&lt;p&gt;2-Declaration merging&lt;/p&gt;

&lt;h2&gt;
  
  
  Unlike a type alias, an interface can be defined multiple times, and will be treated as a single interface (with members of all declarations being merged).
&lt;/h2&gt;

&lt;p&gt;3-Extends and Implements:&lt;br&gt;
In TypeScript, we can easily extend and implement interfaces. This is not possible with types though.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interfaces in TypeScript can extend classes, this is a very awesome concept that helps a lot in a more object-oriented way of programming. We can also create classes implementing interfaces.
&lt;/h2&gt;

&lt;p&gt;4-Intersection (&amp;amp;):&lt;/p&gt;

&lt;p&gt;Intersection allows us to combine multiple types into a single one type. To create an intersection type, we have to use the (&amp;amp;) keyword,&lt;br&gt;
type Name = {&lt;br&gt;
  name: “string”&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;type Age = {&lt;br&gt;
  age: number&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;type Person = Name &amp;amp; Age;&lt;/p&gt;

&lt;p&gt;The nice thing here is that we can create a new intersection type combining two interfaces, for example, but not the other way around. We cannot create an interface combining two types, because it doesn’t work,&lt;/p&gt;

&lt;p&gt;interface Name {&lt;br&gt;
  name: “string”&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;interface Age {&lt;br&gt;
  age: number&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;type Person = Name &amp;amp; Age;&lt;/p&gt;

&lt;p&gt;5-Union (|):&lt;br&gt;
Union types allow us to create a new type that can have a value of one or a few more types. To create a union type, we have to use the | keyword,&lt;/p&gt;

&lt;p&gt;type Man = {&lt;br&gt;
  name: “string”&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;type Woman = {&lt;br&gt;
  name: “string”&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;type Person = Man | Woman;&lt;/p&gt;

&lt;p&gt;Similar to intersections, we can create a new union type combining two interfaces, for example, but not the other way around:&lt;/p&gt;

&lt;p&gt;interface Man {&lt;br&gt;
  name: "string"&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;interface Woman {&lt;br&gt;
  name: "string"&lt;br&gt;
};&lt;/p&gt;

&lt;h2&gt;
  
  
  type Person = Man | Woman;
&lt;/h2&gt;

&lt;p&gt;**Summary of Type Aliases vs Interfaces&lt;br&gt;
Your mileage may differ, but wherever possible, I stick to type aliases for their flexibility and simpler syntax. That is, I pick type aliases except I specifically need features from an interface.&lt;/p&gt;

&lt;p&gt;For the most part, you can also decide based on your personal preference, but stay consistent with your choice — at least in a single given project.&lt;/p&gt;

&lt;p&gt;For completeness, I must add that in performance critical types, interface comparison checks can be faster than type aliases. I’m yet to find this to be an issue.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
