类型“ Request <ParamsDictionary>”上不存在属性“”

当尝试从软件包Request扩展express接口以添加一些自定义属性时,出现以下打字错误:

TS2339: Property '' does not exist on type 'Request<ParamsDictionary>'.

你知道如何解决吗?

chenhao988325 回答:类型“ Request <ParamsDictionary>”上不存在属性“”

只需添加以下内容,这就是将简单的自定义属性添加到Express Request接口中

declare global {
  namespace Express {
    interface Request {
      propertyName: string; //or can be anythin
    }
  }
}
,

自从typings及其依赖项的最新更新以来,我发现以下内容应可修复应用程序中的错误。

在您的tsconfig.json

{
  "compilerOptions": {
    //...
    "typeRoots": [
      "./custom_typings","./node_modules/@types"
    ],}
// ...
}

在您的自定义键入中

// custom_typings/express/index.d.ts
declare namespace Express {
    interface Request {
        customProperties: string[];
    }
}
本文链接:https://www.f2er.com/3064803.html

大家都在问