Home > Blockchain >  How to convert Expressjs Arrow Function from JavaScript to TypeScript
How to convert Expressjs Arrow Function from JavaScript to TypeScript

Time:10-29

I am converting a Expressjs project from JavaScript to TypeScript.

The initial JavaScript module catchAsyncError.js:

module.exports = theFunc => (req, res, next) => {
    Promise.resolve(theFunc(req, res, next)).catch(next);
}

I tried converting to TypeScript catchAsyncError.ts:

import { Request, Response, NextFunction} from "express";

const theFunc => (req: Request, res: Response, next: NextFunction) => {
    Promise.resolve(theFunc(req, res, next)).catch(next);
}
export default theFunc;

But there is an Error: Variable 'theFunc' implicitly has an 'any' type.ts(7005)

Can someone please advise and explain? Thanks in advance.

CodePudding user response:

I believe you need to swap a => to a = in your function definition:

//   see here \/
const theFunc = (req: Request, res: Response, next: NextFunction) => {
    Promise.resolve(theFunc(req, res, next)).catch(next);
}

CodePudding user response:

Referring to the link given by cmgchess, Typescript: TS7006: Parameter 'xxx' implicitly has an 'any' type

I managed to covert it:

catchAsyncError.ts

import { Request, Response, NextFunction} from "express";


const catchAsyncError = (theFunc: any) => (req: Request, res: Response, next: NextFunction) => {
    Promise.resolve(theFunc(req, res, next)).catch(next);
}

export default catchAsyncError;

Thank you all for your time!

  • Related