【问题标题】:Flow: refinement not work on custom type流程:细化不适用于自定义类型
【发布时间】:2018-07-08 12:44:18
【问题描述】:

例如这是我的代码:

export const handleRequestError = (error: (AxiosError<> | Error)) => {
  if (typeof error === Error) {
    errorNotification('Request Fail', error.message);
    console.log('Error', error.message);
  }
}

但是当我运行yarn flow。我总是遇到这个错误:

Cannot get error.message because property message is missing in AxiosError [1].

 [1] 10│ export const handleRequestError = (error: (AxiosError<> | Error)) => {
     11│   // General error
     12│   if (typeof error === Error) {
     13│     errorNotification('Request Fail', error.message);
     14│     console.log('Error', error.message);
     15│   }

看起来该流程无法识别我有错误类型的检查条件。我该如何解决这个问题。

谢谢

【问题讨论】:

    标签: javascript flowtype flow-typed


    【解决方案1】:
     typeof error 
    

    实际上是"object",你的支票应该是:

     error instanceof Error
    

    【讨论】:

    猜你喜欢
    • 2020-03-11
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2013-07-13
    • 2016-07-17
    • 2012-10-26
    • 2020-04-20
    相关资源
    最近更新 更多