<?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: Fatih Aziz</title>
    <description>The latest articles on DEV Community by Fatih Aziz (@fatihaziz).</description>
    <link>https://dev.to/fatihaziz</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%2F490425%2Fe02f643c-b62f-46da-970f-6495ec4da0cb.jpg</url>
      <title>DEV Community: Fatih Aziz</title>
      <link>https://dev.to/fatihaziz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fatihaziz"/>
    <language>en</language>
    <item>
      <title>String Literal Union Type with TypeGuard in Typescript</title>
      <dc:creator>Fatih Aziz</dc:creator>
      <pubDate>Tue, 05 Jul 2022 21:26:34 +0000</pubDate>
      <link>https://dev.to/fatihaziz/string-literal-union-type-with-typeguard-in-typescript-21l5</link>
      <guid>https://dev.to/fatihaziz/string-literal-union-type-with-typeguard-in-typescript-21l5</guid>
      <description>&lt;p&gt;Do you have a hard time dealing with typescript string literal union type?&lt;br&gt;
what is &lt;em&gt;string literal union type?&lt;/em&gt; 🤔&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HIc93UZi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2152/1%2AUQ52_dyVsUW0malTs8Q5HQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HIc93UZi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2152/1%2AUQ52_dyVsUW0malTs8Q5HQ.png" alt="string-literal-type" width="880" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As I understand it, its the way you could use string as union type. the string will be treated as “symbol” and will be different from normal string&lt;br&gt;
It can be anything you want, and it help you to select the correct value for the variable in the typescript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R_-DDgSQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnIkBOIGU9dilm3Ud7m-1hQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R_-DDgSQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnIkBOIGU9dilm3Ud7m-1hQ.png" alt="type-autocomplete" width="811" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Typescript smart intellisense can provide us option when we want to put value into it. 😎&lt;/p&gt;

&lt;p&gt;but sometimes its not perfect, like what we wanted.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DSIsnQmQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AyUAAJ1R3ofiPnfDjqDEFdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DSIsnQmQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AyUAAJ1R3ofiPnfDjqDEFdw.png" alt="string-literal-type-error" width="664" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;like you can see, the string is not pass as the string literal type. 💀&lt;br&gt;
so how you can pass it? 📦&lt;/p&gt;

&lt;p&gt;here the secrets: 😎&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vKFPrRxP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3164/1%2AyxA97QfM05G5pIFAsSp7cA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vKFPrRxP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3164/1%2AyxA97QfM05G5pIFAsSp7cA.png" alt="union-type-function" width="880" height="643"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need to change our codes a a bit.&lt;br&gt;
create an array that store all valid value, and make the type from it automatically. That’s it! The compiler wont complain anymore! 🎉&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tips: its type safe guard, which mean if you don’t input the right value you will get error! better catch this error or you might get trouble in the production line!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is different approach, that I like more, it give you ability to give a default value, instead of throwing error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L6c7bypw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3500/1%2AFPC1BMc0ngg4Uei31_towQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L6c7bypw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3500/1%2AFPC1BMc0ngg4Uei31_towQ.png" alt="another-approach-type-union" width="880" height="1068"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will keep the type guard, if you set the &lt;code&gt;throwError&lt;/code&gt;to false, and blank the &lt;code&gt;defaultValue&lt;/code&gt;, it will return type &lt;code&gt;“A” | “B” | undefined&lt;/code&gt;, so you can deal with &lt;code&gt;undefined&lt;/code&gt;value later.&lt;/p&gt;

&lt;p&gt;That’s All!&lt;br&gt;
I have search this solution from months ago! Finally made it!&lt;/p&gt;

&lt;p&gt;here I create a repo for you who interested to give feedback,&lt;br&gt;
&lt;a href="https://github.com/fatihaziz/StringLiteralEnumType/tree/main"&gt;https://github.com/fatihaziz/StringLiteralEnumType/tree/main&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like always,&lt;/p&gt;

&lt;p&gt;Good Luck, cheers! 🥂&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Also maybe you want to &lt;a href="https://ko-fi.com/fatihaziz"&gt;support&lt;/a&gt; me on Ko-Fi, or give me feedback &lt;a href="https://github.com/fatihaziz"&gt;here&lt;/a&gt;. All your support matters and i really appreciate it! &lt;br&gt;
Thank you! &lt;em&gt;😁&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

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