【问题标题】:flow: function type using ExtractReturnflow:使用 ExtractReturn 的函数类型
【发布时间】:2017-11-07 10:05:54
【问题描述】:

我正在尝试减少使用flow 键入的redux 代码的样板。

特别是,我想避免手动定义action types,而是让flow 使用ExtractReturn 技巧从typed action creators 中推导出它们(如Shane Osbourne postMartin Hochel post 中所述):

type _ExtractReturn<B, F: (...args: any[]) => B> = B;
type ExtractReturn<F> = _ExtractReturn<*, F>;

但它不会捕获简单的错误,例如它应该捕获action.idnumber 而不是stringTry Flow

如果操作是“手动”定义的,则会捕获错误:Try Flow

如何在reducer 函数中正确捕获所有错误,而无需手动编写样板代码?

  • 编辑:修复了试用流程链接。
  • 编辑:我没有注意到Shane Osbourne post 提供了一个似乎有效的Flow Try。看着它。实际上,它也有同样的问题。尝试在帖子上写评论以表示它。见评论here

【问题讨论】:

  • 你的Flow Try从0.72版本开始被抓

标签: redux flowtype boilerplate


【解决方案1】:

如果您有兴趣,我设法让 Flow 捕捉到您的示例没有捕捉到的错误。 Here is the code.

毫无疑问,这很老套。我是从this Github issue 那里得到这个想法的。

除此之外,我无法提供更多见解。我自己正在寻找更好的解决方案。

编辑:再挖了一些之后,我发现$Call 实用程序类型可以提供一点帮助:

type Action =
  | $Call<typeof get, number>
  | $Call<typeof del, number>
  | $Call<typeof set, number, string>

在此之后,也许值得将$Call&lt;...&gt; 模式抽象为它自己的参数类型。不过,目前我对 Flow 的了解有限,所以我的几次尝试都没有结果。

Here is the full refactor.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多