typescript-typings - 如何将自定义请求类型添加到 Express?
问题描述
我想添加自定义请求类型来表达。
有办法只扩展请求。但是我必须检查的这种方式是未定义的身份验证。
我找不到如何将 AuthorizedRequest 与 app.get('/path/, ...) 一起使用
如何正确声明 AuthorizedRequest?
// need to check undefined, auth can not exists so i cant remove "?"
declare module 'express-serve-static-core' {
interface Request {
auth?: {
test: string
}
}
}
// error with routing app.get(...)
declare module 'express-serve-static-core' {
interface AuthorizedRequest<P extends Params = ParamsDictionary> extends Request<P> {
auth: {
test: string
}
}
}
但我收到以下错误:
“- 错误 TS2769:没有与此调用匹配的重载。”
解决方案
- 创造
./typings/index.d.ts
- 设置 tsconfig:
"typeRoots": ["./typings", "./node_modules/@types"]
index.d.ts 示例:
type Params = {
test1: {
test2: string
},
test3: number
}
declare global {
namespace Express {
export interface Request extends Params {}
}
}
declare module 'express' {
export type ExtendableRequest = Request<any, any, any, any> &
}
export {}
推荐阅读
- python - sklearn.utils 模块未找到
- c# - 在 JsonDeserializer 中正确跳过父级
- python - 在 'r+' 中截断文件使整个文件变成矩形
- google-drive-api - 未经授权的 WebHook 回调通道 Google 推送通知
- python - 从 OSMNX Geoseries 获取坐标列表(纬度、经度)
- pandas - Pandas Groupby:函数定义中的条件语句
- ios - 如何使用带有可选 UIImage 的绑定?
- c# - 等待后 EF Core 异步方法挂起/死锁
- eclipse - 无法打包插件
- javascript - Google Slide modalDialog HTML javascript 可以运行服务器端代码吗?环境应用脚本