我在TypeScript项目中使用passport。 The DefinitelyTyped type definition for passport覆盖Express请求,以包含user
属性。但是它将用户定义为一个空接口:
index.d.ts
declare global {
namespace Express {
...
// tslint:disable-next-line:no-empty-interface
interface User {}
interface Request {
...
user?: User;
...
}
}
}
但是在我的整个应用程序中,我希望req.user
成为这种类型的接口:
interface User {
id: number
username: string,profilePicture: string,name: string
}
我厌倦了在整个应用程序中编写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 module declarations
没有declare global
的tsc仍将req.user
视为一个空接口。
我必须在tsconfig.json
中包含什么配置才能使用此替代?