axios try-catch 语句中的意外标记,预期为“)”

Unexpected token, expected ")" inside axios try-catch statement

尝试在 try-catch 语句中将异常 e 的类型添加为 any 时,我遇到了一个奇怪的问题 Unexpected token, expected ")" 。当从 catch 块中删除 any 类型时,它工作正常。 (显式添加 any 与没有 git 管道失败一样)

 try {
      await axios.patch(
        "/scriptschedule/" + pk + "/",
        postData
      );
      setSubmitting(false);
      afterCreateOrUpdateAction();
    } catch (e: any) {
      setSubmitting(false);
      let message =
        e.response.data && e.response.data.non_field_errors
          ? e.response.data.non_field_errors
          : "Error. Please try again";
      addToast({
        id: "1",
        title: "Error",
        color: "danger",
        text: <p>{message}</p>,
      });
    }

语法对我来说很好。我错过了什么吗?

无法对 catch 子句中的错误进行类型注释。解决这个问题的方法是转换变量(我相信你已经做到了)。

try { 
  // ...
} catch (e) {
  (e as any)...
}