tslint 说不允许调用 console.log - 我如何允许这个?
tslint says calls to console.log are not allowed - How do I allow this?
我刚开始使用带有 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() 行,但我希望将来能够这样做。
在您调用 console.log
之前的行中添加 // tslint:disable-next-line:no-console
以防止仅出现一次错误消息。
如果您想完全禁用该规则,请将以下内容添加到您的 tslint.json
(最有可能在您的根文件夹中):
{
"rules": {
"no-console": false
}
}
根据文档:https://eslint.org/docs/user-guide/getting-started#configuration
- "off" 或 0 - 关闭规则
- "warn" 或 1 - 打开规则作为警告(不影响退出代码)
- "error" 或 2 - 将规则作为错误打开(退出代码将为 1)
顺便说一下,您的正确设置应该是
{
"rules": {
"no-console": false
}
}
对于那些使用 javascript 和 typescript 的混合代码库来到这里的人。
您可能需要在 jsRules 中定义 'no-console' 选项,javascript 文件的 jslints 规则对象,即 javascript 和打字稿有单独的规则对象。
//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
}
}
将以下内容添加到您的 tslint.json
{
"rules": {
"no-console": {
"severity": "warning",
}
}
}
这是定义无控制台规则(或与此相关的任何其他规则)的正确语法,但仅带有警告而不是错误(显然将选项更改为您想要的任何内容)
"no-console": {
"severity": "warning",
"options": [
"log",
"error",
"debug",
"info",
"time",
"timeEnd",
"trace"
]
},
在 typeScript 版本 3 中更新 tslint.json 关键规则如下:
"no-console": [
true,
"debug",
"time",
"timeEnd",
"trace"
],
通过这种方式,您只需指定不使用调试、时间、时间结束、跟踪,如果在您的默认 tslint "info" 列表中,只需将其删除。
如果 // tslint:disable-next-line:no-console
不起作用尝试 // eslint:disable-next-line:no-console
{
"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
}
}
我处理 tslint“无控制台”规则 的方式是按文件处理,我发现它在开发阶段很方便并且是独立的。
一旦我需要使用第一个 console.log(); Visual Studio 代码显示要添加的选项:
// tslint:disable-next-line: no-console
console.log();
所以这里我只删除“-next-line”,这个命令将覆盖整个文件。
// tslint:disable: no-console
console.log();
作为禁用整个应用程序功能的替代方法,我希望它能有所帮助。
罗恩
语法已更改!
对于 next-line 异常,no-console 之前需要一个 space:// eslint-disable-next-line no-console
。
对于文件异常,它也必须在Multi-line注释语法中:/* eslint-disable no-console */
.
它也可能取决于某些配置。
我刚开始使用带有 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() 行,但我希望将来能够这样做。
在您调用 console.log
之前的行中添加 // tslint:disable-next-line:no-console
以防止仅出现一次错误消息。
如果您想完全禁用该规则,请将以下内容添加到您的 tslint.json
(最有可能在您的根文件夹中):
{
"rules": {
"no-console": false
}
}
根据文档:https://eslint.org/docs/user-guide/getting-started#configuration
- "off" 或 0 - 关闭规则
- "warn" 或 1 - 打开规则作为警告(不影响退出代码)
- "error" 或 2 - 将规则作为错误打开(退出代码将为 1)
顺便说一下,您的正确设置应该是
{
"rules": {
"no-console": false
}
}
对于那些使用 javascript 和 typescript 的混合代码库来到这里的人。
您可能需要在 jsRules 中定义 'no-console' 选项,javascript 文件的 jslints 规则对象,即 javascript 和打字稿有单独的规则对象。
//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
}
}
将以下内容添加到您的 tslint.json
{
"rules": {
"no-console": {
"severity": "warning",
}
}
}
这是定义无控制台规则(或与此相关的任何其他规则)的正确语法,但仅带有警告而不是错误(显然将选项更改为您想要的任何内容)
"no-console": {
"severity": "warning",
"options": [
"log",
"error",
"debug",
"info",
"time",
"timeEnd",
"trace"
]
},
在 typeScript 版本 3 中更新 tslint.json 关键规则如下:
"no-console": [
true,
"debug",
"time",
"timeEnd",
"trace"
],
通过这种方式,您只需指定不使用调试、时间、时间结束、跟踪,如果在您的默认 tslint "info" 列表中,只需将其删除。
如果 // tslint:disable-next-line:no-console
不起作用尝试 // eslint:disable-next-line:no-console
{
"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
}
}
我处理 tslint“无控制台”规则 的方式是按文件处理,我发现它在开发阶段很方便并且是独立的。
一旦我需要使用第一个 console.log(); Visual Studio 代码显示要添加的选项:
// tslint:disable-next-line: no-console
console.log();
所以这里我只删除“-next-line”,这个命令将覆盖整个文件。
// tslint:disable: no-console
console.log();
作为禁用整个应用程序功能的替代方法,我希望它能有所帮助。
罗恩
语法已更改!
对于 next-line 异常,no-console 之前需要一个 space:// eslint-disable-next-line no-console
。
对于文件异常,它也必须在Multi-line注释语法中:/* eslint-disable no-console */
.
它也可能取决于某些配置。