简体   繁体   English

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

[英]tslint Error - Shadowed name: 'err'

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?任何人都知道解决这个问题的最佳方法是什么,错误甚至意味着什么?

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);
            }
        });
    });

Add this comment just above the error line--在错误行上方添加此注释--

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

This shadowed name tslint error is due to repetition of the name 'err' twice in your callback functions.这个隐藏的名称 tslint 错误是由于在回调函数中重复了两次名称“err”。 You can change either anyone 'err' to other name so this should work.您可以将任何“错误”更改为其他名称,这样就可以了。

Example: This should work示例:这应该有效

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

When same variable is declared multiple times in same scope, this warning occurs.当在同一范围内多次声明同一变量时,会出现此警告。

Use different variable names in such cases.在这种情况下使用不同的变量名称。

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-variable because sometimes typescript compiler cannot understand your code right :)我用这一行禁用了这个错误案例// tslint:disable: no-shadowed-variable因为有时打字稿编译器无法正确理解你的代码:)

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

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