我在 TypeScript 项目中使用通行证。护照的绝对类型类型定义覆盖 Express 请求以包含该user属性。但它将用户定义为一个空接口:
index.d.ts
declare global {
namespace Express {
...
// tslint:disable-next-line:no-empty-interface
interface User {}
interface Request {
...
user?: User;
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在我的整个应用程序中,我想req.user成为这种类型的接口:
interface User {
id: number
username: string,
profilePicture: string,
name: string
}
Run Code Online (Sandbox Code Playgroud)
我厌倦了req.user as User | null在整个申请过程中写作。我可以直接覆盖定义文件中的接口定义,但是node_modules如果我更新类型模块,修改文件中的文件可能会被覆盖,这似乎是不好的做法。
有没有办法在我的 src 文件夹中编写一个覆盖的定义文件Express.User?我已经尝试将定义文件复制并修改到我的 src 目录,但它说
Augmentations for the global scope can only be directly nested in external modules or ambient …