Search code examples
typescriptexpressnestjs

Property 'cookies' does not exist on type 'Request'


I'm trying to read cookie in a nestjs controller.

i am following the docs at https://docs.nestjs.com/techniques/cookies#use-with-express-default

Here's my code

import { Controller, Get, Render, Req } from '@nestjs/common';

@Controller()
export class AppController {

  @Get()
  @Render('home')
  getHello(@Req() req: Request) {
    return { text: req.cookies['id'] };
  }
}

the problem is that type Request from express does not have cookies. So i get this error.

src/app.controller.ts:11:24 - error TS2339: Property 'cookies' does not exist on type 'Request'.

11     return { text: req.cookies['id'] };
                          ~~~~~~~

The code actually works if i remove type Request from req. But thhen i lost type-safety.


Solution

  • you need to import the type Request from express (so install @types/express). That one you're using is not from it. I'm assuming that you're the default http adapter.