我正在开发一个快速应用程序,我想在 Request 的类实例上添加一个新属性。
所以我在我的项目根目录下创建了一个 type.d.ts 文件,类似于
type User = {
name: string
}
declare namespace Express {
export interface Request {
user: User
}
}
然后在我的中间件功能的某个地方,
const user = req.user.name
上面的代码抛出错误说,
Property user does on exist on type Request<ParamsDictionary, any, any .....>
但是,VS 代码不会对此有任何抱怨,只有当我尝试使用 nodemon 启动 express 服务器时,它才会抛出错误。
关于此的任何线索都会有所帮助。提前致谢。