<?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>Forem: Adeel Imran</title>
    <description>The latest articles on Forem by Adeel Imran (@adeelibr).</description>
    <link>https://forem.com/adeelibr</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%2F174253%2Fee3e95e0-a53e-4b3f-8933-30ac99f205bd.jpeg</url>
      <title>Forem: Adeel Imran</title>
      <link>https://forem.com/adeelibr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/adeelibr"/>
    <language>en</language>
    <item>
      <title>Mastering Immutable Types with TypeScript `as const`</title>
      <dc:creator>Adeel Imran</dc:creator>
      <pubDate>Thu, 13 Jun 2024 10:46:58 +0000</pubDate>
      <link>https://forem.com/adeelibr/mastering-immutable-types-with-typescript-as-const-gh1</link>
      <guid>https://forem.com/adeelibr/mastering-immutable-types-with-typescript-as-const-gh1</guid>
      <description>&lt;p&gt;Enhance your TypeScript skills by mastering immutability and type safety with the &lt;code&gt;as const&lt;/code&gt; feature. This guide walks you through key steps, cautionary notes, and tips for efficiency. Watch the full video &lt;a href="https://www.youtube.com/watch?v=ztjMkfeFNrg"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/ztjMkfeFNrg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h3&gt;
  
  
  Objective
&lt;/h3&gt;

&lt;p&gt;To improve immutability and type safety in TypeScript by leveraging the &lt;code&gt;as const&lt;/code&gt; feature.&lt;/p&gt;

&lt;h3&gt;
  
  
  Section 1: Using &lt;code&gt;Object.freeze&lt;/code&gt; for Immutability
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Object.freeze&lt;/code&gt; is a method provided by JavaScript that makes an object immutable. When an object is frozen, you cannot add, delete, or modify its properties.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&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;const&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;home&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;contact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/contact&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;freeze&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Attempting to modify properties will fail&lt;/span&gt;
&lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/new-home&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: Cannot assign to 'home' because it is a read-only property.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Top-Level Immutability:&lt;/strong&gt; &lt;code&gt;Object.freeze&lt;/code&gt; ensures that the object &lt;code&gt;routes&lt;/code&gt; is read-only at the top level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nested Objects:&lt;/strong&gt; For complete immutability, nested objects require individual wrapping with &lt;code&gt;Object.freeze&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Section 2: Leveraging &lt;code&gt;as const&lt;/code&gt; for Immutability and Type Safety
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;as const&lt;/code&gt; feature in TypeScript enhances immutability and type safety by converting the values of an object into their literal types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&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;const&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;home&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;contact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/contact&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// The type of `routes` is now:&lt;/span&gt;
&lt;span class="c1"&gt;// {&lt;/span&gt;
&lt;span class="c1"&gt;//   readonly home: "/",&lt;/span&gt;
&lt;span class="c1"&gt;//   readonly about: "/about",&lt;/span&gt;
&lt;span class="c1"&gt;//   readonly contact: "/contact"&lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Literal Types:&lt;/strong&gt; &lt;code&gt;as const&lt;/code&gt; converts the values into their literal types, providing precise type safety.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read-Only Properties:&lt;/strong&gt; All properties become read-only, ensuring immutability without requiring &lt;code&gt;Object.freeze&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Section 3: &lt;code&gt;as const&lt;/code&gt; vs. Enums
&lt;/h3&gt;

&lt;p&gt;While enums provide a way to define a set of named constants, &lt;code&gt;as const&lt;/code&gt; offers a more intuitive and flexible approach for certain scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Comparison:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using Enums:&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="kr"&gt;enum&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;About&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Contact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/contact&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="nf"&gt;goToRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Logic to navigate to the route&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;goToRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;as const&lt;/code&gt;:&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;const&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;home&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;about&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;contact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/contact&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&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;Routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;goToRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;route&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Logic to navigate to the route&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;goToRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Type-safe and more intuitive&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Benefits of &lt;code&gt;as const&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Improved Type Safety:&lt;/strong&gt; Using &lt;code&gt;as const&lt;/code&gt; with &lt;code&gt;keyof typeof&lt;/code&gt; ensures that only valid keys are used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Intuitive Syntax:&lt;/strong&gt; The syntax is straightforward and more closely aligned with JavaScript objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better Autocomplete:&lt;/strong&gt; IDEs provide better autocomplete suggestions with &lt;code&gt;as const&lt;/code&gt;, enhancing the developer experience.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By integrating these techniques, you can effectively master immutable types with TypeScript using the &lt;code&gt;as const&lt;/code&gt; feature. Enhance your coding skills and ensure your applications are both robust and maintainable.&lt;/p&gt;




&lt;p&gt;For a more detailed walkthrough, check out the full video tutorial on &lt;a href="https://www.youtube.com/watch?v=ztjMkfeFNrg&amp;amp;t=69s"&gt;YouTube&lt;/a&gt;. Happy coding!&lt;/p&gt;




&lt;p&gt;Feel free to share this blog post with your developer community to help others improve their TypeScript skills. Engage with us on Twitter &lt;a class="mentioned-user" href="https://dev.to/adeelibr"&gt;@adeelibr&lt;/a&gt; for more coding tips and tutorials!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Vendor splitting using webpack 4</title>
      <dc:creator>Adeel Imran</dc:creator>
      <pubDate>Sun, 31 Oct 2021 11:42:30 +0000</pubDate>
      <link>https://forem.com/adeelibr/vendor-splitting-using-webpack-4-430g</link>
      <guid>https://forem.com/adeelibr/vendor-splitting-using-webpack-4-430g</guid>
      <description>&lt;p&gt;In your webpack config file simply add this piece of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;optimization: {
    minimizer: [
      new OptimizeCSSAssetsPlugin(),
    ],
    // Automatically split vendor and commons
    // https://twitter.com/wSokra/status/969633336732905474
    // https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
    splitChunks: {
      chunks: 'all',
      name: false,
    },
    // Keep the runtime chunk seperated to enable long term caching
    // https://twitter.com/wSokra/status/969679223278505985
    runtimeChunk: true,
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will need to have this webpack extension &lt;a href="https://github.com/NMFR/optimize-css-assets-webpack-plugin"&gt;https://github.com/NMFR/optimize-css-assets-webpack-plugin&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webpack</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>PowerShell Replace – How to Rename a File or Change a File Extension in Windows 10</title>
      <dc:creator>Adeel Imran</dc:creator>
      <pubDate>Mon, 22 Feb 2021 17:52:57 +0000</pubDate>
      <link>https://forem.com/adeelibr/powershell-replace-how-to-rename-a-file-or-change-a-file-extension-in-windows-10-3359</link>
      <guid>https://forem.com/adeelibr/powershell-replace-how-to-rename-a-file-or-change-a-file-extension-in-windows-10-3359</guid>
      <description>&lt;p&gt;Windows PowerShell is a Microsoft framework for automating tasks using a command line shell.&lt;/p&gt;

&lt;p&gt;In this tutorial we will learn on how to change a file name or its extension using either the relative directory or absolute directory.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/zIlmKtBQBnc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here is a short 3 minute video in which I walk through how to change a file name or its extension.&lt;/p&gt;

&lt;p&gt;We will be using Rename-Item cmdlet. You can read more in the official docs &lt;a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-item?view=powershell-7"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;Rename-Item&lt;/code&gt; we can not only rename a file but also move it into different directories or even change registry names as well.&lt;/p&gt;

&lt;p&gt;You can find some very good official examples on the PowerShell docs page &lt;a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-item?view=powershell-7#examples"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So let's begin.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to rename a file
&lt;/h3&gt;

&lt;p&gt;Open your PowerShell command line shell. Type in the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rename-Item -LiteralPath "hello.xls" -NewName "bye.xls"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rename-Item accepts two flags:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LiteralPath is the file you want to rename &lt;/li&gt;
&lt;li&gt;NewName is the new name of the file you want&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to change a file extension
&lt;/h3&gt;

&lt;p&gt;You can change the extension of a file similar to how you would rename it by simply doing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rename-Item -LiteralPath "hello.txt" -NewName "hello.docx"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you found this useful drop a message to me on &lt;a href="https://twitter/adeelibr"&gt;twitter/adeelibr&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Happy coding.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>windows</category>
      <category>powershell</category>
      <category>renameitem</category>
    </item>
  </channel>
</rss>
