我是一个打字新手,正在做一个项目。我正在尝试构建一个rest API,并且我有一个用于我的请求负载的模型。
但是在进行类型转换之后,reObj的类型仍然是object,我希望它是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,
}
正因为如此,数据根本没有得到验证。我找了很多,但找不到任何灵魂。
发布于 2021-05-11 19:23:22
TypeScript被编译成JavaScript文件,JavaScript没有任何名为requestObj
的类型。所有对象都具有object
类型。在TypeScript中,所有类型都只在编译前存在。如果你想检查TypeScript中的类型,你应该使用conditional types,因为使用javascript的'typeof‘对你没有帮助。
https://stackoverflow.com/questions/67346365
复制相似问题