0

When linting the following code:

  public doStuf = (): Promise<null> => {
    const data = new Stuff(this.value);
    if (this.state === 'test') {
        data.mail = {
          object: label,
          files: this.dropppedFiles || []
        };
        return this.validate = (data) => null;
    } else {
        return this.validate = (data) => null;
    }
  }

I'm passing doStuff() as promise to a child component that's why I need to keep the context with this.validate = (data) => null;

(There's maybe a more elegant way of doing things?)

I get

Shadowed variable: 'data'

I tried unsing let instead of const but lint complains again about data is never modified, use const instead

All my tests pass, and the component works as expected. How do I get rid of this error?

EDIT: What works is this.validate = (boo = data) => null;

How horrible is that?

EDIT:

I actually want to pass parameters to the validate function and keep the arrow function structure at the same time. Something like:

this.validate(data) = () => null;
gyc
  • 4,300
  • 5
  • 32
  • 54
  • Just don't call the other variable `data`. `return this.validate = (whatever) => {} ` – Roberto Zvjerković Mar 06 '18 at 15:42
  • Possible duplicate of [Making Sense of 'No Shadowed Variable' tslint Warning](https://stackoverflow.com/questions/44853057/making-sense-of-no-shadowed-variable-tslint-warning) - the lint is simply telling you that you're hiding a variable by defining another with the same name. That won't necessarily cause issues, but it can make it easy to introduce bugs in some cases. – Joe Clay Mar 06 '18 at 15:42
  • It's not a duplicate. Linked post doesn't solve my problem. – gyc Mar 06 '18 at 16:07

1 Answers1

1

When you write

this.validate = (data) => null;

this.validate is a function with one parameter, data, which means you can no longer reference the original data variable.

If you call this parameter something else it will run fine.

this.validate = (param) => null;

EDIT

From your comment it sounds like you don't want to define but call the this.validate function. In which case just do

this.validate(data);
Aron
  • 8,696
  • 6
  • 33
  • 59
  • But it used to be this.validate(data). I need to pass data as parameter – gyc Mar 06 '18 at 15:44
  • @gyc: The value of the parameter passes to `this.validate` will be the same, regardless of what you name it. – Joe Clay Mar 06 '18 at 15:45
  • 2
    You're not passing `data` this way. You're just declating `this.validate` as a method that takes one parameter, which you can call whatever you want. – Roberto Zvjerković Mar 06 '18 at 15:46
  • I understand. How do I pass data as parameter while keeping the structure this.validate = () => null; ? I need this structure to keep the right context when passing doStuff() to another component – gyc Mar 06 '18 at 15:48
  • 1
    I accept the answer because it led me to avoiding the problem in the first place (which was in a unit test) – gyc Mar 06 '18 at 16:13