tslint说不允许调用console.log-如何允许它?

reactjs React.js

2020-03-12

我刚开始使用带有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配置文件语法,因此我尝试了以下操作:

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

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

我已经注释掉了我拥有的少量console.log()行,但希望将来能够做到这一点。

第1299篇《tslint说不允许调用console.log-如何允许它?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

7个回答
小胖前端逆天 2020.03.12
  {
    "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
    }
 }

在此处输入图片说明

小胖Davaid 2020.03.12

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

TomEva 2020.03.12

在typeScript版本3中,根据如下所示的关键规则更新tslint.json:

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

这样,您只需指定不使用的debug,time,timeEnd,trace,如果在默认tslint中,“ info”在列表中,则将其删除。

Pro伽罗 2020.03.12

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

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

顺便说一句,正确的设置是

{
  "rules": {
    "no-console": false
  }
}
猿小宇宙小哥 2020.03.12

这是定义无控制台规则(或与此相关的任何其他规则)的正确语法,但仅带有警告而非错误(显然将选项更改为所需的任何内容)

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

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

{
   "rules": {
      "no-console": {
         "severity": "warning",
      } 
   }
}
宝儿Near 2020.03.12

// tslint:disable-next-line:no-console在通话前添加一行,console.log以防止错误消息仅出现一次。

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

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android