【问题标题】:How can I extend a flow type for express$Request?如何扩展 express$Request 的流类型?
【发布时间】:2019-09-11 18:10:04
【问题描述】:

我使用的是flow-typed,所以它有一个express 定义。在我的代码中,我有:

isPayloadSignatureValid(req /*: express$Request */ ) {
  const rawBodyPayload = Buffer.from(req.rawBody).toString('utf8')

但流量抱怨说:

Cannot get req.rawBody because property rawBody is missing in express$Request [1].

在我的interfaces/express.js 中,我有:

// @flow
/*::
type express$Request = exports.Request & {
  rawBody: string
}
export type { express$Request };
*/

在我的.flowconfig 中,我有:


[include]
./interfaces/.*

那么为什么扩展的express$Request 没有rawBody 属性?

谢谢

【问题讨论】:

  • 您需要从interfaces/expressdeclare 导出express$Request 类型并将其添加为库定义,以便Flow 提取它。

标签: javascript flowtype flow-typed


【解决方案1】:

当您实际使用 express 时,您不会从 interfaces/express.js 获取类型。您需要将其导入并自己注入。通常在这种情况下,您只想直接从流类型修改 express libdef。另外你应该避免在这里使用交集类型,你应该分散对象类型(...)。

【讨论】:

    猜你喜欢
    • 2019-05-14
    • 2021-05-30
    • 2018-07-21
    • 2015-11-05
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2021-01-07
    相关资源
    最近更新 更多