【问题标题】:How do I annotate the Type of a custom Request header?如何注释自定义请求标头的类型?
【发布时间】:2017-06-18 12:54:18
【问题描述】:

我有一个端点,它采用 string 类型的自定义 header

我正在使用Flow 进行类型注释,但我不确定如何像这样将注释添加到本机Type 的自定义属性上。

比如我的函数是:

function(req: Request) {
  const custom = req.headers.custom_header;
}

Flow 报告the property custom_header is not found in Header

我尝试了几种不同的方式使用类型声明:

declare var Request.headers.custom_header: string declare var req.headers.custom_header: string

但似乎两者都不会影响错误。

【问题讨论】:

    标签: javascript node.js flowtype


    【解决方案1】:

    Headers 不仅仅是一个对象。有一个 Headers.get() 方法可以执行您认为的操作,并进行类型检查。

    function parse(req: Request) {
      const custom = req.headers.get('custom_header');
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 2014-08-08
      • 2013-11-13
      • 2014-08-13
      相关资源
      最近更新 更多