<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nikhil Agrawal</title>
    <description>The latest articles on Forem by Nikhil Agrawal (@nikhilagr15).</description>
    <link>https://forem.com/nikhilagr15</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1970874%2Fc7692503-2dcd-49ed-9315-dd4526cd54c0.jpg</url>
      <title>Forem: Nikhil Agrawal</title>
      <link>https://forem.com/nikhilagr15</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nikhilagr15"/>
    <language>en</language>
    <item>
      <title>How I Fixed My TypeScript Setup Issue: Property user does not exist on type Request.</title>
      <dc:creator>Nikhil Agrawal</dc:creator>
      <pubDate>Sat, 14 Sep 2024 19:00:28 +0000</pubDate>
      <link>https://forem.com/nikhilagr15/how-i-fixed-my-typescript-setup-issue-property-user-does-not-exist-on-type-request-46fj</link>
      <guid>https://forem.com/nikhilagr15/how-i-fixed-my-typescript-setup-issue-property-user-does-not-exist-on-type-request-46fj</guid>
      <description>&lt;p&gt;The Problem&lt;br&gt;
I’ve been working on a Node.js project with TypeScript and Express.js. At one point, I needed to attach a user object to the Express Request object, but I ran into this TypeScript error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Property 'user' does not exist on type 'Request'.&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I quickly realized that this happens because Express's default Request object doesn't include a user property, and TypeScript wasn’t happy about it.&lt;/p&gt;

&lt;p&gt;My Initial Fix&lt;br&gt;
To fix this, I extended the Request interface to add the user property. Here’s how I did it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I created a new file called express.d.ts in the types folder in my project:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/types/express.d.ts
import { User } from '@prisma/client'; // Assuming User is a Prisma model

declare global {
    namespace Express {
        interface Request {
            user?: User; // Add user to the Request interface
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;I updated my tsconfig.json to ensure TypeScript picked up this new type:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    //extra options here
    "typeRoots": ["./node_modules/@types", "./src/types"] // Add the types folder
  }
}

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

&lt;/div&gt;



&lt;p&gt;At this point, the error disappeared from my code editor, so I thought I had fixed the problem. But when I tried running the project, I hit another error in the terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;error TS2339: Property 'user' does not exist on type 'Request'.&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Stuck for Days&lt;br&gt;
I spent 3-4 days troubleshooting this, trying everything I could find online. I was completely stuck and couldn’t figure out why it wasn’t working.&lt;/p&gt;

&lt;p&gt;The Solution&lt;br&gt;
Finally, I discovered the root issue and fixed it with these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install TypeScript Globally&lt;/strong&gt;: I realized I didn’t have the TypeScript compiler (tsc) installed globally, so I ran this command:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;npm install -g typescript&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Run the TypeScript Compiler in Watch Mode&lt;/strong&gt;: I used the --watch flag to automatically recompile my TypeScript code as I worked:&lt;br&gt;
&lt;code&gt;tsc --watch&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Restart My Code Editor: I restarted my editor (VS Code in my case) to make sure everything was loaded properly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the Project: After restarting, I ran the project again—and it worked!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Conclusion&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
What I thought would be a small issue turned into days of frustration, but I finally got it working! If you're facing similar problems with TypeScript not recognizing new properties on Express’s Request object, remember to:&lt;/p&gt;

&lt;p&gt;Extend the Request interface correctly.&lt;br&gt;
Make sure TypeScript’s compiler (tsc) is installed and running properly.&lt;br&gt;
Hopefully, this helps anyone else who's been stuck like I was!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>programming</category>
      <category>node</category>
    </item>
    <item>
      <title>Try...Catch V/s Safe Assignment (?=): A Boon or a Curse for Modern Development?</title>
      <dc:creator>Nikhil Agrawal</dc:creator>
      <pubDate>Sun, 25 Aug 2024 18:27:57 +0000</pubDate>
      <link>https://forem.com/nikhilagr15/trycatch-vs-safe-assignment-a-boon-or-a-curse-for-modern-development-55dg</link>
      <guid>https://forem.com/nikhilagr15/trycatch-vs-safe-assignment-a-boon-or-a-curse-for-modern-development-55dg</guid>
      <description>&lt;p&gt;Recently, I discovered the new Safe Assignment Operator (?.=) introduced in JavaScript, and I’m really fascinated by its simplicity. 😍&lt;/p&gt;

&lt;p&gt;The Safe Assignment Operator (SAO) is a shorthand alternative to the traditional try...catch block. It lets you catch errors inline without writing explicit error-handling code for each operation. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [error, response] ?= await fetch("https://api.example.com/data");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it! It’s that easy. If the fetch request throws an error, it’s automatically stored in the error constant; otherwise, the response holds the result. Pretty cool, right?&lt;/p&gt;

&lt;p&gt;But wait… there’s more.&lt;/p&gt;

&lt;p&gt;When using SAO, you still have to handle errors further down the line, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getData() {
  const [requestError, response] ?= await fetch("https://api.example.com/data");

  if (requestError) {
    handleRequestError(requestError);
    return;
  }

  const [parseError, json] ?= await response.json();

  if (parseError) {
    handleParseError(parseError);
    return;
  }

  const [validationError, data] ?= validation.parse(json);

  if (validationError) {
    handleValidationError(validationError);
    return;
  }

  return data;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While SAO simplifies error handling, it can lead to more verbose code. Compare that with a traditional try...catch block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getData() {
try {
  const response = await fetch("https://api.example.com/data");
  const json = await response.json();
  const data = validation.parse(json);
  return data;
} catch (error) {
  handleError(error);
  return;
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, try...catch takes only 9 lines of code, while SAO approximately double of it.&lt;/p&gt;

&lt;p&gt;So, what do you think? Is the Safe Assignment Operator a time-saver, or does it add unnecessary complexity?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>trycatch</category>
      <category>thesafeassignmentoperator</category>
    </item>
  </channel>
</rss>
