代码之家  ›  专栏  ›  技术社区  ›  HelloWorld

我应该在try/catch中注释TypeScript中的错误吗?

  •  0
  • HelloWorld  · 技术社区  · 3 年前

    我陷入了错误处理的地狱,我仍然很难理解2022年针对 try/catch 条款

    例如:

    try {
    } catch (error) {
        console.log(error.message);
    }
    

    注释是否可取 error 具有类型 Error ,还是不? eslint 抱怨 错误 属于类型 any ,我不应该使用 .message

    因此,解决方案是:

    try {
    } catch (error: Error) {
        console.log(error.message);
    ///
    

    是真的需要声明,还是只是嘈杂的代码?在我看来 eslint 我别无选择。

    0 回复  |  直到 3 年前