tslint Error - Shadowed name: 'err'
Asked Answered
F

5

32

tslint is currently throwing the following error

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?

Fishworm answered 28/5, 2018 at 8:26 Comment(1)
It means you've shadowed the name err; you're using the same name for the error in two nested callbacks, so the outer error is inaccessible in the inner callback body. As to how to solve it: use a different name for one of them.Maddeu
T
40

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

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

fs.readdir(fileUrl, (readDirError, files) => {
    fs.readFile(path.join(fileUrl, files[0]), function (err, data) {
            if (!err) {
                res.send(data);
            }
        });
    });
Turpitude answered 28/5, 2018 at 8:47 Comment(1)
In this context what is 'err' value? where is that var defined?Embellish
D
5

Add this comment just above the error line--

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

Diastema answered 29/4, 2019 at 8:32 Comment(0)
E
3

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.

Example: This should work

fs.readdir(fileUrl, (error, files) => {
        fs.readFile(path.join(fileUrl, files[0]), function (err, data) {
            if (!err) {
                res.send(data);
            }
        });
    });
Enshrine answered 12/2, 2020 at 8:20 Comment(0)
J
3

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

Use different variable names in such cases.

Janae answered 26/4, 2020 at 3:0 Comment(0)
T
0

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

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'

// 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 :)

Trommel answered 4/2, 2019 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.