<?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: Yuto </title>
    <description>The latest articles on DEV Community by Yuto  (@3ayut0).</description>
    <link>https://dev.to/3ayut0</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1528355%2F45ac4390-50cf-4e4c-9a66-edddbdce5821.JPG</url>
      <title>DEV Community: Yuto </title>
      <link>https://dev.to/3ayut0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/3ayut0"/>
    <language>en</language>
    <item>
      <title>What is the Dependency Inversion Principle?</title>
      <dc:creator>Yuto </dc:creator>
      <pubDate>Sun, 27 Sep 2026 22:55:20 +0000</pubDate>
      <link>https://dev.to/3ayut0/what-is-the-dependency-inversion-principle-4mk2</link>
      <guid>https://dev.to/3ayut0/what-is-the-dependency-inversion-principle-4mk2</guid>
      <description>&lt;p&gt;I recently encountered this principle while I was learning modern server-side design in Node.js and found out that this is one of the core concepts of the SOLID Principles.&lt;/p&gt;

&lt;p&gt;I'm going to explain the Dependency Inversion Principle in short.&lt;/p&gt;

&lt;p&gt;In a nutshell, the Dependency Inversion Principle (a.k.a. DIP) says&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;High-level logic should not depend on low-level modules. Both should depend on abstraction.&lt;/li&gt;
&lt;li&gt;Abstraction must not depend on the details, but details should depend on abstraction.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first time I saw this explanation, that was all jargon. Absolutely rocket science.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Houston, we have a problem.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The longer I invested my time, the clearer my understanding became.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency Inversion Principle
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fluv7c6vf139uelq8c1ei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fluv7c6vf139uelq8c1ei.png" alt="The difference between using Dependency Inversion or not" width="800" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If I picture the Dependency Inversion Principle, it would be like this image.&lt;/p&gt;

&lt;p&gt;Take a look at the left side of the image; the Low-Level Module depends on the High-Level Module.&amp;nbsp;&lt;br&gt;
However, the right side shows both Modules depend on the Abstraction ( an Interface). Our goal is the right side.&lt;/p&gt;

&lt;p&gt;The module uses other modules with "import" and "use" called the &lt;strong&gt;Low-Level Module&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's dive into more details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7r7yewsmsqb8tnabtglu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7r7yewsmsqb8tnabtglu.png" alt="AN example of clean architecture" width="300" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On top of this layer, the Presentation layer is responsible for UI needs and must be flexible because it changes often. On the other hand, a domain layer, which represents business logic, doesn't have to be the same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The domain layer must be the most stable.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If there is a dependency between the modules, the dependent side will have lower stability and higher flexibility. This is because as you import more modules, the code becomes more complex. On the other hand, the one being depended upon will have higher stability and lower flexibility.&lt;/p&gt;

&lt;p&gt;You have to avoid importing other modules as much as you can in the domain layer; otherwise, your application gets lower stability.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example codes
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Infrastructure/CarRepository.ts&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CarRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;model&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="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Starting &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Domain/Car.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CarRepository&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../Infrastructure/CarRepository&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;carRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CarRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;model&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;carRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CarRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;startEngine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;carRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Toyota&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startEngine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This code violates DIP. "Domain/Car.ts" depends on &lt;code&gt;Infrastructure/CarRepository.ts&lt;/code&gt; and imports the CarRepository class from the infrastructure layer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh1kdsl6cf4dzys8nztfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh1kdsl6cf4dzys8nztfa.png" alt="Creating CarRepositoryClass" width="300" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How can we achieve DIP? The steps are like this below&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an interface for CarRepository (We name it ICarRepository)&lt;/li&gt;
&lt;li&gt;Implement CarRepository by implementing that interface&lt;/li&gt;
&lt;li&gt;Make the Car class dependent on that interface.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Steps 1 and&amp;nbsp;2&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="c1"&gt;// Domain/Car.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;CarRepository&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../Infrastructure/CarRepository&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Creating an interface&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ICarRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// Receiving carRepository&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;carRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ICarRepository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;carRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;carRepository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;startEngine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Using the carRepository you received &lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;carRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&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;Using Dependency Injection (a.k.a. DI)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get rid of the declaration of carRepository from the domain layer.&lt;/li&gt;
&lt;li&gt;Create an interface for CarRepository.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, the constructor takes an instance of ICarRepository and assigns it to the carRepository.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&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="c1"&gt;// Infrastructure/CarRepository.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ICarRepository&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../Domain/Car&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CarRepository&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;ICarRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;model&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="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Starting &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&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;Using the interface to define CarRepository.&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="c1"&gt;// main&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;carRepository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CarRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Toyota&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;carRepository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startEngine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating a new Car instance. The carRepository is passed as a parameter. This demonstrates Dependency Injection, where the Car class receives the repository it should work with.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk7pp9xdclo2xji2vpxo7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk7pp9xdclo2xji2vpxo7.png" alt=" " width="300" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the Domain layer is independent of the Infrastructure layer; however, the Infrastructure layer depends on the Domain layer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is called Dependency Inversion. If there is a change in the Infrastructure layer, it doesn't affect the Domain layer.&lt;br&gt;
In other words, business logic is independent of the choice of technologies. It allows you to make a robust application.&lt;/p&gt;

</description>
      <category>solidprinciples</category>
      <category>node</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Using Array.at() over Array[index]</title>
      <dc:creator>Yuto </dc:creator>
      <pubDate>Sun, 19 Jan 2025 05:11:07 +0000</pubDate>
      <link>https://dev.to/3ayut0/using-arrayat-over-arrayindex-12lk</link>
      <guid>https://dev.to/3ayut0/using-arrayat-over-arrayindex-12lk</guid>
      <description>&lt;p&gt;In this article, I'll talk about why we might want to use Array.prototype.at() over Array[index] when accessing elements of an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;When accessing elements of an array, I used to use Array[index], similar to Array[1].&lt;br&gt;
This is what I have been familiar with and what I learned when grabbing an element.&lt;/p&gt;

&lt;p&gt;However, I recently received a code review from one of my coworkers, who said "Why don't you use Array.prototype.at() instead of relying on the index?"&lt;/p&gt;

&lt;p&gt;My code was&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const element = arr[1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and he suggested&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const element = arr.at(1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This way was eye-opening to me because it looked pretty straightforward. &lt;/p&gt;
&lt;h2&gt;
  
  
  How to use Array.prototype.at()
&lt;/h2&gt;

&lt;p&gt;Let me introduce you to what Array.prototype.at() is capable of.&lt;/p&gt;

&lt;p&gt;It takes an integer and returns an element of an array.&lt;/p&gt;

&lt;p&gt;For instance, you have an array&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const arr = ["One", "Two", "Three"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then you call&lt;/p&gt;

&lt;p&gt;&lt;code&gt;arr.at(0) // returns "One"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's straightforward.&lt;/p&gt;

&lt;p&gt;It's equivalent to bracket notation,  array[0]. You might wonder what's the deal. We are going to dive into the benefits of using this method.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Array[index] is not ideal?
&lt;/h2&gt;

&lt;p&gt;Let's take a look at a couple of situations where we might want to use Array.prototype.at() method instead of Array[index].&lt;/p&gt;
&lt;h3&gt;
  
  
  Getting the last element of an array.
&lt;/h3&gt;

&lt;p&gt;Suppose you have an array of strings such as&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const sports = ["basketball", "baseball", "football"]&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
and when you want to pick up the last element of this array, "football" in this case you could write this code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const lastElement = sports[sports.length - 1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is the correct way; however, you can write differently with Array.prototype.at() method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const lastElment = sports.at(-1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Don't you think it's way readable?&lt;/p&gt;
&lt;h3&gt;
  
  
  Type inference
&lt;/h3&gt;

&lt;p&gt;In TypeScript, bracket notation doesn't give a possibility of being undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr:string[] = []
const element = arr[0]
console.log(element) // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value, element is typed as &lt;code&gt;string&lt;/code&gt; not &lt;code&gt;string | undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We expect TypeScript to give a compile error while writing code.&lt;/p&gt;

&lt;p&gt;In general, we want to ensure the array has an element we try to access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr:string[] = []
const element = arr[0]
if(typeof element === 'string') console.log(element) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Weirdly, we are checking the element's type that TypeScript infers as &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As you might wonder, we can do type assertions like&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const element : string | undefined = arr[0]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;however, this is the last thing we want to do because we don't have to assume ourselves writing the perfect code.&lt;/p&gt;

&lt;p&gt;To solve this problem, we can do two things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write a type-guard function&lt;/li&gt;
&lt;li&gt;Use noUncheckedIndexedAccess&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/tsconfig/#noUncheckedIndexedAccess" rel="noopener noreferrer"&gt;noUncheckedIndexedAccess&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both ways work well but if you use Array.prototype.at(), you don't; have do both of them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr:string[] = []
const element = arr.at(0) // string | undefined
console.log(element)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to insert the value element to other values that typed as string, you'd get a compile error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrOfFruit = ["Apple", "Banana", "Grape"]
const arr:string[] = []
const element = arr.at(0)

arrOfFruit.includes(element)

Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You can write more straightforward code with Array.prototype.at() and avoid putting extra functions and configs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at" rel="noopener noreferrer"&gt;Array.prototype.at() explained in mozilla&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
