typescript - Typescript 扩展了 Express Request 正文
问题描述
我有以下内容:
const fff = async (req: express.Request, res: express.Response): Promise<void> => {...}
我如何声明它req.body.xxx
存在?所以当我输入req.body.xxx
它会知道它存在于req.body
对象上?
解决方案
定义您的Request
类型,该类型扩展express.Request
,您可以设置请求正文类型:
interface ILoginBody {
username: string;
password: number;
}
interface ILoginRequest extends Request {
body: ILoginBody;
}
// use ILoginRequest instead of express.Request
const fff = async (req: ILoginRequest, res: Response): Promise<void> => {
const { username, password } = req.body; // body now is an ILoginBody
}
推荐阅读
- java - 安装 JVM 的自动无缝方式
- javascript - 我无法理解一件事
- python - 如何使用驱动程序对象将python selenium中的所有页面
- tensorflow - 如何使用嵌入式 tensorflow 方法来组合多个特征列操作和窗口化,以用于 RNN?
- gitlab - GitLab 是否显示以 base64 给出的图像?
- android - Android 自动备份 - 如何知道上次备份的时间?
- c++ - 指针数组,在 C++ 中删除并分配给它的指针
- apache-spark - Databricks REST API 节流和容量限制/限制
- javascript - 在一个组件中反应清除 LocalStorage 和 Asyncstorage
- elasticsearch - Elasticsearch 索引中嵌套字段的 Cube.js 架构