Skip to content

Argument of type '(req: ProtectedRequest, res: Response<any>, next: NextFunction) => Promise<void>' is not assignable to parameter of type 'AsyncFunction' #20

Open
@vuthehuyht

Description

@vuthehuyht

I customized type of request in app-request file. But when I call type of requests, my typescript get error and I do not know reason. I try to fix but not. Here error:

Argument of type '(req: ProtectedRequest, res: Response<any>, next: NextFunction) => Promise<void>' is not assignable to parameter of type 'AsyncFunction'

signup.ts

import express from 'express';
import { SuccessResponse } from '../../../middleware/ApiResponse';
import UserRepo from '../../../database/repository/UserRepo';
import User from '../../../database/model/User';
import validator from '../../../helpers/validator';
import asyncHandler from '../../../helpers/asyncHandler';
import { BadRequestError } from '../../../middleware/ApiError';
import bcrypt from 'bcrypt';
import schema from './schema';
import _ from 'lodash';
import { PublishRequest } from 'app-request';

const router = express.Router();

router.post(
  '',
  validator(schema.signup),
  asyncHandler(async (req: PublishRequest, res) => {
    const user = await UserRepo.findByEmail(req.body.email);
    if (user) throw new BadRequestError('User already registered');

    const passwordHash = await bcrypt.hash(req.body.password, 10);

    const { user: createdUser } = await UserRepo.create(
      {
        fullname: req.body.fullname,
        username: req.body.username,
        phone: req.body.phone,
        email: req.body.email,
        profilePicUrl: req.body.profilePicUrl,
        password: passwordHash,
      } as User
    );

    new SuccessResponse('Signup Successful', {
      user: _.pick(createdUser, ['_id', 'fullname', 'name', 'phone', 'email', 'profilePicUrl'])
    }).send(res);
  }),
);

export default router;

app-request.d.ts

import { Request } from 'express';
import User from '../database/model/User';
import Keystore from '../database/model/Keystore';

declare interface PublicRequest extends Request {
  apiKey: string;
}

declare interface ProtectedRequest extends Request {
  user: User;
  accessToken: string;
  keystore: Keystore;
}

declare interface Tokens {
  accessToken: string;
  refreshToken: string;
}

Please help me fix it. Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions