{"id":1893,"date":"2023-12-19T05:00:00","date_gmt":"2023-12-19T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1893"},"modified":"2023-12-18T15:19:04","modified_gmt":"2023-12-18T23:19:04","slug":"typescript-enums-and-why-i-avoid-them","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/typescript-enums-and-why-i-avoid-them\/","title":{"rendered":"Typescript enums and why I avoid them"},"content":{"rendered":"\n<p>As an <a href=\"https:\/\/angulartraining.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular coach<\/a>, I review a lot of Angular applications. One thing that I see in a lot of code bases is the use of Typescript enums to store a bunch of constants:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"54\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-18.png\" alt=\"\" class=\"wp-image-1894\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-18.png 619w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-18-300x26.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above code creates a new type <code>CompassDirection<\/code> with four possible values: <code>CompassDirection.North<\/code>, <code>CompassDirection.East<\/code>, etc.<\/p>\n\n\n\n<p>Each constant gets assigned a numerical value starting at 0, so in that example, CompassDirection.North is equal to 0, and <code>CompassDirection.North<\/code> is equal to 3.<\/p>\n\n\n\n<p>These values can be customized, and we can use strings, objects, or anything we want instead of numbers:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"56\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-19.png\" alt=\"\" class=\"wp-image-1895\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-19.png 908w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-19-300x19.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-19-768x47.png 768w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>And that&#8217;s about all you can do with <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/enums.html\" target=\"_blank\" rel=\"noopener\" title=\"\">enums in Typescript<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why I avoid enums<\/h2>\n\n\n\n<p>I avoid enums because they&#8217;re neither convenient nor performant. For instance, this is what the last example gets compiled into by the Typescript compiler:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"609\" height=\"246\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-20.png\" alt=\"\" class=\"wp-image-1896\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-20.png 609w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-20-300x121.png 300w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>That&#8217;s not pretty, and it comes at the extra cost of all that code being downloaded in the browser and then interpreted, which impacts performance.<\/p>\n\n\n\n<p>Also, enums aren&#8217;t very convenient to be used in component templates. If I want to use an enum in a component template, I need this additional code to make that type accessible on the component instance (the <code>this<\/code> reference):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"477\" height=\"383\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-21.png\" alt=\"\" class=\"wp-image-1897\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-21.png 477w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-21-300x241.png 300w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Instead, we could use a <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/union-types-in-typescript\/\" target=\"_blank\" rel=\"noopener\" title=\"\">union type<\/a>: <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"757\" height=\"55\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-22.png\" alt=\"\" class=\"wp-image-1898\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-22.png 757w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-22-300x22.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-22-750x55.png 750w\" sizes=\"auto, (max-width: 757px) 100vw, 757px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>We still get a type associated with specific values, but now this gets compiled into the following:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"237\" height=\"71\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-23.png\" alt=\"\" class=\"wp-image-1899\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>No, I didn&#8217;t forget anything in that black rectangle: Union types are like interfaces and don&#8217;t get compiled into anything, meaning they do not increase the size of your code base or impact performance. Also, since those types are just the union of other types (such as strings or numbers), these constants can be used as-is in a component template without needing to tweak our component class. <\/p>\n\n\n\n<p>In other words, union types preserve type safety without degrading our app&#8217;s performance, which is a win-win.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As an Angular coach, I review a lot of Angular applications. One thing that I see in a lot of code bases is the use of Typescript enums to store a bunch of constants: The above code creates a new type CompassDirection with four possible values: CompassDirection.North, CompassDirection.East, etc. Each constant gets assigned a numerical [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1893","post","type-post","status-publish","format-standard","hentry","category-typescript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1893"}],"version-history":[{"count":1,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1893\/revisions"}],"predecessor-version":[{"id":1900,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1893\/revisions\/1900"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}