I've recently started using TypeScript and encountered the following issue:
Element implicitly has an 'any' type because index expression is not of type 'number'
This error message appears on this line -->
const { status, msg } = codes[err.code];
I'm unsure about how to resolve it.
Below is the code snippet in question:
export const handlePSQLErrors = (
err: { status: number; msg: string; code: string | number },
req: Request,
res: Response,
next: NextFunction
) => {
const codes: any = {
'22P02': { status: 400, msg: 'Bad Request' },
42703: { status: 400, msg: 'Bad Request' },
23502: { status: 400, msg: 'Bad Request' },
23503: { status: 404, msg: 'Bad Request' },
};
function hasKey<O>(obj: O, key: keyof any): key is keyof O {
return key in obj;
}
if (hasKey(err.code, codes)) {
const { status, msg } = codes[err.code];
res.status(status).send({ msg });
} else next(err);
};
Any insights on what's causing this issue and how to address it would be greatly appreciated.
Thank you