【发布时间】:2020-03-06 08:21:11
【问题描述】:
我在 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 中包含什么配置才能获取此覆盖?
【问题讨论】:
标签: typescript typescript-typings