嘿!
为什么不将请求的正文更改为 T? 这样我就可以确定我的对象包含什么。
在 express-serve-static-core/index.d.ts 第 438 行:
interface Request<T> extends http.IncomingMessage, Express.Request {
...
...
body: T
...
...
我像这样使用它:
updateUser = (req: Request<User>, res: Response, next: NextFunction) => {
const userToUpdate = req.body;
...
...
}
同意,对于想要输入请求正文的人来说,这应该改变,同样应该适用于cookies
, params
, query
, @borisyankov你怎么看?
我不讨厌这个想法,但与此同时,types/express 存在这个问题,这解释了body
被排除在外是有原因的。 原因是body
本身并不存在于 Express Request 对象上,而是由于 body-parser 而被添加。
不幸的是,body-parser 的 DefinitiveTyped 版本不适用于他们提出的解决方法。 所以这里要么需要添加body,要么需要更新DefinitelyTyped/body-parser。
有道理,我也认为有一种方法可以通过使用默认泛型来包含此功能而不会破坏更改,例如:
interfare Request<B = any, Q = any, P = any, C = any> {
body: B;
query: Q;
params: P;
cookies: C;
}
最有用的评论
有道理,我也认为有一种方法可以通过使用默认泛型来包含此功能而不会破坏更改,例如: