typescript tslint 错误 - 阴影名称:'err'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50562079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:26:31  来源:igfitidea点击:

tslint Error - Shadowed name: 'err'

node.jsangulartypescriptlinttslint

提问by bobdolan

tslint is currently throwing the following error

tslint 当前抛出以下错误

Shadowed name: 'err'

Here is the code

这是代码

fs.readdir(fileUrl, (err, files) => {
        fs.readFile(path.join(fileUrl, files[0]), function (err, data) {
            if (!err) {
                res.send(data);
            }
        });
    });

Anybody have a clue on what the best way would be to solve this and what the error even means?

任何人都知道解决这个问题的最佳方法是什么,错误甚至意味着什么?

回答by Padmapriya Vishnuvardhan

You are using the same variable "err" in both outer and inner callbacks, which is prevented by tslint.

您在外部和内部回调中使用相同的变量“err”,这是被 tslint 阻止的。

If you want to use the same variable then "no-shadowed-variable": false, otherwise do as below.

如果要使用相同的变量,则“no-shadowed-variable”:false,否则请执行以下操作。

fs.readdir(fileUrl, (readDirError, files) => {
    fs.readFile(path.join(fileUrl, files[0]), function (err, data) {
            if (!err) {
                res.send(data);
            }
        });
    });

回答by Ankit

Add this comment just above the error line--

在错误行上方添加此注释--

// tslint:disable-next-line:no-shadowed-variable

// tslint:disable-next-line:no-shadowed-variable

回答by vijaya bharathi

This shadowed name tslint error is due to repetition of the name 'err' twice in your callback functions. You can change either anyone 'err' to other name so this should work.

这个隐藏的名称 tslint 错误是由于在回调函数中重复了两次名称“err”。您可以将任何“错误”更改为其他名称,这样就可以了。

Example: This should work

示例:这应该有效

fs.readdir(fileUrl, (error, files) => {
        fs.readFile(path.join(fileUrl, files[0]), function (err, data) {
            if (!err) {
                res.send(data);
            }
        });
    });

回答by Yuvraj Patil

When same variable is declared multiple times in same scope, this warning occurs.

当在同一范围内多次声明同一变量时,会出现此警告。

Use different variable names in such cases.

在这种情况下使用不同的变量名称。

回答by Nver Abgaryan

this line will disable your error,

此行将禁用您的错误,

// tslint:disable: no-shadowed-variable

but it's not okay to have tow err variables you can also change the second err variable name to something different

但是拥有两个 err 变量是不行的,您也可以将第二个 err 变量名称更改为不同的名称

fs.readdir(fileUrl, (err, files) => {
  fs.readFile(path.join(fileUrl, files[0]), function (readFileErr, data) {        
    if (!readFileErr) {
            res.send(data);
        }
    });
});

I had an error like this interfaces.ts:119:26 - Shadowed name: 'POST'

我有这样的错误 interfaces.ts:119:26 - Shadowed name: 'POST'

// tslint:disable: no-shadowed-variable
interface API {
   export namespace APINAME {
     export type POST {

     }
   }
   export namespace OTHERAPINAME {
     export type POST {

     }
   }
}

I have disabled this error case with this line // tslint:disable: no-shadowed-variablebecause sometimes typescript compiler cannot understand your code right :)

我用这一行禁用了这个错误案例,// tslint:disable: no-shadowed-variable因为有时打字稿编译器无法正确理解您的代码:)