#Typescript router and controllers

3 messages · Page 1 of 1 (latest)

frigid nacelle
#

Hello,

I havce a typescript nodeJS project and I have can't make my typization right!

Here is my router:

router.get("/locations", authorizedHTTP, deviceController.device_locations);

Here is my controller:

const device_locations = async (
  req: Request & {userId: string},
  res: Response
) => {
  let result: Callback = await locationHandler({
    method: req.method,
    userId: req.userId,
    originalUrl: req.originalUrl,
  });
  res.status(result.status).send({ message: result.message });
};

But I get the following error:

#
No overload matches this call.
  The last overload gave the following error.
    Argument of type '(req: Request & {    userId: string;}, res: Response) => Promise<void>' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
      Type '(req: Request & {    userId: string;}, res: Response) => Promise<void>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
        Types of parameters 'req' and 'req' are incompatible.
          Type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is not assignable to type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>> & { userId: string; }'.
            Property 'userId' is missing in type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' but required in type '{ userId: string; }'.ts(2769)
device.controller.ts(45, 19): 'userId' is declared here.
index.d.ts(153, 5): The last overload is declared here.
#

I know that this is due the fact that express expect express.Request, and I give it express.Request & {userId: string} so I am saying that it must have userId: string, but that it because I know the client will provide it