如何将自定义请求类型添加到Express?

时间:2019-09-04 09:13:28

标签: typescript-typings

我想添加自定义请求类型来表达。

有一种方法可以扩展请求。但是这种方式我必须检查的是auth undefined。

我找不到与app.get('/ path /,...)一起使用AuthorizedRequest的方法

如何正确声明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:此调用没有重载。”

1 个答案:

答案 0 :(得分:0)

  1. 创建.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
  2. 设置tsconfig:./typings/index.d.ts

index.d.ts示例:

"typeRoots": ["./typings", "./node_modules/@types"]
相关问题