Home > database >  Error TS2769 with protect middleware in express.js
Error TS2769 with protect middleware in express.js

Time:10-15

I try to protect routes on Express server.So I simplified my middleware to this yet I still catch an error:

src/routes/userRoutes.ts:10:19 - error TS2769: No overload matches this call.

The last overload gave the following error. Argument of type '(req: express.Request, res: Response, next: express.NextFunction) => Promise' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.Type '(req: express.Request, res: Response, next: express.NextFunction) => Promise' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'. Types of parameters 'res' and 'res' are incompatible. Type 'Response<any, Record<string, any>, number>' is missing the following properties from type 'Response': headers, ok, redirected, statusText, and 8 more.

10 router.get('/me', protect, getMe);

Here is middleware:

import express from "express";
const jwt = require('jsonwebtoken');
const User = require('../model/userModel');

export const protect = async ( req: express.Request, res: Response, next: express.NextFunction) => {
  const { authorization } = req.headers;
  next();
}

Here it is invoked in routers:

import express from "express";
const { getMe } = require('../controller/controller');
export const router = express.Router();
import { protect } from '../middleware/authMiddleWare';

router.get('/me', protect, getMe);

How to get rid of this error?

CodePudding user response:

Try to change the function signature to (you are not retrieving the Response type correctly):

export const protect = async (
  req: express.Request,
  res: express.Response,
  next: express.NextFunction
) => { ... };
  • Related