{"id":1351,"date":"2021-11-04T10:30:30","date_gmt":"2021-11-04T09:30:30","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1351"},"modified":"2021-11-04T10:32:00","modified_gmt":"2021-11-04T09:32:00","slug":"classes-vs-interfaces-in-typescript","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/11\/04\/classes-vs-interfaces-in-typescript\/","title":{"rendered":"Classes vs Interfaces in TypeScript"},"content":{"rendered":"\n<p>Classes and interfaces are powerful structures that facilitate not just object-oriented programming but also type-checking in TypeScript. <strong><em>A class<\/em><\/strong> is a blueprint from which we can create objects that share the same configuration &#8211; properties and methods. <strong><em>An interface<\/em><\/strong> is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them.<\/p>\n\n\n\n<p>Since both of these structures define what an object looks like, both can be used in TypeScript to type our variables. The decision to use a class or an interface truly depends on our use case: <strong>type-checking only ?  implementation details (typically via creating a new instance)? or even both!<\/strong> <\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>We can use classes for type-checking&nbsp;and&nbsp;the underlying implementation &#8211; whereas we cannot with an interface.<\/em> <\/span>Understanding what we can get from each structure will easily let us make the best decision that will enhance our code and improve our developer experience.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Using TypeScript class<\/h4>\n\n\n\n<p>ES6 introduced&nbsp;<code>class<\/code>&nbsp;officially to the JavaScript ecosystem. TypeScript boosts JavaScript classes with extra power such as type-checking and&nbsp;<code>static<\/code>&nbsp;properties. This also means that whenever we transpile our code to whatever target JavaScript of our choice, the transpiler will keep all of our&nbsp;<code>class<\/code>&nbsp;code present in the transpiled file.<\/p>\n\n\n\n<p>Let\u2019s look at an example of defining a class named&nbsp;<code>PizzaMaker<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"652\" height=\"199\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-1.png\" alt=\"\" class=\"wp-image-1352\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-1.png 652w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-1-300x92.png 300w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/figure>\n\n\n\n<p><code>PizzaMaker<\/code>&nbsp;is a simple class. It has a&nbsp;<code>static<\/code>&nbsp;method called&nbsp;<code>create<\/code>. What makes this method special is that we can use it without creating an instance of the class. We just invoke the method on the class directly&nbsp;:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"625\" height=\"227\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-2.png\" alt=\"\" class=\"wp-image-1353\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-2.png 625w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-2-300x109.png 300w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/figure>\n\n\n\n<p>Then,&nbsp;<code>PizzaMaker.create()<\/code>&nbsp;returns a new object &#8211; not a class &#8211; with a<em><span class=\"has-inline-color has-pale-pink-color\">&nbsp;<\/span><\/em><strong><em><span class=\"has-inline-color has-pale-pink-color\"><code>name<\/code><\/span><\/em>&nbsp;<\/strong>and<em><span class=\"has-inline-color has-pale-pink-color\"><strong>&nbsp;<code>toppings<\/code><\/strong><\/span><\/em>&nbsp;properties defined from the object passed to it as argument.<\/p>\n\n\n\n<p>If&nbsp;<code>PizzaMaker<\/code>&nbsp;did not define&nbsp;<code>create<\/code>&nbsp;as a&nbsp;<code>static<\/code>&nbsp;method, then to use the method we would need to create an instance of&nbsp;<code>PizzaMaker<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"682\" height=\"460\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-3.png\" alt=\"\" class=\"wp-image-1354\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-3.png 682w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-3-300x202.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/figure>\n\n\n\n<p>We get the same output we had with&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>create<\/strong><\/span><\/code>&nbsp;as a<span class=\"has-inline-color has-pale-pink-color\"><strong>&nbsp;<code>static<\/code><\/strong><\/span>&nbsp;method. Being able to use TypeScript classes with and without an existing instance of a class makes them extremely versatile and flexible. Adding&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>static<\/strong><\/span><\/code>&nbsp;properties and methods to a class makes them act like a&nbsp;<em><strong>singleton<\/strong><\/em>&nbsp;while defining non-static properties and methods make them act like a&nbsp;<em><strong>factory<\/strong><\/em>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>Now, unique to TypeScript is the ability to use classes for type-checking. Let\u2019s declare a class that defines what a&nbsp;<\/em><\/span><em><code><span class=\"has-inline-color has-vivid-red-color\">Pizza<\/span><\/code><\/em><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>&nbsp;looks like:<\/em><\/span><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"622\" height=\"134\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-4.png\" alt=\"\" class=\"wp-image-1355\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-4.png 622w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-4-300x65.png 300w\" sizes=\"(max-width: 622px) 100vw, 622px\" \/><\/figure>\n\n\n\n<p>In the&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>Pizza<\/strong><\/span><\/code>&nbsp;class definition, we are using a handy TypeScript shorthand to define class properties from the arguments of the constructor &#8211; it saves a lot of typing!&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>Pizza<\/strong><\/span><\/code>&nbsp;can create objects that have a&nbsp;<span class=\"has-inline-color has-pale-pink-color\"><strong><code>name<\/code>&nbsp;<\/strong><\/span>and a&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>toppings<\/strong><\/span><\/code>&nbsp;property:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"635\" height=\"159\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-5.png\" alt=\"\" class=\"wp-image-1356\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-5.png 635w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-5-300x75.png 300w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<p><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Therefore, we can use the&nbsp;<\/span><code><span class=\"has-inline-color has-vivid-red-color\">Pizza<\/span><\/code><span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;class to type-check the&nbsp;<\/span><code><span class=\"has-inline-color has-vivid-red-color\">event<\/span><\/code><span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;argument of&nbsp;<\/span><code><span class=\"has-inline-color has-vivid-red-color\">PizzaMaker.create(...)<\/span><\/code><span class=\"has-inline-color has-vivid-cyan-blue-color\">:<\/span><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"660\" height=\"301\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-6.png\" alt=\"\" class=\"wp-image-1357\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-6.png 660w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-6-300x137.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p>We\u2019ve made&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>PizzaMaker<\/strong><\/span><\/code>&nbsp;much more declarative, and hence, much more readable. Not only that, but if we need to enforce the same object structure defined in&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\">Pizza<\/span><\/code>&nbsp;in other places, we now have a portable construct to do so! Just append<span class=\"has-inline-color has-vivid-red-color\">&nbsp;<code><strong>export<\/strong><\/code><\/span>&nbsp;to the definition of&nbsp;<code>Pizza<\/code>&nbsp;and you get access to it from anywhere in your application.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export class Pizza {\n  constructor(public name: string, public toppings: string&#91;]) {}\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Using&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>Pizza<\/strong><\/span><\/code>&nbsp;as a class is great if we want to define and create a&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>Pizza<\/strong><\/span><\/code>, but what if we only want to define the structure of a<span class=\"has-inline-color has-pale-pink-color\"><strong>&nbsp;<code>Pizza<\/code><\/strong><\/span>&nbsp;but we\u2019d never need to instantiate it? That\u2019s when&nbsp;<span class=\"has-inline-color has-vivid-red-color\"><strong><code>interface<\/code>&nbsp;<\/strong><\/span>comes handy!<\/p><\/blockquote>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Using TypeScript interface<\/h4>\n\n\n\n<p>Unlike classes, an&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\"><strong>interface<\/strong><\/span><\/code>&nbsp;is a virtual structure that only exists within the context of TypeScript. The TypeScript compiler uses interfaces solely for type-checking purposes. Once your code is transpiled to its target language, it will be stripped from its interfaces &#8211; JavaScript isn\u2019t typed, there\u2019s no use for them there. Also we will use <strong>interface<\/strong> only on development cycle and for testing purpose .<\/p>\n\n\n\n<p>While a class may define a&nbsp;<code>factory<\/code>&nbsp;or a&nbsp;<code>singleton<\/code>&nbsp;by providing initialisation to its properties and implementation to its methods, an<strong>&nbsp;<code>interface<\/code>&nbsp;<\/strong>is simply a structural contract that defines what the properties of an object should have as a name and as a type. How you implement or initialise the properties declared within the&nbsp;<code>interface<\/code>&nbsp;is not relevant to it. Let\u2019s see an example by transforming our&nbsp;<span class=\"has-inline-color has-pale-pink-color\"><code>Pizza<\/code>&nbsp;<\/span>class into a&nbsp;<code><span class=\"has-inline-color has-pale-pink-color\">Pizza<\/span><\/code>&nbsp;interface:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"624\" height=\"322\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-7.png\" alt=\"\" class=\"wp-image-1359\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-7.png 624w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/11\/image-7-300x155.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p>&nbsp;our current code provides type-checking for&nbsp;<code>Pizza<\/code>&nbsp;but can\u2019t create a pizza.  For doing that, we have to first initialise an object of type interface.  Then we can use this object with the <span class=\"has-inline-color has-pale-pink-color\">static <\/span>function<span class=\"has-inline-color has-pale-pink-color\"><em> create()<\/em><\/span> of  the class PizzaMaker. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Classes and interfaces are powerful structures that facilitate not just object-oriented programming but also type-checking in TypeScript. A class is a blueprint from which we can create objects that share the same configuration &#8211; properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1360,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,83],"tags":[371,374,376,372,375,373],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1351"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1351"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1351\/revisions"}],"predecessor-version":[{"id":1362,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1351\/revisions\/1362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1360"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}