我在Node.js中使用Typescript。使用Express中间件时,通常会转换Request对象。但是,使用Typescript,我们无法跟踪Request对象的转换方式。如果你知道之前通过的中间件,有没有办法从中找出请求的类型?如果在快递中不可能,我想找到另一个可行的框架。是否可以在Nest(https://github.com/kamilmysliwiec/nest)?
示例代码
import { Request, Response, NextFunction } from 'express';
function userMiddleware(req: Request & User, res: Response, next: NextFunction) {
req.user = {
id: 'user_id',
};
next();
}
interface User {
user: {
id: string;
}
}
interface Middleware {
<T>(req: Request & T, res: Response, next: NextFunction): void;
}
class Controller {
middleware = [userMiddleware];
get = new GetMethod(this.middleware);
post = (req: Request /* I don't know exact req type */, res: Response, next: NextFunction) => {
console.log(req.user) // Error!
}
}
class GetMethod {
constructor(middleware: Middleware[]) {
// How to deduce type of req from Middleware array?
}
}
const controller = new Controller();
express.use('/', controller.middleware, controller.post);
我想从Controller类中的中间件列表中提取类型信息。
答案 0 :(得分:1)
首先,我认为正确的界面是
interface User {
id: string;
}
由于它们是回调,因此将收到默认Request
,其签名中没有user
。
因此,您有2个选项,进行类型断言或编写自定义声明。如果正确执行,两者都很好。
类型断言:
interface User {
id: string;
}
const isObject = (value: unknown): value is {[key: string]: unknown} => {
return value && typeof value === 'object';
};
const isReqWithUser = (req: Request): req is Request & {user: User} => {
return isObject(req) && !!req.user;
}
class Controller {
post = (req: Request, res: Response, next: NextFunction) => {
if (isReqWithUser(req)) {
console.log(req.user) // now it works
}
next();
}
}
自定义声明
但是我们需要了解用户并非总是存在于请求中,我们应该将其标记为可选。
interface User {
id: string;
}
declare module 'express' {
export interface Request {
user?: User; // adding our custom declaration.
}
}
class Controller {
post = (req: Request, res: Response, next: NextFunction) => {
console.log(req.user) // now it works
next();
}
}