简体   繁体   English

tslint不会在* .js文件中检测到未声明的变量

[英]tslint does not detect undeclared variable in *.js files

Under Visual Code and using tslint to also check *.ts and *.js files, undeclared variable are not detected in .js files as they are in .ts files. 在Visual Code中,并使用tslint来检查* .ts和* .js文件,.js文件中未检测到未声明的变量,因为它们在.ts文件中。

Any idea ? 任何想法 ?

As you can see no-console is well detected on both files : .js and .ts iterableGGG is not declared => in the ts file, it is underlined in red. 如您所见,在两个文件中都很好地检测到没有控制台:.ts和.ts的iterableGGG没有在ts文件中声明为=>,并带有红色下划线。 But not in the js file. 但不在js文件中。

Note: the import from lodash is just there to have a module, so variables in the ts file are not global. 注意:从lodash导入只是为了有一个模块,因此ts文件中的变量不是全局变量。

在此处输入图片说明

在此处输入图片说明

tslint.json tslint.json

{
    "defaultSeverity": "error",
    "extends": [
        "tslint:recommended"
    ],
    "jsRules": {},
    "rules": {
        "no-any": false
    },
    "rulesDirectory": []
}

I believe that the jsRules should look like the following if I'm not mistaken. 我相信,如果我没有记错的话,jsRules应该如下所示。

"jsRules": {
    "no-use-before-declare": true
}

I found the solution. 我找到了解决方案。

in the TS file the undeclared variable iterableGGG is detected by tsc NOT tslint, You can observe that because in VC the warning/error popup message begins with [ts] instead of [tslint] . 在TS文件中,未声明的变量iterableGGG由tsc NOT tslint检测到,您可以观察到,因为在VC中,警告/错误弹出消息以[ts]而不是[tslint]开头。 And when tslint detect sth, there is a rule name (for ex : "no-use-before-declare") You also see that in CLI, directly executing the command tsc and tslint. 并且当tslint检测到sth时,会有一个规则名称(例如:“ no-use-before-declare”),您还可以在CLI中直接执行命令tsc和tslint。 you can check that this type of error is raised by tsc. 您可以检查tsc是否引发了此类错误。

>tsc
src/examplJS.js(3,19): error TS2552: Cannot find name 'iterableGGG'. Did you mean 'iterable' ?
src/examplTS.js(5,19): error TS2304: Cannot find name 'iterableGGG'.

so going to the tsc config tsconfig.json, just activate "checkJs" to true and VC will also show the error for JS codes as well as TS 因此转到tsc config tsconfig.json,只需将“ checkJs”激活为true,VC还将显示JS代码和TS的错误

"allowJs": true, /* Allow javascript files to be compiled. “ allowJs”:true,/ *允许编译javascript文件。 */ * /

"checkJs": true, /* Report errors in .js files. “ checkJs”:true,/ *报告.js文件中的错误。 */ * /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM