扩展请求类型以添加 Redis 客户端



我正在尝试创建这个api.ts文件并将 redis 客户端放在请求上以将其传递给所有路由。但是我收到一个错误,指出请求>上没有客户端。如何扩展请求类型并将其放在 TypeScript 中的请求中?

这是我的文件

import { version } from '../../package.json';
import { Router, Request, Response, NextFunction } from 'express';
export default ({ config, client } : any) => {
let api = Router();
api.use((req: Request, res: Response, next: NextFunction) => {
req.client = client;
next()
})
api.get('/', (_req, res) => {
res.json({ version });
});
return api;
}

试试这个,

req['client'] = client;

最新更新