我试图添加一个属性来表达使用typescript从中间件请求对象。但是,我不知道如何向对象添加额外的属性。如果可能的话,我宁愿不用括号。
我正在寻找一个解决方案,允许我写类似的东西(如果可能的话):
app.use((req, res, next) => {
req.property = setProperty();
next();
});
我试图添加一个属性来表达使用typescript从中间件请求对象。但是,我不知道如何向对象添加额外的属性。如果可能的话,我宁愿不用括号。
我正在寻找一个解决方案,允许我写类似的东西(如果可能的话):
app.use((req, res, next) => {
req.property = setProperty();
next();
});
当前回答
现在回答这个问题可能已经很晚了,但无论如何,我是这样解决的:
确保在tsconfig文件中包含了类型的源代码(这可能是一个全新的线程) 在types目录中添加一个新目录,并将其命名为要为其扩展或创建类型的包。在本例中,您将创建一个名为express的目录 在express目录中创建一个文件,并将其命名为index.d.ts(必须与此完全相同) 最后,为了扩展类型,你只需要放如下代码:
declare module 'express' {
export interface Request {
property?: string;
}
}
其他回答
所有这些回答在某种程度上似乎都是错误的或过时的。
这在2020年5月对我有效:
在$ {PROJECT_ROOT} / @types /快递/ index.d.ts:
import * as express from "express"
declare global {
namespace Express {
interface Request {
my_custom_property: TheCustomType
}
}
}
在tsconfig。Json,添加/合并属性,使:
"typeRoots": [ "@types" ]
欢呼。
在mac节点12.19.0和express 4上,使用护照进行身份验证,我需要扩展我的Session对象
与上面相似,但略有不同:
import { Request } from "express";
declare global {
namespace Express {
export interface Session {
passport: any;
participantIds: any;
uniqueId: string;
}
export interface Request {
session: Session;
}
}
}
export interface Context {
req: Request;
user?: any;
}```
我也有同样的问题,我是这样解决的:
// /src/types/types.express.d.ts
declare namespace Express {
export interface Request {
user: IUser
}
}
但有一些条件是必须的!
添加到tsconfig。json配置
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
},
在此之后,tsc将构建bundle,而ts-node则不会。
必须在编译器命令中添加——files
ts-node --files src/server.ts
对于简单的情况,我在外部中间件中使用headers属性,然后在内部中间件中获得它。
// outer middleware
req.headers["custom_id"] = "abcxyz123";
// inner middleware
req.get("custom_id");
缺点:
只能存储字符串。如果您想存储其他类型,如json或number,您可能必须稍后解析它。 headers属性没有文档化。Express只记录了req.get()方法。因此,您必须使用与属性头文件一起工作的Express的确切版本。
可选择的解决方案
这实际上并没有直接回答这个问题,但我提供了一个替代方案。我也在与同样的问题作斗争,并尝试了该页面上几乎所有的界面扩展解决方案,但没有一个有效。
这让我停下来思考:“为什么我实际上在修改请求对象?”
使用response.locals
Express开发人员似乎认为用户可能希望添加自己的属性。这就是为什么有一个locals对象。问题是,它不在请求中,而是在响应对象中。
响应。Locals对象可以包含您可能希望拥有的任何自定义属性,这些属性封装在请求-响应周期中,因此不会暴露给来自不同用户的其他请求。
需要存储userId?只需设置response.locals.userId = '123'。没有必要纠结于打字。
它的缺点是,您必须传递响应对象,但很可能您已经这样做了。
https://expressjs.com/en/api.html#res.locals
打字
另一个缺点是缺乏类型安全性。然而,你可以在Response对象上使用泛型类型来定义body和locals对象的结构:
响应< MyResponseBody, MyResponseLocals >
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts#L127
警告
你不能保证userId属性确实在那里。您可能希望在访问它之前进行检查,特别是在对象的情况下。
使用上面的例子来添加一个userId,我们可以有这样的东西:
interface MyResponseLocals {
userId: string;
}
const userMiddleware = (
request: Request,
response: Response<MyResponseBody, MyResponseLocals>,
next: NextFunction
) => {
const userId: string = getUserId(request.cookies.myAuthTokenCookie);
// Will nag if you try to assign something else than a string here
response.locals.userId = userId;
next();
};
router.get(
'/path/to/somewhere',
userMiddleware,
(request: Request, response: Response<MyResponseBody, MyResponseLocals>) => {
// userId will have string type instead of any
const { userId } = response.locals;
// You might want to check that it's actually there
if (!userId) {
throw Error('No userId!');
}
// Do more stuff
}
);