【问题标题】:Possible to enforce type in this specific functional context?可以在这个特定的功能上下文中强制执行类型吗?
【发布时间】:2016-08-04 17:36:19
【问题描述】:

我正在使用具有特定语法的 lodash,并且我想使用 facebook flowtype 强制执行类型。这是代码:

 let errorMessageBag = _.flow( objectWithFieldsToValidate, requiredFields, 
                             stringLengthValidationCheck, emailValidation)
 let result = errorMessageBag(fields, linkmodel())

我想强制执行 errorMessageBag 调用以传递 2 个参数、字段和 linkmodel()。为了简单起见,假设我希望他们成为string,string

流参数是下面函数的名称。这是第一个 objectWithFieldsToValidate 接受 2 个参数,但如果我输入它们,它不会传播。无论如何要解决这个问题?

【问题讨论】:

    标签: javascript types flowtype


    【解决方案1】:

    一个简单的解决方法是将 errorMessageBag(fields, linkmodel()) 封装到一个具有易于键入参数的函数中。所以你最终调用了返回 errorMessageBag(fields, linkmodel()) 的新函数。

    【讨论】:

      【解决方案2】:

      这个怎么样:

      // assuming `void` is the return type of validation
      let errorMessageBag: (x: string, y: string) => void = ...; 
      let result = errorMessageBag(fields, linkmodel())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        • 2019-06-15
        • 1970-01-01
        • 2016-06-01
        • 1970-01-01
        • 2018-07-23
        • 1970-01-01
        相关资源
        最近更新 更多