Search code examples
javascriptflowtypeflow-typed

Flow: refinement not work on custom type


For example here is my code:

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

But when I run yarn flow. I always meet this error:

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│   }

It looks like that flow doesn't recognize that I have check condition to Error type. How can I fix this.

Thanks


Solution

  •  typeof error 
    

    is actually "object", your check should be:

     error instanceof Error