1

As shown in the image below, I need to manipulate the input parameter of the constructor that is commandsList but it gave an unknown variable, even with using the this. with it.

I wounder that it accepted this parameter and passed it as an input paramter for another method in the same class, that forced me to write a separate method to handle the few lines I need.

Any help?

image

1 Answer 1

5

You have to make it an instance variable. Currently it is only a parameter. You can create auto instance variables from constructors like this

constructor(private commandslist: ICommandList) {
   console.log(this.commandslist);
}

Or if you prefer to have it declared explicitly:

private commandslist: ICommandList;

constructor(commandslist: ICommandList) {
    this.commandslist = commandslist;
    console.log(this.commandslist);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Although I usually prefer more-explicit. I like the first approach in this case. It's soo clean, and TS takes care of the rest.
FYI I think they're officially called "parameter properties", not "auto instance variables". typescriptlang.org/docs/handbook/classes.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.