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?
5 Answers
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);
}
});
});
Add this comment just above the error line--
// 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. 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
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 :)