【发布时间】:2021-07-24 12:47:49
【问题描述】:
我是打字稿的新手,正在做一个项目。 我正在尝试构建一个 REST API,并且我的请求有效负载有一个模型。
但是在类型转换之后 reObj 的类型仍然是对象,我希望它是 requestObj 类型
export const funcName = async function (req: Express.Request, res: Express.Response) {
console.log(typeof req.body) // object
const resObj : requestObj = req.body as requestObj
console.log(typeof resObj) // object. but it should be requestObj
export interface requestObj {
to: string ,
name: string,
phoneNo : string,
}
因为这些数据根本没有得到验证。 我搜索了很多,但找不到任何灵魂。
【问题讨论】:
-
Express.Request类型支持泛型定义body类型。见stackoverflow.com/a/55413670/14357
标签: node.js typescript rest express backend