<?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: All Things Typescript</title>
    <description>The latest articles on DEV Community by All Things Typescript (@allthingstypescript).</description>
    <link>https://dev.to/allthingstypescript</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%2Forganization%2Fprofile_image%2F7772%2F53c7770a-2d9a-4587-a26d-60007b514e9e.jpg</url>
      <title>DEV Community: All Things Typescript</title>
      <link>https://dev.to/allthingstypescript</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/allthingstypescript"/>
    <language>en</language>
    <item>
      <title>Why do we have const enums in Typescript?</title>
      <dc:creator>Maina Wycliffe</dc:creator>
      <pubDate>Mon, 17 Jun 2024 13:33:22 +0000</pubDate>
      <link>https://dev.to/allthingstypescript/why-do-we-have-const-enums-in-typescript-4l0</link>
      <guid>https://dev.to/allthingstypescript/why-do-we-have-const-enums-in-typescript-4l0</guid>
      <description>&lt;p&gt;Typescript usually has two schools of thought—enums and unions—with strong supporters on both sides of the argument. In today's issue, I want to avoid that debate and focus solely on Enums, specifically a different, lesser-known version of enums: const enums.&lt;/p&gt;

&lt;p&gt;Before we go any further, let’s briefly talk about what enums are: Enums allow developers to define a list of named constants representing distinct states within their application. For instance, if you wanted to have a switch button, the values can either be on or off, and an enum is perfect to represent this state, as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjh28zrheabvrj4uqcxvw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjh28zrheabvrj4uqcxvw.png" width="800" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you can use the above enum, just like any type in Typescript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffiqkfe5i4ilz77smyfms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffiqkfe5i4ilz77smyfms.png" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To understand the difference between enums and const enums, we first need to understand their behavior after compilation before the behavior is very similar.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;If you want to learn Typescript with weekly lessons, please subscribe to my &lt;a href="https://www.allthingstypescript.dev/" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;In the book &lt;a href="https://effectivetypescript.com/" rel="noopener noreferrer"&gt;Effective Typescript by Don Vanderkom&lt;/a&gt; (a strongly recommended read), the author says that typescript has no impact on Javascript runtime. This is because Types are removed during transpilation, leaving you with good old Javascript. You can learn more about this relationship in this &lt;a href="https://www.allthingstypescript.dev/p/typescript-and-javascript-two-sides" rel="noopener noreferrer"&gt;issue&lt;/a&gt;, which we covered a few weeks ago.&lt;/p&gt;

&lt;p&gt;Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript—&lt;a href="https://www.typescriptlang.org/docs/handbook/enums.html" rel="noopener noreferrer"&gt;Official TS Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Of course, this isn’t always the case, and Enums are one of those exceptions, with Enums getting converted into objects instead of being removed, as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fods6q0n1i6nlnvsbde7p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fods6q0n1i6nlnvsbde7p.png" width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, when we pass an enum, the object that is produced during the transpilation process is referenced, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fkfi31i0v6s61l1ga7uw1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkfi31i0v6s61l1ga7uw1.png" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another thing to note about enums is that the values of each element in an Enum are numbers - &lt;em&gt;incrementing from 0, from the top down, unless the first number is indicated&lt;/em&gt; - instead of the values we entered above, so in our case, &lt;code&gt;ON&lt;/code&gt; would be &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;Off&lt;/code&gt; would be &lt;code&gt;1&lt;/code&gt;. Below is a case where the initial number is indicated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fafotjmv4sx95pjxkhcit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fafotjmv4sx95pjxkhcit.png" width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, On is &lt;code&gt;100&lt;/code&gt; and Off is &lt;code&gt;101&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This isn’t always the desired behavior, but it can be fixed by explicitly adding the value of each element in the enum, such as having the value be a string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fal95ddemhziuroavwrdy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fal95ddemhziuroavwrdy.png" width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;String Enums&lt;/p&gt;

&lt;p&gt;Or even an expression, as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F2p9ad3u002sf5aggan0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2p9ad3u002sf5aggan0h.png" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Computed Enums&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;If you like my content and want to support my work, please consider supporting me (&lt;/strong&gt;&lt;em&gt;&lt;strong&gt;you can buy me a double latte, the juice that powers my creativity and determination ☕️&lt;/strong&gt;&lt;/em&gt;&lt;strong&gt;) through &lt;a href="https://github.com/sponsors/mainawycliffe/" rel="noopener noreferrer"&gt;Github Sponsors&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/sponsors/mainawycliffe/" rel="noopener noreferrer"&gt;☕️ Consider buying me a cup of coffee&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Const Enums
&lt;/h3&gt;

&lt;p&gt;This isn’t always desired, and that's where const enums come in. They are just like regular enums, but they are prefixed by the const keyword before their declaration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F3yybucis49ywhyrc772c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3yybucis49ywhyrc772c.png" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The other important difference is that const enum values get inlined instead of producing an object, taking us back to zero impact on the JS runtime, as const enums do not generate objects after transpilation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fg4at683zyn76p1oszedn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fg4at683zyn76p1oszedn.png" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Generally speaking, regular enums are valid solutions, but in those cases where you don’t want to pay the penalty of extra code generated by using regular enums, const enums are a very good solution, as they don’t generate any code for their support.&lt;/p&gt;

&lt;p&gt;As you can see, no more &lt;code&gt;Switch&lt;/code&gt; object is being created. Of course, just like with regular enums, we still have the same issue of enum element values being incremental numbers unless otherwise specified.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fh5pn2s6wg2zw7u5r5e2z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fh5pn2s6wg2zw7u5r5e2z.png" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this issue, we examined the difference between enums and const enums. Unlike regular enums, const enums don’t generate the javascript object; the code is inlined. This means that you are not paying the penalty for extra code generation that you get when using regular enums.&lt;/p&gt;

&lt;p&gt;While this is normally not a concern for most projects, in some cases, it can have a negative impact, leading to large JS code being produced and longer build / transpilation times. This can easily be solved by using const enums, which would be easier to refactor compared to alternative solutions such as unions.&lt;/p&gt;

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