Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'

Please help, I am getting this error

src/app/middlewares/authentication.ts:16:17 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

16             req.user = user;

I have created the .d.ts file and also included it in tsconfig file. Still I am not able to run this code

Please find attached screenshots

enter image description here

enter image description here

enter image description here

like image 705
Piyush Garg Avatar asked Dec 20 '25 21:12

Piyush Garg


2 Answers

  1. Create a types folder in your src directory
  2. Create a folder within the types folder with the name of the package you intend to extend. (In this case express).
  3. Create an index.d.ts file in that folder
 src/
   - types/
    - express/
     - index.d.ts
  1. add this code to the index file
import express from "express";

declare global {
  namespace Express {
    interface Request {
      user?: Record<string,any>
    }
  }
}
  1. remember to update your tsconfig.json file
{
  "compilerOptions": {
    "typeRoots" : ["./src/types", "./node_modules/@types"]
  }
}
like image 104
Solz Avatar answered Dec 22 '25 18:12

Solz


Just install types for Passport.js:

npm install -D @types/passport
like image 29
glory1 Avatar answered Dec 22 '25 19:12

glory1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!