<?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: Abhishek S</title>
    <description>The latest articles on DEV Community by Abhishek S (@abhishekvash).</description>
    <link>https://dev.to/abhishekvash</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%2F359382%2Fca5f08cc-da10-4798-95a3-4163c595361e.jpeg</url>
      <title>DEV Community: Abhishek S</title>
      <link>https://dev.to/abhishekvash</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhishekvash"/>
    <language>en</language>
    <item>
      <title>CSS Painkillers:has() selector</title>
      <dc:creator>Abhishek S</dc:creator>
      <pubDate>Mon, 16 Oct 2023 14:32:34 +0000</pubDate>
      <link>https://dev.to/abhishekvash/css-painkillershas-selector-1n50</link>
      <guid>https://dev.to/abhishekvash/css-painkillershas-selector-1n50</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is the first in an ongoing series of posts called CSS Painkillers, covering seldom heard or used CSS concepts easing my development journey. So, if it’s your cup of tea, follow for more!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For the longest time, and especially after the advent of JS frameworks (yes React is a framework too), it has been easier to apply dynamic styles via JS. Dynamic styles are pivotal to modern web applications, since they are the backbone of interactivity. They allow for immediate user feedback after events. Therefore, using application state to conditionally and dynamically apply CSS styles is a common practice which most frameworks facilitate. Let’s take the following example:&lt;/p&gt;

&lt;p&gt;There is a table in which the first cell in each row contains a checkbox to select or deselect the row. If the checkbox is checked, you apply a style (let’s say a background color) to the entire row to highlight it. Since using CSS selectors exclusively would be a painful approach, you would use JS. In Vue, you would do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;tr&lt;/span&gt;
    &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"row in someData"&lt;/span&gt;
    &lt;span class="na"&gt;:key=&lt;/span&gt;&lt;span class="s"&gt;"row.id"&lt;/span&gt;
    &lt;span class="na"&gt;:class=&lt;/span&gt;&lt;span class="s"&gt;"{ 'row-selected': row.isSelected } //dynamically adding a class here based on whether row is selected"&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt;
        &lt;span class="na"&gt;:id=&lt;/span&gt;&lt;span class="s"&gt;"`row-${row.id}`"&lt;/span&gt;
        &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt;
        &lt;span class="na"&gt;:name=&lt;/span&gt;&lt;span class="s"&gt;"row.data"&lt;/span&gt;
        &lt;span class="na"&gt;:value=&lt;/span&gt;&lt;span class="s"&gt;"row.isSelected"&lt;/span&gt;
        &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;change=&lt;/span&gt;&lt;span class="s"&gt;"toggleRowSelection"&lt;/span&gt;
      &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- Rest of the cells --&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Straightforward and easy to replicate across all frameworks. Until recently, I thought this was only way to accomplish this. I mean who would want to write CSS which looks like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* Original background */&lt;/span&gt;
&lt;span class="nt"&gt;tr&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* Change background color when checked, targeting siblings of the input checkbox */&lt;/span&gt;
&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;td&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;td&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;td&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Shivers&lt;/em&gt;. Anyway, things changed around 2022 when the eagerly waited &lt;code&gt;has()&lt;/code&gt; selector gained major(but not complete) browser support. All Chromium based browsers support it, Firefox is yet to implement it and support on mobile browsers is so-so. For the current state of browser support, check &lt;a href="https://caniuse.com/css-has"&gt;this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;has()&lt;/code&gt; selector, according to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:has"&gt;MDN&lt;/a&gt;, presents a way of selecting a parent element or a previous sibling element with respect to a reference element by taking a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Selector_list#relative_selector_list"&gt;relative selector list&lt;/a&gt; as an argument. That’s …. surprisingly trivial, but hey, when you haven’t had something for the longest time (still waiting on Subgrid T_T), it’s a breath of fresh air.&lt;/p&gt;

&lt;p&gt;Let’s see how the CSS using &lt;code&gt;has()&lt;/code&gt; looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it! A bit anti-climactic if you ask me. But this little guy is deviously powerful. &lt;/p&gt;

&lt;p&gt;I just wanted to introduce the topic to you, hence kept the post short with a simple example. But the scope for using the selector is big. Study it well, and the world’s your oyster. And as always, remember it’s a Swiss Army knife, not a golden hammer. &lt;/p&gt;

&lt;p&gt;So how do you see yourself using the &lt;code&gt;has()&lt;/code&gt; selector? Let me know in the comments!&lt;/p&gt;

</description>
      <category>css</category>
      <category>ui</category>
      <category>webdev</category>
    </item>
    <item>
      <title>TypeScript's Discriminated Unions and How I Began to Worry Less</title>
      <dc:creator>Abhishek S</dc:creator>
      <pubDate>Fri, 15 Sep 2023 14:25:57 +0000</pubDate>
      <link>https://dev.to/abhishekvash/typescripts-discriminated-unions-and-how-i-began-to-worry-less-22j6</link>
      <guid>https://dev.to/abhishekvash/typescripts-discriminated-unions-and-how-i-began-to-worry-less-22j6</guid>
      <description>&lt;p&gt;If you’ve used Typescript in any real capacity, you’ve probably run into unions. Either defined in third party libraries or your own definitions. You can define a union with almost any combination or number of types. They’re pretty simple and generally look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Stage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;empty&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;personalInfo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;billingInfo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;allowSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Stage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that only computationally supported values are passable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;allowSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;empty&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// OK&lt;/span&gt;
&lt;span class="nx"&gt;allowSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inPayment&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Error: Argument of type '"inPayment"' is not assignable to parameter of type 'Stage'.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This by itself made the code more robust. But what about more advanced cases? &lt;/p&gt;

&lt;p&gt;Consider the snippet below. It defines a type for attachments of media posts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PostAttachment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Can be "image", "video" or "audio"&lt;/span&gt;
  &lt;span class="nl"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;altText&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;lowResUrl&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;thumbnailUrl&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;autoplay&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks fine on the surface, but that’s a lot more optional properties than I would like. TypeScript will make you explicitly check for the existence of those properties. That's a lot more if statements. Or if you're lazy, a lot of  &lt;code&gt;as string&lt;/code&gt; assertions (Officer! He's right here!). In short, it's a pain.&lt;/p&gt;

&lt;p&gt;This is where discriminated unions come into play. Let’s break down the conditions for the properties.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the type is &lt;code&gt;image&lt;/code&gt;, you will have altText and lowResUrl&lt;/li&gt;
&lt;li&gt;When the type is &lt;code&gt;video&lt;/code&gt;, you will have altText, thumbnailUrl and autoplay&lt;/li&gt;
&lt;li&gt;When the type is &lt;code&gt;audio&lt;/code&gt;, you will have only autoplay&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let’s transform the above type to something more intuitive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;image&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;altText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;lowResUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;altText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;thumbnailUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;autoplay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Audio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;audio&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;autoplay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PostAttachment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Video&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Audio&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That looks a lot cleaner doesn’t it? By anchoring to the type property, TypeScript can narrow down on what fields are available.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;processAttachments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PostAttachment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;image&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// TypeScript knows that 'altText' and 'lowResUrl' exist here&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;altText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lowResUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// TypeScript knows that 'altText', 'thumbnailUrl', and 'autoplay' exist here&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;altText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;thumbnailUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;autoplay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// TypeScript knows that 'autoplay' exists here&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;autoplay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lowResUrl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Error: Property 'lowResUrl' does not exist on type 'Audio'.&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've kept the example pretty simple to keep the post short. But that should still give you an idea about how powerful these features are. Discriminated unions offer a robust way to handle different and often complex shapes of data, in a type-safe manner. It’s like a Swiss Army knife—versatile, efficient, and indispensable once you understand its uses. They've made my life easier, and I'm sure they'll do the same for you. But remember it’s a Swiss Army knife, not a golden hammer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tQ28etqu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwnih4edv0r3tq2qvt03.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tQ28etqu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwnih4edv0r3tq2qvt03.jpg" alt="Image description" width="601" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Discriminated unions are a TypeScript feature, not an HR issue!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>typescript</category>
      <category>discriminatedunions</category>
      <category>typesafety</category>
      <category>programming</category>
    </item>
    <item>
      <title>Software Engineering: The Textbook Omissions</title>
      <dc:creator>Abhishek S</dc:creator>
      <pubDate>Wed, 06 Sep 2023 04:29:34 +0000</pubDate>
      <link>https://dev.to/abhishekvash/software-engineering-the-textbook-omissions-3ge8</link>
      <guid>https://dev.to/abhishekvash/software-engineering-the-textbook-omissions-3ge8</guid>
      <description>&lt;p&gt;It’s been two years since I emerged as the victor from interview hell. After countless sleepless nights, hours in code reviews, and thousands of lines of code in production, what are my thoughts on this line of work? Is it everything I dreamt of? Yes and no. I love it, and I’ve learned almost everything I know on the job. But it’s not all sunshine and rainbows. The grass is less green than I thought, and the rose tint on my glasses has worn off. Several gotchas almost got me. So, here are some things I wish I had known earlier in my brief experience as a UI engineer.&lt;/p&gt;

&lt;h2&gt;
  
  
  It’s not just about being good at the technical stuff
&lt;/h2&gt;

&lt;p&gt;You can churn out 10k lines of code a week, have flawless code reviews, and even know everything there is to know (Hey, Sheldon Cooper). But if you're difficult to work with, that's not going to fly. Being a team player and open to collaboration can take you further than going solo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JQfapdCd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtijmi8xyjicj5fnb9pl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JQfapdCd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtijmi8xyjicj5fnb9pl.png" alt="Image description" width="521" height="496"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hence, communication is as, if not more, important than code
&lt;/h2&gt;

&lt;p&gt;Facing an approaching deadline or a feature you don’t know how to build? Is there a bug that no matter how much you scratch your brains out, you can’t fix? There’s a high chance that things will work out if you talk about it with your peers/superiors. It's not productive to scour StackOverflow for countless hours all by yourself. People would rather know about issues as soon as possible than get blindsided at the last moment. Bite that bullet and send that Slack dammit.&lt;/p&gt;

&lt;p&gt;Communication is also key to efficiency. Good communication helps get ideas across without much hassle. This allows brainstorming to yield more meaningful results. It makes pair programming a fun experience. It makes the workplace a well-oiled machine. Speak out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Put in a conscious effort towards a work-life balance
&lt;/h2&gt;

&lt;p&gt;It feels so good when you pull in those 30 hours non-stop to get that one feature up and running. It’s immensely gratifying. But before you know it, you’re doing it all the time. And unsurprisingly, you’ve become disconnected. Things you used to like don’t interest you anymore. And you don’t seem to find the time to do them either anyway. And that is a one-way ticket to ‘I hate my job’-ville. You need to make a conscious effort to maintain a work-life balance to ensure both go smoothly. Get yourself some ‘Me’ time. Hang out with your friends and family. Finish that book you’ve left half-read.&lt;/p&gt;

&lt;p&gt;So yes, hustle. But hustle responsibly. You don’t want to break down at the age of 25.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write code for humans, computers can understand it anyway
&lt;/h2&gt;

&lt;p&gt;There are thousands of books and articles on Clean Code practices, that’s how important it is. Code readability takes priority over efficiency. Efficiency cannot come at the cost of spaghetti code. If you can't avoid writing convoluted logic, drop in appropriate code comments. Go a step further to make a document for it.&lt;/p&gt;

&lt;p&gt;We created a well-defined coding policy. This helped us put the best practices in place. Peer reviews, peer programming, and maintenance have never been easier. In the end, readability and context matter. Think about the poor chap who has to work on it years down the line (might even end up being you).&lt;/p&gt;

&lt;p&gt;And hey, if you work on web UIs like me, who are you kidding thinking about efficiency and performance? I mean come on, it's Javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  But you will bury a few bodies
&lt;/h2&gt;

&lt;p&gt;I just told you clean code is important. You watch a ton of YouTube tutorials focusing on how to write the most pristine code. You read a gazillion articles about the correct way of doing things. You now love good engineering, to an extent where it’s intellectual masturbation. But you know what, life ain’t full of unicorns. There are unrealistic deadlines. Realistic expectations. You just need to get shit done. When companies have to choose between well-written software and fast delivery (what is fast delivery? Well this Friday), they often go for speed. Because that’s how businesses work. Sometimes getting something in the hands of customers, even if it's a little shabby, can make or break it. So there is always, however little, technical debt.&lt;/p&gt;

&lt;h2&gt;
  
  
  So document… everything
&lt;/h2&gt;

&lt;p&gt;Hacked something together for now? Found a cool short way to implement a function off of Stack Overflow? Found a neat niche CSS trick to position that button? Are you using a hardly known edge case library function? Document it. Add good comments. even add a ReadMe with the link to the source. Chances are even you won’t remember it 3 months down the line. And there’s no way another developer jumping into the code would understand it. Have some empathy for future developers (which might include you).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V9beXx8z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbpyleg3br2eqn9u5m3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V9beXx8z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbpyleg3br2eqn9u5m3p.png" alt="Image description" width="634" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember, a comment should ideally not tell me what is going on. That’s the job of well-named variables, functions and files. A comment should tell me the why and in rare cases, the how.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Key Takeaway: Find your USP and improve on it
&lt;/h2&gt;

&lt;p&gt;If you take just one thing from this article, it should be this. Every person is different. Each of us has something unique to offer. For some, it may be plain right off the bat, but for some, not so much. Take some time off for yourself and think about it ( caution: existential crisis lies ahead! ) What’s something of value that only you can provide? It might be simple. It might be weird.&lt;/p&gt;

&lt;p&gt;I come from a creative background (I’m a musician). And I also seem to have great attention to detail. This helps me build pixel-perfect UIs. I always make an effort to think outside the box and further improve on the intended designs. I always look to empower our designers with ways to make our UIs delightful.&lt;/p&gt;

&lt;p&gt;My strengths help elevate our products. It helps me provide value on top of my expected responsibilities. So, find your unique strengths. Work towards integrating it into your daily life. You might see an immediate change in all areas of life, not just work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Something to figure out: It can get overwhelming
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MUy2QEGm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr43f55bvgri4m5rs69i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MUy2QEGm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr43f55bvgri4m5rs69i.png" alt="Image description" width="508" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Software moves really fast (yeah I’m not talking about you, Enterprise). It can get overwhelming trying to keep up with everything. How do you know which hype is real vs. a dud? How do you know if the 2349239th Javascript released today really is, in fact, a game changer? What if you bet on the wrong stack? What if you misjudged something, and have now suddenly, fallen behind? Let me know in the comments.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>softskills</category>
      <category>newbie</category>
      <category>career</category>
    </item>
  </channel>
</rss>
