Home > Enterprise >  node.js res: Response type is showing error
node.js res: Response type is showing error

Time:09-29

I am trying to get some node.js and typescript working, but i have some basic issue. I have created such routeController

  public allUsers = (req: Request, res: Response) => {
    res.status(500).json({
      status: "ERROR",
      message: "Not implemented",
    });
  };

My res.status(500) is showing me error: This expression is not callable.Type 'Number' has no call signatures.ts(2349)

do i need to define some extra interface for this? or extend something?

CodePudding user response:

It looks you are referencing the wrong Request type. Now that fetch is built in to Node, it's Response would be the default global type which defines status: number;

If you explicitly use the express (or whatever your HTTP framework is) types then you should be good.

import type {Request, Response} from 'express';

public allUsers = (req: Request, res: Response) => {
    res.status(500).json({
      status: "ERROR",
      message: "Not implemented",
    });
  };
  • Related