【问题标题】:tslint says calls to console.log are not allowed - How do I allow this?tslint 说不允许调用 console.log - 我该如何允许?
【发布时间】:2023-03-26 06:01:01
【问题描述】:

我刚开始使用带有 typescript 的 create-react-app

create-react-app my-app --scripts-version=react-scripts-ts

并且默认的 tslint.json 配置不允许 console.log()。

我如何(目前)启用 console.log?

这方面的文档位于https://palantir.github.io/tslint/rules/no-console/。但他们没有说把这条线放在哪里:

    "no-console": [true, "log", "error"]

我搜索并找到了这个tslint.json configuration file syntax,所以我尝试了这个:

"rules": {
    "no-console": [true, "warning"]
}

试图获取只是警告的日志消息。 但这没有用。

我已经注释掉了我拥有的几行 console.log() 行,但我希望将来能够这样做。

【问题讨论】:

    标签: reactjs typescript create-react-app tslint


    【解决方案1】:

    在您调用console.log 之前的行中添加// tslint:disable-next-line:no-console,以防止仅出现一次错误消息。

    如果您想完全禁用该规则,请将以下内容添加到您的 tslint.json(很可能在您的根文件夹中):

    {
        "rules": {
            "no-console": false
        }
    }
    

    【讨论】:

    • 我不确定发生了什么,但现在 "no-console": false 对我不起作用。我发现解决方法是将// tslint:disable:no-console 放在文件顶部。
    • "no-console": false 对我有用,但我必须重新启动 "npm start" 才能生效。
    • "no-console": false 对我不起作用,即使使用 npm run start
    • @EricFulmer 将其放在“jsRules”节点中。 "jsRules": { "no-console": false },
    • 或在同一行如下:console.log('hello world'); // tslint:disable-line:no-console
    【解决方案2】:

    对于那些带着 javascript 和 typescript 混合代码库来到这里的人。

    您可能需要在 jsRules 中定义 'no-console' 选项,用于 javascript 文件的 jslints 规则对象,即 javascript 和 typescript 有单独的规则对象。

    //tslint.json

    {
      "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"], //Example... 
      "rules": {
        "no-console": false //Disable for typescript
      },
      "jsRules": {
        "no-console": false //Disable for javascript
      }
    }
    

    【讨论】:

    • 但是——这东西的目的是什么?
    • 是jsRules的,还是没有控制台的?
    • no-console - 似乎(我查了一下)它只是为了警告您控制台消息不属于生产代码。每当您的环境不是产品时,这都会成为一个值得商榷的规则
    • 我在一定程度上明白你的意思。要考虑的一件事是控制台不是 javascript 语言的一部分,它通常在 javascript 引擎中实现,但这就是重点 - 它不是 javascript 语言的一部分,您正在将依赖项烘焙到您的代码中,可能存在也可能不存在.话虽如此,我可以看到这条规则的用途。
    • @robertotomás,此规则基于代码中不包含 console.log 消息的最佳实践。生产代码不应该有这个,所以这让你知道你没有准备好生产。您可能有两种 tslint 配置,一种允许,另一种不允许。我有一个logger.info 函数,它调用console.log(所以是一个包装器),它允许我轻松启用或禁用整个应用程序的日志记录。我并不是说这是最佳实践,只是我已经做过的事情。它还可以更轻松地与 github.com/krakenjs/beaver-logger 等其他记录器集成。
    【解决方案3】:

    将以下内容添加到您的tslint.json

    {
       "rules": {
          "no-console": {
             "severity": "warning",
          } 
       }
    }
    

    【讨论】:

      【解决方案4】:

      这是定义无控制台规则(或任何其他规则)的正确语法,但仅带有警告而不是错误(显然将选项更改为您想要的任何内容)

      "no-console": {
          "severity": "warning",
          "options": [
              "log",
              "error",
              "debug",
              "info",
              "time",
              "timeEnd",
              "trace"
          ]
      },
      

      【讨论】:

      • 这非常适合作为警告。顺便说一句,这在 tslint 文档中没有记录。
      【解决方案5】:

      如果// tslint:disable-next-line:no-console 不起作用,请尝试使用// eslint:disable-next-line:no-console

      【讨论】:

      • tslint:disable-next-line:no-console 适合我
      【解决方案6】:

      我处理 tslint "no-console" 规则的方式是我发现在开发阶段方便且隔离的每个文件。

      只要我需要使用第一个 console.log(); Visual Studio Code 显示添加选项:

      // tslint:disable-next-line: no-console

      console.log();

      所以这里我只删除“-next-line”,这个命令会覆盖整个文件。

      // tslint:disable: 无控制台

      console.log();

      我希望它可以作为禁用整个应用的功能的替代方法。

      【讨论】:

        【解决方案7】:

        在 typeScript 版本 3 中更新 tslint.json 下的关键规则如下:

        "no-console": [
            true,
            "debug",
            "time",
            "timeEnd",
            "trace"
        ],
        

        这样你只需指定不使用的调试、时间、时间结束、跟踪,如果在你的默认 tslint 中“信息”在列表中,只需将其删除。

        【讨论】:

        【解决方案8】:

        根据文档:https://eslint.org/docs/user-guide/getting-started#configuration

        • “关闭”或 0 - 关闭规则
        • "warn" 或 1 - 打开规则作为警告(不影响退出代码)
        • “错误”或 2 - 将规则作为错误打开(退出代码将为 1)

        顺便说一下,你的正确设置应该是

        {
          "rules": {
            "no-console": false
          }
        }
        

        【讨论】:

          【解决方案9】:
            {
              "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
              "linterOptions": {
                  "exclude": [
                      "config/**/*.js",
                      "node_modules/**/*.ts",
                      "coverage/lcov-report/*.js"
                  ]
              },
              "rules": {
                  "no-console": false
              },
              "jsRules": {
                  "no-console": false
              }
           }
          

          【讨论】:

            猜你喜欢
            • 2017-08-30
            • 2015-03-13
            • 1970-01-01
            • 2017-08-08
            • 2020-12-22
            • 1970-01-01
            • 2016-12-12
            • 1970-01-01
            • 2015-08-15
            相关资源
            最近更新 更多