{"id":433,"date":"2023-02-07T05:00:00","date_gmt":"2023-02-07T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=433"},"modified":"2023-02-03T15:57:41","modified_gmt":"2023-02-03T23:57:41","slug":"union-types-in-typescript","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/union-types-in-typescript\/","title":{"rendered":"Union types in TypeScript"},"content":{"rendered":"\n<p>Yesterday, we talked about <code>any<\/code>, <code>unknown<\/code>, and the pros and cons of using such types.<\/p>\n\n\n\n<p>Today, let&#8217;s focus on <a href=\"https:\/\/blog.angulartraining.com\/union-types-in-typescript-2517a82a1ea0\" target=\"_blank\" rel=\"noopener\" title=\"\">union types<\/a>, an alternative to <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/enums.html\" target=\"_blank\" rel=\"noopener\" title=\"\">enums<\/a> that are more performant and flexible to describe custom types. Let&#8217;s assume we need to support different currencies in our application. Since currencies are mostly a code\/symbol, we could take the easy route and do the following:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"181\" height=\"38\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-4.png\" alt=\"\" class=\"wp-image-435\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>But this doesn&#8217;t help us much. For example, is any <code>string<\/code> a valid currency? Definitely not. So a better option would be to create an <code>enum<\/code> for it and describe all possible values in it:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"358\" height=\"40\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-5.png\" alt=\"\" class=\"wp-image-436\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-5.png 358w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-5-300x34.png 300w\" sizes=\"auto, (max-width: 358px) 100vw, 358px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Now we have a proper type to describe what a <code>Currency<\/code> is. But <code>enums<\/code> aren&#8217;t that convenient because they can&#8217;t be used as-is in an Angular component template, requiring <a href=\"https:\/\/stackblitz.com\/edit\/angular-lnwbhz?file=src%2Fmain.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">some workarounds<\/a>. Also, they get compiled into a <a href=\"https:\/\/www.typescriptlang.org\/play?q=283#example\/enums\" target=\"_blank\" rel=\"noopener\" title=\"\">monster structure of JavaScript<\/a> that increases the size of your code bundle for no valid reason since we don&#8217;t have type-checking at runtime in the browser.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Enter union types<\/h2>\n\n\n\n<p>Union types solve all these problems. They&#8217;re lightweight and do not get compiled into anything at all, which means they&#8217;re always more performant than <code>enums<\/code> (less code to download + less code to interpret = faster app in the browser). They guarantee type safety and can be made out of anything, including strings:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"376\" height=\"43\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-6.png\" alt=\"\" class=\"wp-image-437\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-6.png 376w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-6-300x34.png 300w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>A lot more can be done with union types, such as having more options than just one single type for a given variable:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-7.png\" alt=\"\" class=\"wp-image-440\" width=\"319\" height=\"47\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-7.png 319w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-7-300x44.png 300w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Angular uses union types <strong>a lot<\/strong>. For instance, if you&#8217;ve ever used the <a href=\"https:\/\/angular.io\/api\/router\/CanActivate\" target=\"_blank\" rel=\"noopener\" title=\"\">canActivate router guard<\/a>, its signature supports up to 6 different return types, all defined with union types:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"692\" height=\"124\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-9.png\" alt=\"\" class=\"wp-image-445\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-9.png 692w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-9-300x54.png 300w\" sizes=\"auto, (max-width: 692px) 100vw, 692px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And we can have unions of union types when more granularity is needed with specific sub-types:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"523\" height=\"81\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-8.png\" alt=\"\" class=\"wp-image-441\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-8.png 523w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-8-300x46.png 300w\" sizes=\"auto, (max-width: 523px) 100vw, 523px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we talked about any, unknown, and the pros and cons of using such types. Today, let&#8217;s focus on union types, an alternative to enums that are more performant and flexible to describe custom types. Let&#8217;s assume we need to support different currencies in our application. Since currencies are mostly a code\/symbol, we could take [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,3],"tags":[],"class_list":["post-433","post","type-post","status-publish","format-standard","hentry","category-performance","category-typescript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/433","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=433"}],"version-history":[{"count":9,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/433\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/433\/revisions\/449"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}