DEV Community

Muhammad Danish
Muhammad Danish

Posted on • Edited on

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

You can avoid directly extending the Request interface and enhance type safety when accessing user data, you can create a custom parameter decorator.

Create user.decorator.ts file in src/common/decorator or at any place and add this code:


import {User} from '../../common/decorators/user.decorator';

@User() user: { userId: string; email: string; roleId: string } //instead of @Req user: Request
Enter fullscreen mode Exit fullscreen mode

more clear example:

 @UseGuards(AuthGuard('jwt'))
  @Get('email')
  getUserEmail(@User() user: { userId: string; email: string; roleId: string }) {
    // your remaining logic 
  }
Enter fullscreen mode Exit fullscreen mode

you can use all the attributes exit in user. Using that fix you don't further need to use Request Type from express and it work in deployment too and provides strong type safety.

Top comments (0)