<?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: Shubham_Baghel</title>
    <description>The latest articles on DEV Community by Shubham_Baghel (@shubhamb).</description>
    <link>https://dev.to/shubhamb</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%2F304901%2F818ed5b9-2e78-4d9b-852a-eb14da4f47f0.jpeg</url>
      <title>DEV Community: Shubham_Baghel</title>
      <link>https://dev.to/shubhamb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubhamb"/>
    <language>en</language>
    <item>
      <title>7 System Design Concepts I Wish I Had Learned Earlier</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Tue, 20 Oct 2026 14:19:28 +0000</pubDate>
      <link>https://dev.to/shubhamb/system-design-is-not-just-about-microservices-4pog</link>
      <guid>https://dev.to/shubhamb/system-design-is-not-just-about-microservices-4pog</guid>
      <description>&lt;p&gt;When we start building backend applications, we usually focus on writing APIs, connecting databases, and making everything work.&lt;/p&gt;

&lt;p&gt;And honestly, that's enough for many small applications.&lt;/p&gt;

&lt;p&gt;But things start getting interesting when more users come in.&lt;/p&gt;

&lt;p&gt;Suddenly, APIs become slow. The database gets overloaded. One service going down starts affecting other services. Sometimes the same request gets processed twice.&lt;/p&gt;

&lt;p&gt;That's when we start thinking about system design.I've been exploring different architecture concepts, and I wanted to share some of the things I think every backend developer should understand.&lt;/p&gt;

&lt;p&gt;Not with complicated definitions, but with examples we can relate to.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Load Balancer — What Happens When One Server Is Not Enough?
&lt;/h2&gt;

&lt;p&gt;Imagine you open an online shopping website during a big sale.&lt;/p&gt;

&lt;p&gt;Thousands of people are trying to use it at the same time.&lt;/p&gt;

&lt;p&gt;If all requests go to one server, that server might struggle to handle the traffic.&lt;/p&gt;

&lt;p&gt;One solution is to run multiple instances of the application and distribute requests between them.&lt;/p&gt;

&lt;p&gt;That's basically what a load balancer does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;           Users
              |
              v
        Load Balancer
         /    |    \
        v     v     v
      API 1  API 2  API 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of depending on one server, we have multiple instances handling requests.&lt;/p&gt;

&lt;p&gt;If one instance becomes unhealthy, the load balancer can stop sending traffic to it, assuming health checks and routing are configured correctly.&lt;/p&gt;

&lt;p&gt;Of course, running three servers isn't automatically enough. We also need to think about the database, shared state, and other dependencies.&lt;/p&gt;

&lt;p&gt;But the basic idea is simple: don't make one server do all the work when it doesn't have to.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. API Gateway — One Door for Your Application
&lt;/h2&gt;

&lt;p&gt;Let's say you're building an application with separate services for users, orders, payments, and notifications.&lt;/p&gt;

&lt;p&gt;Without a common entry point, the frontend might need to know where each service lives and how to communicate with it.&lt;/p&gt;

&lt;p&gt;An API gateway can simplify that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          Frontend
              |
              v
          API Gateway
          /    |    \
         v     v     v
       Users  Orders Payments
       Service Service Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frontend sends a request to the gateway, and the gateway routes it to the appropriate service.&lt;/p&gt;

&lt;p&gt;It can also help with authentication integration, rate limits, and other shared API concerns.&lt;/p&gt;

&lt;p&gt;Think of it like the reception desk in an office. You don't need to know where everyone sits; you explain what you need, and the receptionist directs you to the right person.&lt;/p&gt;

&lt;p&gt;One thing I've learned is that not every application needs an API gateway. For a small application, it might just add unnecessary complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Message Queues — Why Make Users Wait for Everything?
&lt;/h2&gt;

&lt;p&gt;Imagine a customer places an order.&lt;/p&gt;

&lt;p&gt;Your application needs to save the order, send an email, update analytics, and notify another service.&lt;/p&gt;

&lt;p&gt;Should the customer wait until every single task finishes?&lt;/p&gt;

&lt;p&gt;Probably not.&lt;/p&gt;

&lt;p&gt;Some work can happen in the background.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          Order Service
                |
                v
            Save Order
                |
                v
           Message Queue
            /        \
           v          v
      Email Worker  Analytics Worker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The order service can publish a message, and other components can process it separately.&lt;/p&gt;

&lt;p&gt;Tools like Amazon SQS, RabbitMQ, and Kafka help with different kinds of messaging requirements.&lt;/p&gt;

&lt;p&gt;The benefit is that the user doesn't need to wait for every background task to finish.&lt;/p&gt;

&lt;p&gt;But there's a catch: messages can sometimes be delivered more than once, consumers can fail, and processing might be delayed.&lt;/p&gt;

&lt;p&gt;That's why retries, error handling, and idempotency matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Caching — Do We Really Need to Query the Database Every Time?
&lt;/h2&gt;

&lt;p&gt;Imagine your application displays a list of product categories.&lt;/p&gt;

&lt;p&gt;Thousands of users request the same information every day, but the categories rarely change.&lt;/p&gt;

&lt;p&gt;Does it make sense to query the database every time?&lt;/p&gt;

&lt;p&gt;Sometimes, no.&lt;/p&gt;

&lt;p&gt;We can keep frequently requested data in a cache, such as Redis, and return it without making another database query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       API Request
            |
            v
        Redis Cache
         /       \
      Found     Not Found
        |           |
        v           v
     Return      Database
                  |
                  v
              Store in Cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can reduce database load and improve response times.&lt;/p&gt;

&lt;p&gt;But caching isn't always straightforward.&lt;/p&gt;

&lt;p&gt;What happens when the price of a product changes? What if the cache still contains the old price?&lt;/p&gt;

&lt;p&gt;Now we have another problem to solve: keeping cached data reasonably up to date.&lt;/p&gt;

&lt;p&gt;Caching can be very useful, but adding it without understanding the data and its update patterns can create confusing bugs.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Circuit Breaker — What If Another Service Stops Responding?
&lt;/h2&gt;

&lt;p&gt;Imagine your order service depends on a payment service.&lt;/p&gt;

&lt;p&gt;The payment service suddenly becomes slow.&lt;/p&gt;

&lt;p&gt;Your order service keeps calling it, waiting for responses, and consuming resources.&lt;/p&gt;

&lt;p&gt;Soon, your own service might start struggling too.&lt;/p&gt;

&lt;p&gt;A circuit breaker helps limit this situation.&lt;/p&gt;

&lt;p&gt;Think of an electrical circuit breaker at home. When something goes wrong, it interrupts the connection to prevent further problems.&lt;/p&gt;

&lt;p&gt;In software, a circuit breaker generally has three states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Closed:&lt;/strong&gt; Requests are allowed through.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open:&lt;/strong&gt; Calls are blocked temporarily because the dependency has been failing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Half-open:&lt;/strong&gt; A few calls are allowed to check whether the dependency has recovered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This doesn't fix the payment service. It helps stop that failure from causing even more trouble elsewhere.&lt;/p&gt;

&lt;p&gt;We still need sensible timeouts, retry policies, monitoring, and proper handling of failed payments.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Database Replication vs Sharding — They're Not the Same Thing
&lt;/h2&gt;

&lt;p&gt;As an application grows, the database can become a bottleneck.&lt;/p&gt;

&lt;p&gt;Two terms that often come up in system design discussions are replication and sharding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Replication&lt;/strong&gt; means keeping copies of data on multiple database instances.&lt;/p&gt;

&lt;p&gt;For example, a primary database might handle writes while replicas handle some read traffic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sharding&lt;/strong&gt; means splitting data across multiple database partitions or servers.&lt;/p&gt;

&lt;p&gt;Imagine a shopping application with millions of customers. Instead of storing every customer record on one database server, records can be distributed across multiple shards.&lt;/p&gt;

&lt;p&gt;Here's the simple difference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Replication gives you copies of data.&lt;/li&gt;
&lt;li&gt;Sharding divides data across different places.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both can help with particular scaling problems, but both add complexity.&lt;/p&gt;

&lt;p&gt;Replicas can have delays before receiving new changes, and sharding introduces questions about how data should be distributed and queried.&lt;/p&gt;

&lt;p&gt;So before reaching for either solution, I'd first want to understand what is actually causing the database problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Monitoring and Logs — How Do We Know What's Going Wrong?
&lt;/h2&gt;

&lt;p&gt;Your application works perfectly on your laptop.&lt;/p&gt;

&lt;p&gt;You deploy it to production, and suddenly a customer reports that placing an order takes 15 seconds.&lt;/p&gt;

&lt;p&gt;Now what?&lt;/p&gt;

&lt;p&gt;Is the API slow? Is the database struggling? Is another service timing out?&lt;/p&gt;

&lt;p&gt;Without useful logs and metrics, debugging becomes guesswork.&lt;/p&gt;

&lt;p&gt;This is why observability matters.&lt;/p&gt;

&lt;p&gt;The three things I try to distinguish are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Logs:&lt;/strong&gt; What happened?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Metrics:&lt;/strong&gt; How is the system performing?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traces:&lt;/strong&gt; Where did a request spend its time across different services?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tools such as CloudWatch, Grafana, and OpenTelemetry can help collect and investigate this information.&lt;/p&gt;

&lt;p&gt;The goal isn't to collect as much data as possible. It's to collect information that helps us understand what went wrong and where to look.&lt;/p&gt;

&lt;h2&gt;
  
  
  One Last Thing: Don't Overcomplicate Your Architecture
&lt;/h2&gt;

&lt;p&gt;Something worth remembering is that we don't need all these components from day one.&lt;/p&gt;

&lt;p&gt;A small application with a single backend and a database might be perfectly fine.&lt;/p&gt;

&lt;p&gt;Adding Kafka, Redis, multiple microservices, and an API gateway just because they are popular can make the application harder to maintain.&lt;/p&gt;

&lt;p&gt;I'd rather start with a simple design, understand where the real problems are, and introduce additional components when they solve a specific problem.&lt;/p&gt;

&lt;p&gt;For me, that's the interesting part of system design: understanding the trade-offs, not just knowing the names of different technologies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I'm curious to hear from other backend developers:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What's one system design problem you've faced in a real project, and how did you solve it?&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
      <category>architecture</category>
    </item>
    <item>
      <title>TypeScript Tricks That Feel Like Cheating</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Wed, 14 Oct 2026 16:35:00 +0000</pubDate>
      <link>https://dev.to/shubhamb/typescript-tricks-that-feel-like-cheating-408n</link>
      <guid>https://dev.to/shubhamb/typescript-tricks-that-feel-like-cheating-408n</guid>
      <description>&lt;p&gt;TypeScript looks simple when you're writing basic types and interfaces.But once you start exploring its advanced features, you realize it can do much more than just check whether a variable is a string or a number.&lt;/p&gt;

&lt;p&gt;Some TypeScript features can help you catch mistakes at compile time, build smarter APIs, and even calculate types based on other types.&lt;/p&gt;

&lt;p&gt;Here are 8 TypeScript tricks that made me appreciate its type system a lot more.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Create Types From Existing Objects Using &lt;code&gt;typeof&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;What if you could create a TypeScript type directly from a JavaScript object instead of maintaining both separately?&lt;/p&gt;

&lt;p&gt;You can.&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;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;Config&lt;/code&gt; represents the structure inferred from &lt;code&gt;config&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But there is an important detail: &lt;code&gt;environment&lt;/code&gt; is inferred as &lt;code&gt;string&lt;/code&gt;, not specifically &lt;code&gt;"production"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you want to preserve literal values, use &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;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&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;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the properties have literal types, and they become readonly in the inferred type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why this is useful:&lt;/strong&gt; Configuration objects, shared constants, and avoiding duplicate type definitions.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Turn an Array Into a Union Type
&lt;/h2&gt;

&lt;p&gt;Imagine you have a list of supported payment methods.&lt;/p&gt;

&lt;p&gt;You want the list to be the source of truth, rather than maintaining a separate union type.&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;paymentMethods&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;card&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;paypal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bank-transfer&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="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;PaymentMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;paymentMethods&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting type is:&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;PaymentMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;card&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;paypal&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;bank-transfer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;typeof paymentMethods&lt;/code&gt; gets the array's type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[number]&lt;/code&gt; extracts the type of its elements.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;as const&lt;/code&gt; preserves each string as a literal type.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now this works:&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;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PaymentMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;card&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this fails:&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;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PaymentMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;crypto&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// TypeScript error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this is useful:&lt;/strong&gt; Roles, permissions, status values, supported features, and configuration options.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Use &lt;code&gt;satisfies&lt;/code&gt; Without Losing Type Inference
&lt;/h2&gt;

&lt;p&gt;This is one of my favorite TypeScript features.&lt;/p&gt;

&lt;p&gt;Sometimes you want to verify that an object follows a specific structure without replacing its inferred type with that structure.&lt;/p&gt;

&lt;p&gt;That's what &lt;code&gt;satisfies&lt;/code&gt; helps with.&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;Route&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;path&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;requiresAuth&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;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="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;requiresAuth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;dashboard&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/dashboard&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;requiresAuth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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="nx"&gt;satisfies&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeScript checks that every route matches the required structure.&lt;/p&gt;

&lt;p&gt;But unlike a straightforward type annotation, the &lt;code&gt;satisfies&lt;/code&gt; operator preserves useful details from the expression's inferred type.&lt;/p&gt;

&lt;p&gt;For example:&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;routes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dashboard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiresAuth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Inferred as boolean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expression retains its property information, while the &lt;code&gt;satisfies&lt;/code&gt; check ensures that the object conforms to &lt;code&gt;Record&amp;lt;string, Route&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why this is useful:&lt;/strong&gt; Route configurations, feature flags, application settings, and event maps.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Make Impossible States Impossible
&lt;/h2&gt;

&lt;p&gt;Imagine an API request can be in one of three states: loading, successful, or failed.&lt;/p&gt;

&lt;p&gt;A poorly designed type might allow contradictory combinations, such as a failed request that also contains successful data.&lt;/p&gt;

&lt;p&gt;A discriminated union can model the valid states more clearly.&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;ApiState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;message&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can write:&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="nf"&gt;handleState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ApiState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please wait&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&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;TypeScript knows which properties are available in each branch.&lt;/p&gt;

&lt;p&gt;You cannot accidentally access &lt;code&gt;data&lt;/code&gt; on the loading state without checking which state you're dealing with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why this is useful:&lt;/strong&gt; API responses, authentication flows, payment processing, and background jobs.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Use &lt;code&gt;never&lt;/code&gt; to Catch Missing Cases
&lt;/h2&gt;

&lt;p&gt;Suppose you handle every order status in a switch statement.&lt;/p&gt;

&lt;p&gt;Later, someone adds another status, but forgets to update your function.&lt;/p&gt;

&lt;p&gt;You can make TypeScript catch that mistake.&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;OrderStatus&lt;/span&gt; &lt;span class="o"&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;pending&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;shipped&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;delivered&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;assertNever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Unexpected value: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;value&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;OrderStatus&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="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pending&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Order is pending&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shipped&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Order has shipped&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;delivered&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Order delivered&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;assertNever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&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;Now add &lt;code&gt;"cancelled"&lt;/code&gt; to &lt;code&gt;OrderStatus&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;TypeScript reports an error at &lt;code&gt;assertNever(status)&lt;/code&gt; because &lt;code&gt;status&lt;/code&gt; can now be &lt;code&gt;"cancelled"&lt;/code&gt; instead of being narrowed to &lt;code&gt;never&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You are forced to handle the new case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why this is useful:&lt;/strong&gt; Business workflows, payment states, event handlers, and large applications where missing a case can cause bugs.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Build Types From Strings Using Template Literal Types
&lt;/h2&gt;

&lt;p&gt;TypeScript can generate new string types from existing ones.&lt;/p&gt;

&lt;p&gt;For example, imagine you have a list of application events.&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;EventName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&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;order&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;payment&lt;/span&gt;&lt;span class="dl"&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;EventHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`on&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;Capitalize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;EventName&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting type becomes:&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;EventHandler&lt;/span&gt; &lt;span class="o"&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;onUser&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;onOrder&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;onPayment&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even create event names dynamically at the type level:&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;Action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;create&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;delete&lt;/span&gt;&lt;span class="dl"&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;Entity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&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;order&lt;/span&gt;&lt;span class="dl"&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;Permission&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;Action&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="nx"&gt;Entity&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This generates:&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;Permission&lt;/span&gt; &lt;span class="o"&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;create:user&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;create:order&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;delete:user&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;delete:order&lt;/span&gt;&lt;span class="dl"&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;strong&gt;Why this is useful:&lt;/strong&gt; Permission systems, event naming conventions, typed API endpoints, and reusable libraries.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Use &lt;code&gt;keyof&lt;/code&gt; to Build Safer Generic Functions
&lt;/h2&gt;

&lt;p&gt;Suppose you want a function that retrieves a property from an object.&lt;/p&gt;

&lt;p&gt;A naive implementation might accept any string as the property name, which can lead to mistakes.&lt;/p&gt;

&lt;p&gt;Here's a safer version:&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="nf"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;active&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&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;active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeScript infers &lt;code&gt;name&lt;/code&gt; as &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;active&lt;/code&gt; as &lt;code&gt;boolean&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But this fails:&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="nf"&gt;getProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// TypeScript error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;keyof T&lt;/code&gt; produces the valid keys of an object type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;K extends keyof T&lt;/code&gt; restricts the key argument.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;T[K]&lt;/code&gt; represents the type of the selected property.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why this is useful:&lt;/strong&gt; Reusable utility functions, form libraries, state management, and object mapping.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Use Recursive Conditional Types to Transform Types
&lt;/h2&gt;

&lt;p&gt;This one is more advanced.&lt;/p&gt;

&lt;p&gt;TypeScript can define types that refer back to themselves, allowing recursive transformations.&lt;/p&gt;

&lt;p&gt;For example, you can create a type that makes every nested object property readonly.&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;DeepReadonly&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;extends &lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;unknown&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="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;DeepReadonly&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;
        &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;DeepReadonly&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now consider:&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;Settings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;theme&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;notifications&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;email&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="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;ReadonlySettings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DeepReadonly&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Settings&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Properties such as &lt;code&gt;app.theme&lt;/code&gt; and &lt;code&gt;app.notifications.email&lt;/code&gt; become readonly at the type level.&lt;/p&gt;

&lt;p&gt;This means TypeScript will reject assignments to those properties.&lt;/p&gt;

&lt;p&gt;A couple of important details:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is a compile-time transformation, not runtime freezing.&lt;/li&gt;
&lt;li&gt;Recursive types can become expensive or difficult for the compiler to evaluate when used with very complex structures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why this is useful:&lt;/strong&gt; Configuration models, immutable state, and advanced type utilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: TypeScript Types Don't Validate Runtime Data
&lt;/h2&gt;

&lt;p&gt;This is an important distinction that even experienced developers sometimes overlook.&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;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&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;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{"id":"wrong","name":123}&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="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeScript accepts the assertion, but the actual object still contains a string for &lt;code&gt;id&lt;/code&gt; and a number for &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why? Because &lt;code&gt;as User&lt;/code&gt; tells the compiler to treat the value as that type. It doesn't validate the object.&lt;/p&gt;

&lt;p&gt;When dealing with external data, use runtime validation with a schema or explicit checks.&lt;/p&gt;

&lt;p&gt;Types make development safer, but they don't replace runtime validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;TypeScript becomes much more interesting when you move beyond basic types and interfaces.&lt;/p&gt;

&lt;p&gt;Features like &lt;code&gt;satisfies&lt;/code&gt;, &lt;code&gt;keyof&lt;/code&gt;, template literal types, discriminated unions, and recursive conditional types allow you to catch mistakes and express complex rules directly in your code.&lt;/p&gt;

&lt;p&gt;My suggestion: don't use advanced types just because you can. Use them when they make your code safer, clearer, and easier to maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which TypeScript trick do you use most in your projects? Did I miss one worth sharing?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's discuss in the comments.&lt;/p&gt;




</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>career</category>
    </item>
    <item>
      <title># TypeScript Made Simple: 7 Concepts Every Developer Should Know</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Mon, 12 Oct 2026 16:30:00 +0000</pubDate>
      <link>https://dev.to/shubhamb/-typescript-made-simple-7-concepts-every-developer-should-know-2cc5</link>
      <guid>https://dev.to/shubhamb/-typescript-made-simple-7-concepts-every-developer-should-know-2cc5</guid>
      <description>&lt;p&gt;If you have worked with JavaScript, you have probably faced situations where your code looked perfectly fine but failed because of a small mistake.&lt;/p&gt;

&lt;p&gt;Maybe you passed a string instead of a number. Maybe an API returned something you were not expecting. Or maybe you spent 20 minutes debugging a property that did not exist.&lt;/p&gt;

&lt;p&gt;This is one of the reasons I enjoy working with TypeScript.&lt;/p&gt;

&lt;p&gt;TypeScript does not magically eliminate bugs, but it helps us catch many common mistakes before our code even runs.&lt;/p&gt;

&lt;p&gt;Let's understand seven useful TypeScript concepts with simple examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Type: Tell TypeScript What You Expect
&lt;/h2&gt;

&lt;p&gt;Imagine you are building an application where every user has a name and an age.&lt;/p&gt;

&lt;p&gt;In JavaScript, you can accidentally assign the wrong value without getting an immediate warning.&lt;/p&gt;

&lt;p&gt;TypeScript helps you define what values are expected.&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;let&lt;/span&gt; &lt;span class="nx"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twenty-five&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// TypeScript error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of types as basic rules for your data.&lt;/p&gt;

&lt;p&gt;They help you catch mistakes while writing code instead of discovering them later.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Interface: Define the Shape of an Object
&lt;/h2&gt;

&lt;p&gt;Suppose your application receives user information from an API.&lt;/p&gt;

&lt;p&gt;Instead of guessing which properties the user object should contain, you can define its structure.&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;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&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;email&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;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;alex@example.com&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now TypeScript can warn you if you forget a required property or provide an incorrect type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In simple words:&lt;/strong&gt; An interface describes what an object should look like.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Type vs Interface: Are They the Same?
&lt;/h2&gt;

&lt;p&gt;This is one of the most common questions developers ask.&lt;/p&gt;

&lt;p&gt;Both can describe an object's structure.&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;interface&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&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;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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;Customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&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;email&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, why have both?&lt;/p&gt;

&lt;p&gt;The main difference becomes clearer when you need more advanced features.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;type&lt;/code&gt; can represent a union of different values:&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;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pending&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;success&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;failed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An interface is often a natural choice for object contracts, while a type alias is particularly useful for unions, intersections, and other type combinations.&lt;/p&gt;

&lt;p&gt;There is some overlap, so you do not need to overthink the choice for every object.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Union Types: Allow More Than One Possibility
&lt;/h2&gt;

&lt;p&gt;Imagine an API that returns a user ID as either a number or a string.&lt;/p&gt;

&lt;p&gt;You can represent both possibilities with a union type.&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;let&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;userId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;userId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;USER-101&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;|&lt;/code&gt; symbol means the value can be one type or another.&lt;/p&gt;

&lt;p&gt;You can also use this for application states:&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;OrderStatus&lt;/span&gt; &lt;span class="o"&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;pending&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;shipped&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;delivered&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;cancelled&lt;/span&gt;&lt;span class="dl"&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 is much safer than allowing any random string.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;"shiped"&lt;/code&gt; would be rejected because it is not one of the allowed values.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Generics: Write Code That Works With Different Types
&lt;/h2&gt;

&lt;p&gt;Suppose you need a function that returns whatever value you pass to it.&lt;/p&gt;

&lt;p&gt;You could write separate functions for strings, numbers, and objects. But that would create unnecessary repetition.&lt;/p&gt;

&lt;p&gt;Generics solve this problem.&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="nf"&gt;getValue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alex&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;T&lt;/code&gt; is a placeholder for a type.&lt;/p&gt;

&lt;p&gt;TypeScript can infer that &lt;code&gt;name&lt;/code&gt; is a string and &lt;code&gt;age&lt;/code&gt; is a number.&lt;/p&gt;

&lt;p&gt;You get reusable code without losing type information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Think of generics as a way to make one function work with different data types while still respecting their types.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Utility Types: Reuse Existing Types
&lt;/h2&gt;

&lt;p&gt;Sometimes you already have a type but need a slightly different version of it.&lt;/p&gt;

&lt;p&gt;For example, you have a user model but want to create a user without requiring every property.&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;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&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;email&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;CreateUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;CreateUser&lt;/code&gt; contains only &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here are a few useful utility types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Partial&amp;lt;T&amp;gt;&lt;/code&gt; — makes all properties optional.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt; — selects specific properties.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; — removes specific properties.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Required&amp;lt;T&amp;gt;&lt;/code&gt; — makes all properties required.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Readonly&amp;lt;T&amp;gt;&lt;/code&gt; — makes properties read-only at the type level.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These become especially useful when working with API requests, database models, and update operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Type Narrowing: Check Before You Use a Value
&lt;/h2&gt;

&lt;p&gt;Imagine a function accepts either a string or a number.&lt;/p&gt;

&lt;p&gt;You cannot safely call string-specific methods until you know that the value is actually a string.&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="nf"&gt;printValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kr"&gt;number&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&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="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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&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="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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&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;TypeScript understands the condition and narrows the type inside each branch.&lt;/p&gt;

&lt;p&gt;This makes your code safer and easier to understand.&lt;/p&gt;

&lt;p&gt;You can also narrow types using checks such as &lt;code&gt;in&lt;/code&gt;, equality comparisons, and custom type guards.&lt;/p&gt;

&lt;h2&gt;
  
  
  One Important Thing to Remember
&lt;/h2&gt;

&lt;p&gt;TypeScript checks types during development and compilation, but its types do not automatically validate incoming data at runtime.&lt;/p&gt;

&lt;p&gt;For example, an API response can still contain unexpected data even if you have defined an interface for it.&lt;/p&gt;

&lt;p&gt;For external data, use runtime validation when necessary.&lt;/p&gt;

&lt;p&gt;TypeScript helps you write safer code, but it is not a replacement for proper validation and testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;You do not need to master every advanced TypeScript feature to start using it effectively.&lt;/p&gt;

&lt;p&gt;Start with basic types, interfaces, union types, and type narrowing. Once you are comfortable with those, explore generics and utility types.&lt;/p&gt;

&lt;p&gt;The goal is not to write complicated types everywhere. The goal is to make your code easier to understand, maintain, and debug.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What TypeScript concept took you the longest to understand?&lt;/strong&gt; Share your experience in the comments. It might help another developer who is learning the same thing.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Tue, 28 Oct 2025 11:15:12 +0000</pubDate>
      <link>https://dev.to/shubhamb/-1lc0</link>
      <guid>https://dev.to/shubhamb/-1lc0</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/shubhamb/-20bc" class="crayons-story__hidden-navigation-link"&gt;𝐒𝐨𝐥𝐯𝐢𝐧𝐠 𝐂𝐨𝐥𝐝 𝐒𝐭𝐚𝐫𝐭𝐬 𝐢𝐧 𝐀𝐖𝐒 𝐒𝐞𝐫𝐯𝐞𝐫𝐥𝐞𝐬𝐬 (𝐋𝐚𝐦𝐛𝐝𝐚)&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/shubhamb" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F304901%2F818ed5b9-2e78-4d9b-852a-eb14da4f47f0.jpeg" alt="shubhamb profile" class="crayons-avatar__image" width="800" height="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/shubhamb" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Shubham_Baghel
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Shubham_Baghel
                
                
              
              &lt;div id="story-author-preview-content-2968083" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/shubhamb" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F304901%2F818ed5b9-2e78-4d9b-852a-eb14da4f47f0.jpeg" class="crayons-avatar__image" alt="" width="800" height="800"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Shubham_Baghel&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/shubhamb/-20bc" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Oct 28 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/shubhamb/-20bc" id="article-link-2968083"&gt;
          𝐒𝐨𝐥𝐯𝐢𝐧𝐠 𝐂𝐨𝐥𝐝 𝐒𝐭𝐚𝐫𝐭𝐬 𝐢𝐧 𝐀𝐖𝐒 𝐒𝐞𝐫𝐯𝐞𝐫𝐥𝐞𝐬𝐬 (𝐋𝐚𝐦𝐛𝐝𝐚)
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/aws"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;aws&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/devops"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;devops&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/shubhamb/-20bc" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;1&lt;span class="hidden s:inline"&gt;&amp;nbsp;reaction&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/shubhamb/-20bc#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            2 min read
          &lt;/small&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>programming</category>
      <category>aws</category>
      <category>devops</category>
    </item>
    <item>
      <title>𝐒𝐨𝐥𝐯𝐢𝐧𝐠 𝐂𝐨𝐥𝐝 𝐒𝐭𝐚𝐫𝐭𝐬 𝐢𝐧 𝐀𝐖𝐒 𝐒𝐞𝐫𝐯𝐞𝐫𝐥𝐞𝐬𝐬 (𝐋𝐚𝐦𝐛𝐝𝐚)</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Tue, 28 Oct 2025 09:25:21 +0000</pubDate>
      <link>https://dev.to/shubhamb/-20bc</link>
      <guid>https://dev.to/shubhamb/-20bc</guid>
      <description>&lt;p&gt;One of the most common performance challenge in serverless app is the Lambda cold start that small delay when AWS spins up a fresh execution environment after inactivity.&lt;/p&gt;

&lt;p&gt;Some easy tricks to make Lambda functions start faster in production:&lt;/p&gt;

&lt;h3&gt;
  
  
  1.𝐔𝐬𝐞 𝐏𝐫𝐨𝐯𝐢𝐬𝐢𝐨𝐧𝐞𝐝 𝐂𝐨𝐧𝐜𝐮𝐫𝐫𝐞𝐧𝐜𝐲
&lt;/h3&gt;

&lt;p&gt;Keeps a set number of Lambda instances always warm.&lt;/p&gt;

&lt;p&gt;AWS pre-initialise execution environments for you so no first-call delay.&lt;/p&gt;

&lt;p&gt;Configure via Console, CLI, or CDK:&lt;br&gt;
AWS Docs: &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/provisioned-concurrency.html" rel="noopener noreferrer"&gt;AWS provisioned concurrency&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: Perfect for APIs or real-time workloads (e.g., login or payment endpoints).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2.𝐊𝐞𝐞𝐩 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬 𝐖𝐚𝐫𝐦 𝐰𝐢𝐭𝐡 𝐚 𝐒𝐜𝐡𝐞𝐝𝐮𝐥𝐞𝐫
&lt;/h3&gt;

&lt;p&gt;Trigger your function periodically (every 5–10 mins) to prevent it from going cold.&lt;/p&gt;

&lt;p&gt;Use Amazon EventBridge or CloudWatch Schedule for simple pinging.&lt;/p&gt;

&lt;p&gt;EventBridge Docs:&lt;a href="https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html" rel="noopener noreferrer"&gt;EventBridge&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: Combine this with a lightweight health-check lambda endpoint (like /ping). This is useful in microservices setups for example, after a new deployment, you can quickly test and verify that all functions are warm and responding correctly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  3.𝐎𝐩𝐭𝐢𝐦𝐢𝐳𝐞 𝐃𝐞𝐩𝐥𝐨𝐲𝐦𝐞𝐧𝐭 𝐏𝐚𝐜𝐤𝐚𝐠𝐞 𝐒𝐢𝐳𝐞
&lt;/h3&gt;

&lt;p&gt;The bigger your Lambda zip, the longer AWS takes to initialize it.&lt;/p&gt;

&lt;p&gt;Bundle only what’s needed (exclude dev dependencies).&lt;/p&gt;

&lt;p&gt;Use tools like esbuild, webpack, or AWS SAM build.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: For Node.js avoid installing large libraries like aws-sdk (it’s pre-included) and remove unused packages with library called depcheck.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  4.𝐂𝐡𝐨𝐨𝐬𝐞 𝐭𝐡𝐞 𝐑𝐢𝐠𝐡𝐭 𝐑𝐮𝐧𝐭𝐢𝐦𝐞
&lt;/h3&gt;

&lt;p&gt;Cold start time depends heavily on the runtime.&lt;/p&gt;

&lt;p&gt;Node.js and Python start up faster than Java or .NET.&lt;/p&gt;

&lt;p&gt;AWS SnapStart Doc:&lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/snapstart.html" rel="noopener noreferrer"&gt;AWS SnapStart&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: If you need Java, enable AWS Lambda SnapStart&lt;br&gt;
 to pre-initialise the function state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  5.𝐁𝐞 𝐂𝐚𝐫𝐞𝐟𝐮𝐥 𝐰𝐢𝐭𝐡 𝐕𝐏𝐂 𝐂𝐨𝐧𝐟𝐢𝐠𝐮𝐫𝐚𝐭𝐢𝐨𝐧
&lt;/h3&gt;

&lt;p&gt;Putting Lambdas inside a VPC adds ENI (Elastic Network Interface) setup time.&lt;/p&gt;

&lt;p&gt;Avoid VPC unless you truly need private resources (like RDS).&lt;/p&gt;

&lt;p&gt;If required, use VPC endpoints or NAT optimization.&lt;br&gt;
Best Practices for VPC Lambda Networking: &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html" rel="noopener noreferrer"&gt;VPC configuration&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: For DynamoDB, S3, or SNS we can go without a VPC entirely.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  6.𝐒𝐩𝐥𝐢𝐭 𝐋𝐚𝐫𝐠𝐞 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬
&lt;/h3&gt;

&lt;p&gt;Monolithic Lambdas = longer cold starts.&lt;/p&gt;

&lt;p&gt;Break down your logic into multiple smaller Lambdas (micro-Lambdas).&lt;/p&gt;

&lt;p&gt;Tip: This also improves deploy speed and observability.&lt;/p&gt;

&lt;p&gt;In one project, our first API call took ~2.4s (cold Lambda in VPC).&lt;br&gt;
After making a few changes like&lt;br&gt;
 • Removed VPC dependency&lt;br&gt;
 • Enabled provisioned concurrency (2 instances)&lt;br&gt;
 • Added an EventBridge warm-up every 10 mins&lt;/p&gt;

&lt;p&gt;Cold start dropped to ~150ms consistently.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>aws</category>
      <category>devops</category>
    </item>
    <item>
      <title>Best way to deploy Nodejs app</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Thu, 27 Mar 2025 15:56:49 +0000</pubDate>
      <link>https://dev.to/shubhamb/best-way-to-deploy-nodejs-app-35fk</link>
      <guid>https://dev.to/shubhamb/best-way-to-deploy-nodejs-app-35fk</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/shubhamb/deploying-a-nodejs-application-using-docker-on-aws-ec2-kubernetes-eks-1fa3" class="crayons-story__hidden-navigation-link"&gt;Deploying a NodeJS Application using Docker on AWS EC2 &amp;amp; Kubernetes (EKS)&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/shubhamb" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F304901%2F818ed5b9-2e78-4d9b-852a-eb14da4f47f0.jpeg" alt="shubhamb profile" class="crayons-avatar__image" width="800" height="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/shubhamb" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Shubham_Baghel
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Shubham_Baghel
                
                
              
              &lt;div id="story-author-preview-content-2352846" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/shubhamb" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F304901%2F818ed5b9-2e78-4d9b-852a-eb14da4f47f0.jpeg" class="crayons-avatar__image" alt="" width="800" height="800"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Shubham_Baghel&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/shubhamb/deploying-a-nodejs-application-using-docker-on-aws-ec2-kubernetes-eks-1fa3" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Mar 27 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/shubhamb/deploying-a-nodejs-application-using-docker-on-aws-ec2-kubernetes-eks-1fa3" id="article-link-2352846"&gt;
          Deploying a NodeJS Application using Docker on AWS EC2 &amp;amp; Kubernetes (EKS)
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/aws"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;aws&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/docker"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;docker&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
            &lt;a href="https://dev.to/shubhamb/deploying-a-nodejs-application-using-docker-on-aws-ec2-kubernetes-eks-1fa3#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            3 min read
          &lt;/small&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>aws</category>
      <category>docker</category>
    </item>
    <item>
      <title>Deploying a NodeJS Application using Docker on AWS EC2 &amp; Kubernetes (EKS)</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Thu, 27 Mar 2025 15:44:45 +0000</pubDate>
      <link>https://dev.to/shubhamb/deploying-a-nodejs-application-using-docker-on-aws-ec2-kubernetes-eks-1fa3</link>
      <guid>https://dev.to/shubhamb/deploying-a-nodejs-application-using-docker-on-aws-ec2-kubernetes-eks-1fa3</guid>
      <description>&lt;p&gt;Deploying a Node.js application using Docker, Kubernetes, and AWS is a multi-step process that requires building the Docker image, configuring Kubernetes for deployment, and setting up AWS services. Below is an process and commands involved&lt;/p&gt;

&lt;p&gt;I'll walk through the steps of deploying a simple Node.js application using Docker, Kubernetes, and AWS.&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.amazonaws.com%2Fuploads%2Farticles%2Fxi5qje9bgchd7tvyo27b.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.amazonaws.com%2Fuploads%2Farticles%2Fxi5qje9bgchd7tvyo27b.png" alt=" " width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 1: Set Up Node.js Application&lt;br&gt;
First, create a simple Node.js application. For this example, we'll use a basic Express server.&lt;/p&gt;

&lt;p&gt;Create a server.js file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// server.js
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) =&amp;gt; {
  res.send('First Service from Node.js!');
});

app.listen(port, () =&amp;gt; {
  console.log(`Server is running on port ${port}`);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Dockerize the Node.js Application&lt;br&gt;
Create a Dockerfile:&lt;/p&gt;

&lt;p&gt;The Dockerfile defines the environment to run your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Step 1: Use the official Node.js image
FROM node:14

# Step 2: Set the working directory inside the container
WORKDIR /usr/src/app

# Step 3: Copy the application code into the container
COPY . .

# Step 4: Install dependencies
RUN npm install

# Step 5: Expose the application port
EXPOSE 3000

# Step 6: Run the application
CMD ["npm", "start"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Build the Docker image using below command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build -t node-docker-app .&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -p 3000:3000 node-docker-app&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Now we can check &lt;br&gt;
our application should now be accessible at &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Step 3: Push Docker Image to AWS ECR (Elastic Container Registry)&lt;br&gt;
First, create a repository in ECR on AWS Console:&lt;/p&gt;

&lt;p&gt;Go to the ECR section of AWS.&lt;/p&gt;

&lt;p&gt;Create a new repository, e.g., node-docker-app.&lt;/p&gt;

&lt;p&gt;Then, authenticate Docker with AWS ECR:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-west-2.amazonaws.com&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Reference Link: link:&lt;a href="https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tag the Docker image:&lt;br&gt;
&lt;code&gt;docker tag node-docker-app:latest &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-west-2.amazonaws.com/node-docker-app:latest&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Push the Docker image to ECR:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker push &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-west-2.amazonaws.com/node-docker-app:latest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step 4: Create Kubernetes Deployment Configuration&lt;br&gt;
Create a Kubernetes deployment YAML file to manage your Dockerized app.&lt;/p&gt;

&lt;p&gt;Create a deployment.yaml file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: node-docker-app-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: node-docker-app
  template:
    metadata:
      labels:
        app: node-docker-app
    spec:
      containers:
      - name: node-docker-app
        image: &amp;lt;aws_account_id&amp;gt;.dkr.ecr.us-west-2.amazonaws.com/node-docker-app:latest
        ports:
        - containerPort: 3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a Kubernetes service YAML file:&lt;/p&gt;

&lt;p&gt;This exposes your application to the outside world.&lt;/p&gt;

&lt;p&gt;Create a service.yaml file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
kind: Service
metadata:
  name: node-docker-app-service
spec:
  selector:
    app: node-docker-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 5: Set Up Kubernetes on AWS (EKS)&lt;br&gt;
Install kubectl and Install eksctl (for creating an EKS cluster)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew install kubectl&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew install eksctl&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create an EKS cluster:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;eksctl create cluster --name nodejs-cluster --region us-west-2 --nodegroup-name nodegroup --node-type t2.micro --nodes 2&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Configure kubectl to use your EKS cluster:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws eks --region us-west-2 update-kubeconfig --name nodejs-cluster&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step 6: Deploy the Application to Kubernetes&lt;br&gt;
Apply the deployment and service files:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kubectl apply -f deployment.yaml&lt;br&gt;
kubectl apply -f service.yaml&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Check the status of the deployment:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kubectl get deployments&lt;br&gt;
kubectl get pods&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Get the LoadBalancer IP:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kubectl get svc node-docker-app-service&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The external IP address will be listed under the EXTERNAL-IP column. You can now access the application via the LoadBalancer's IP.&lt;/p&gt;

&lt;p&gt;Step 7: Clean Up&lt;br&gt;
When you're done, you can delete the Kubernetes cluster on AWS to avoid any ongoing charges:&lt;/p&gt;

&lt;p&gt;`eksctl delete cluster --name nodejs-cluster --region us-west-2&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>aws</category>
      <category>docker</category>
    </item>
    <item>
      <title>How Lambda will enhance your app scalability</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Sun, 07 May 2023 15:06:22 +0000</pubDate>
      <link>https://dev.to/shubhamb/how-lambda-will-enhance-your-app-scalability-m40</link>
      <guid>https://dev.to/shubhamb/how-lambda-will-enhance-your-app-scalability-m40</guid>
      <description>&lt;p&gt;Suppose you have an e-commerce site and you need to tune the consumer's behavior for the duration of their visit. You want to recognize how a lot time the purchaser spends on each web page and which pages they go to in what order. This information can help you examine the consumer's hobbies and possibilities and enhance their universal revel in on the website.&lt;/p&gt;

&lt;p&gt;One way to reap that is by means of developing a Lambda feature that is caused every time a user visits a web page on your internet site. The function can record the user's consultation facts in a database or file. Here's a pattern code for the Lambda characteristic:&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.amazonaws.com%2Fuploads%2Farticles%2F3p9qohg2lyq02911hx7c.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.amazonaws.com%2Fuploads%2Farticles%2F3p9qohg2lyq02911hx7c.png" alt="AWS lambda" width="800" height="912"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the Lambda characteristic receives an occasion object that consists of the pageId and sessionId. The pageId is the identifier for the web page that the person is presently visiting, and the sessionId is a unique identifier for the consumer's consultation.&lt;/p&gt;

&lt;p&gt;The feature then generates a timestamp and creates an item that includes the sessionId, pageId, and timestamp. It makes use of the AWS SDK to put in writing this object to a DynamoDB table named web page-visit-tracking. Finally, it logs a message indicating that the page visit has been recorded.&lt;/p&gt;

&lt;p&gt;This Lambda function can be incorporated together with your internet site's frontend code to ship an occasion item to the Lambda function each time a consumer visits a web page. By doing so, you may music the person's conduct and benefit precious insights into their pursuits and preferences.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>aws</category>
    </item>
    <item>
      <title>Redis with Nodejs</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Mon, 27 Mar 2023 09:38:36 +0000</pubDate>
      <link>https://dev.to/shubhamb/redis-with-nodejs-dg6</link>
      <guid>https://dev.to/shubhamb/redis-with-nodejs-dg6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Redis is an in-memory data store that can be used as a database, cache and message broker. The main benefits of using Redis is its speed. Because it's an in-memory database, it can read and write data very quickly with feature like pipeline and transactions, making it a great choice for applications that need to process large volumes of data in real-time. Redis also supports a variety of data structures, including strings, hashes, lists, sets, and sorted sets. This makes it very versatile and useful for a wide range of applications.&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.amazonaws.com%2Fuploads%2Farticles%2Fk0fr013h9zcn0xx5h23d.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.amazonaws.com%2Fuploads%2Farticles%2Fk0fr013h9zcn0xx5h23d.png" alt="Redis" width="389" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use Redis with node.js,you will need to install "redis" package,We can installed it using below command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install redis&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;After this lets create Redis client using &lt;code&gt;redis.createclient&lt;/code&gt;.This will create a new client that connects to default redis server on &lt;code&gt;localhost:6379&lt;/code&gt;.Also we can pass configuration objectin createclient method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const client = redis.createClient({
    socket: {
        host: '&amp;lt;hostname&amp;gt;',
        port: '&amp;lt;port&amp;gt;'
    },
    password: '&amp;lt;password&amp;gt;'
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Installation on windows:&lt;br&gt;
&lt;a href="https://redis.io/docs/getting-started/installation/install-redis-on-windows/" rel="noopener noreferrer"&gt;Redis installation step for windows&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://redis.io/docs/getting-started/installation/install-redis-on-mac-os/" rel="noopener noreferrer"&gt;Redis installation step for macOs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now lets see how we can use &lt;code&gt;client.set()&lt;/code&gt; to set a key-value pair in redis,The first argument is key which will be any filed that we need to store and will be consider as KEY and second argument here is actual value and third is callback function that gets executed or called once the execution is complete.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Set a key-value pair in Redis
client.set('name', 'devtoUser', (error, result) =&amp;gt; {
  if (error) {
    console.error(error);
  } else {
    console.log(result); // this will return OK
  }
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we use client.get() to get the value of the namekey from Redis.The first argument is the key, and the second argument is a callback function that gets called once the operation is complete. In this example, we log the result to the console if there are no errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Get a value from Redis
client.get('name', (error, result) =&amp;gt; {
  if (error) {
    console.error(error);
  } else {
    console.log(result); //this will return devtoUser
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Redis benefits
&lt;/h2&gt;

&lt;p&gt;Redis also has a number of advanced features that make it a powerful tool for developers. Some of these features include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Pub/Sub messaging: Redis can be used as a message broker, allowing applications to publish and subscribe to messages. This makes it useful for building real-time applications, such as chat apps or real-time analytics systems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lua scripting: Redis has a built-in Lua interpreter, which allows developers to write custom scripts that can be executed on the server. This can be useful for complex data operations that can't be easily performed using built-in Redis commands.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transactions: Redis supports transactions, allowing developers to group multiple commands together into a single atomic operation. This can help ensure data consistency and prevent race conditions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Persistence: Redis can be configured to periodically save its data to disk, ensuring that data is not lost if the server crashes or is restarted.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Github Repo:&lt;br&gt;
&lt;a href="https://github.com/shubhambaghel34/Redis-Examples" rel="noopener noreferrer"&gt;Redis repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additional Radis commands :&lt;br&gt;
&lt;a href="https://redis.io/commands/" rel="noopener noreferrer"&gt;Redis commands list&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>useCallback vs useMemo</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Mon, 06 Mar 2023 14:33:12 +0000</pubDate>
      <link>https://dev.to/shubhamb/usecallback-vs-usememo-47h8</link>
      <guid>https://dev.to/shubhamb/usecallback-vs-usememo-47h8</guid>
      <description>&lt;p&gt;&lt;strong&gt;useCallback&lt;/strong&gt; and &lt;strong&gt;useMemo&lt;/strong&gt; are both React hooks that can be used to optimize the performance of your React application by remembering calculated values ​​in components but there is a slight difference in terms of Return Value, Dependency array. Below is good comparison of both.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: The main use case of useCallback is to remember a function, while the main use case of useMemo is to remember a value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Return value&lt;/strong&gt;: useCallback returns a memorized function, while useMemo returns a memorized value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependencies Array&lt;/strong&gt;: &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;useCallback takes a dependency array as the second argument, which specifies the values ​​on which the stored function depends. The function will only be recalculated if any value in the dependencies array changes. On the other hand, &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useMemo also takes an array of dependencies as the second argument, but it specifies the values ​​on which the remembered value depends. The value will only be recalculated if any value in the dependencies array changes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Performance Considerations&lt;/strong&gt;: If you have a computationally expensive function that you need to pass as a prop to a child component, you should use useCallback to remember that function. This will prevent the child component from rendering unnecessarily. If you have a computationally expensive value that you need to use in a component, you should use useMemo to remember that value. This will prevent the value from being unnecessarily recalculated on every new render.&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.amazonaws.com%2Fuploads%2Farticles%2Fhecgus433x4fp1z88n8j.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.amazonaws.com%2Fuploads%2Farticles%2Fhecgus433x4fp1z88n8j.png" alt="useCallback vs useMemo" width="706" height="616"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we use both useMemo and useCallback to remember a value and a function respectively. Expensive value is remembered by useMemo and expensive function is remembered by useCallback. Both hooks depend on the value of the count state and will only recalculate if the count value changes. This can help optimize component performance.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Why to useMemo hook?</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Sun, 05 Mar 2023 12:09:16 +0000</pubDate>
      <link>https://dev.to/shubhamb/why-to-usememo-hook-415c</link>
      <guid>https://dev.to/shubhamb/why-to-usememo-hook-415c</guid>
      <description>&lt;p&gt;Suppose you have a module that displays a listing of products, each one with its own label and cost. Moreover, you have a separate function that determines the collective price for all the items. This is an illustration of how to employ the useMemo hook to store the result of the total price calculation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useMemo } from 'react';

function ItemList({ items }) {
  const totalPrice = useMemo(() =&amp;gt; {
    console.log('Calculating total price...');
    return items.reduce((acc, item) =&amp;gt; acc + item.price, 0);
  }, [items]);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;ul&amp;gt;
        {items.map((item) =&amp;gt; (
          &amp;lt;li key={item.name}&amp;gt;
            {item.name}: {item.price}
          &amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
      &amp;lt;p&amp;gt;Total price: {totalPrice}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useMemo hook lets us set up a variable, entitled totalPrice, in this instance. The first piece of the hook is a function that works out the cost of all the items; the second part is an array of dependencies, meaning that React will recalculate the price any time the dependencies alter. In this instance, the only thing that the calculation is reliant on is the items prop.&lt;/p&gt;

&lt;p&gt;The useMemo hook memorizes the result of the function, so if the items prop hasn't changed since the last render, React will take the previous result instead of running the function again. This can be helpful for complicated calculations that don't need to be re-run every time the render is executed. For example, if there is a large list of items, figuring out the total cost on every render could be time-consuming; memorizing the result can increase performance.&lt;/p&gt;

&lt;p&gt;It is worth noting that useMemo is not a substitute for useEffect. useMemo should be used to store the outcome of a function, while useEffect should be used for completing side effects like obtaining data from an API.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Write React Hooks More Effectively</title>
      <dc:creator>Shubham_Baghel</dc:creator>
      <pubDate>Sun, 05 Mar 2023 10:30:39 +0000</pubDate>
      <link>https://dev.to/shubhamb/how-to-write-react-hooks-more-effectively-2ale</link>
      <guid>https://dev.to/shubhamb/how-to-write-react-hooks-more-effectively-2ale</guid>
      <description>&lt;p&gt;1.Only call hooks at the top level: Hooks should only be called at the top level of your component or custom hook. Do not call hooks inside loops, conditions, or nested functions&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function MyComponent() {
  const [state, setState] = useState(0);

  function handleClick() {
    setState(state + 1); // This is correct
  }

  return (
    &amp;lt;button onClick={handleClick}&amp;gt;Click me&amp;lt;/button&amp;gt;
  );
}

function MyOtherComponent() {
  const [state, setState] = useState(0);

  function handleClick() {
    if (state &amp;gt; 0) {
      setState(state - 1); // This is correct
    }
  }

  return (
    &amp;lt;button onClick={handleClick}&amp;gt;Click me&amp;lt;/button&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Use the state hook for local state: The state hook is the most commonly used hook and should be used for any local state that your component needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function MyComponent() {
  const [name, setName] = useState("");

  return (
    &amp;lt;&amp;gt;
      &amp;lt;input value={name} onChange={e =&amp;gt; setName(e.target.value)} /&amp;gt;
      &amp;lt;p&amp;gt;Hello, {name}&amp;lt;/p&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Use the effect hook for side effects: The effect hook allows you to synchronize a component with an external system, such as a web API or a browser API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function MyComponent() {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    fetch("https://my-api.com/data")
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setData(data));
  }, []);

  return (
    &amp;lt;ul&amp;gt;
      {data.map(item =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.Avoid unnecessary re-renders: Hooks like useMemo and useCallback can help you avoid unnecessary re-renders by memoizing values and functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function MyComponent({ items }) {
  const sortedItems = useMemo(() =&amp;gt; items.sort(), [items]);

  return (
    &amp;lt;ul&amp;gt;
      {sortedItems.map(item =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.Keep hooks simple: Each hook should only handle one piece of logic, such as setting up a subscription or measuring the size of an element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function useData(url) {
  const [data, setData] = useState([]);

  useEffect(() =&amp;gt; {
    fetch(url)
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setData(data));
  }, [url]);

  return data;
}

function MyComponent() {
  const data = useData("https://my-api.com/data");

  return (
    &amp;lt;ul&amp;gt;
      {data.map(item =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6.Use the context hook to share state: The context hook allows you to share state between components without having to pass props down the component tree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyContext = React.createContext();

function MyProvider({ children }) {
  const [name, setName] = useState("");

  return (
    &amp;lt;MyContext.Provider value={{ name, setName }}&amp;gt;
      {children}
    &amp;lt;/MyContext.Provider&amp;gt;
  );
}

function MyComponent() {
  const { name, setName } = useContext(MyContext);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;input value={name} onChange={e =&amp;gt; setName(e.target.value)} /&amp;gt;
      &amp;lt;p&amp;gt;Hello, {name}&amp;lt;/p&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

function App() {
  return (
    &amp;lt;MyProvider&amp;gt;
      &amp;lt;MyComponent /&amp;gt;
    &amp;lt;/MyProvider&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the MyProvider component creates a context and provides the name and setName state to its children via the value prop. The MyComponent component then uses the useContext hook to access the name and setName state from the context.&lt;/p&gt;

&lt;p&gt;This way, the name state can be accessed and modified by any component that is a child of the MyProvider component, without having to pass props down the component tree.&lt;/p&gt;

&lt;p&gt;7.The handleClick function uses the callback form of the setCount updater, which receives the previous state and returns the new state. This way, the function can update the state based on the previous value, without needing to use a useEffect or useRef to store the previous value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function MyComponent() {
  const [count, setCount] = useState(0);

  const handleIncrement = useCallback(() =&amp;gt; {
    setCount(prevCount =&amp;gt; prevCount + 1);
  }, [setCount]);

  const handleDecrement = useCallback(() =&amp;gt; {
    setCount(prevCount =&amp;gt; prevCount - 1);
  }, [setCount]);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;button onClick={handleIncrement}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={handleDecrement}&amp;gt;Decrement&amp;lt;/button&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach is useful in situations where the component updates frequently, and you want to prevent unnecessary re-renders caused by changing function references. It also allows to memoize the function, which means it will not recreate the function if the dependencies haven't changed.&lt;/p&gt;

&lt;p&gt;8.Use the useEffect hook with a cleanup function: A cleanup function is a function that is executed when a component is unmounted or a hook is re-run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from 'react';

function MyComponent() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    const intervalId = setInterval(() =&amp;gt; {
      setCount(count =&amp;gt; count + 1);
    }, 1000);
    // Cleanup function
    return () =&amp;gt; clearInterval(intervalId);
  }, [setCount]);

  return &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the useEffect hook is used to set up an interval that increments the count state every second. The setInterval function returns an interval ID, which is stored in a variable called intervalId.&lt;/p&gt;

&lt;p&gt;The useEffect hook also takes a function as a second argument that is used as a cleanup function. It is executed when the component is unmounted or the hook is re-run. In this case, the cleanup function is used to clear the interval by passing in the intervalId variable to the clearInterval function.&lt;/p&gt;

&lt;p&gt;This way, we can ensure that the interval is properly cleared when the component is no longer in use, and avoid memory leaks.&lt;/p&gt;

&lt;p&gt;Cleanup functions are a powerful feature of the useEffect hook, they allow you to perform a cleanup operation when a component is unmounted or when a hook is re-run. This makes sure that you don't leave any unnecessary event listeners, timeouts, intervals or other resources that are no longer needed.&lt;/p&gt;

&lt;p&gt;9.Always pass the dependencies array to useEffect: This array should include all values that the effect depends on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from 'react';

function MyComponent({ userId }) {
  const [userData, setUserData] = useState(null);

  useEffect(() =&amp;gt; {
    fetch(`https://jsonplaceholder.typicode.com/users/${userId}`)
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setUserData(data));
  }, [userId, setUserData]);

  return userData ? (
    &amp;lt;&amp;gt;
      &amp;lt;h2&amp;gt;{userData.name}&amp;lt;/h2&amp;gt;
      &amp;lt;p&amp;gt;{userData.email}&amp;lt;/p&amp;gt;
    &amp;lt;/&amp;gt;
  ) : (
    &amp;lt;p&amp;gt;Loading...&amp;lt;/p&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the useEffect hook is used to fetch data from an API based on the userId prop passed to the component. The fetch function is used to make a request to the API, and the then method is used to handle the response and update the userData state.&lt;/p&gt;

&lt;p&gt;The second argument passed to the useEffect hook is an array of dependencies, which includes userId and setUserData. These are the values that the effect depends on, so they should be included in the dependencies array.&lt;/p&gt;

&lt;p&gt;This way, the effect will only re-run when the userId prop or setUserData state change, and it will not re-run when other props or state values change. This can help to optimize the performance of your component and avoid unnecessary re-renders.&lt;/p&gt;

&lt;p&gt;Always passing the dependencies array to the useEffect hook is important because it tells React when the effect should be run again. If you don't specify the dependencies array, React will assume that the effect depends on all the state and props, and will run the effect on every render. This can lead to unexpected behavior and performance issues.&lt;/p&gt;

&lt;p&gt;10.Be careful when using hooks in loops: If you want to use a hook in a loop, make sure that each rendered element has its own state&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from "react";

function MyComponent({ items }) {
  return (
    &amp;lt;ul&amp;gt;
      {items.map((item, index) =&amp;gt; {
        const [isSelected, setIsSelected] = useState(false);
        return (
          &amp;lt;li key={item.id} onClick={() =&amp;gt; setIsSelected(!isSelected)}&amp;gt;
            {item.name} {isSelected ? "Selected" : ""}
          &amp;lt;/li&amp;gt;
        );
      })}
    &amp;lt;/ul&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, My component takes an items prop and maps over it to render a list of items. Each item is rendered as a list item (li) element. Inside the map function, I am using the useState hook to create a isSelected state variable for each item.&lt;/p&gt;

&lt;p&gt;By providing a unique key for each item, React can tell which elements are being added, removed, or re-arranged. This way, React can manage the state of each element separately, and you don't have to worry about the hook state being shared across elements.&lt;/p&gt;

&lt;p&gt;It is important to be careful when using hooks inside loops, because if you don't provide a unique key for each element, React will not be able to tell which elements are being added, removed or re-arranged, and it will reuse the state of the hook across elements, which can lead to unexpected behavior and bugs.&lt;/p&gt;

&lt;p&gt;It's also important to mention that we could use useState inside a callback or inside useEffect instead of directly in the JSX, this way we don't have to worry about different components using the same state.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
    </item>
  </channel>
</rss>
