1

My code looks like this

export class AddressingService {
   myVar;
   protected static getUserId() {
      this.myVar // property myVar doesn't exist on type 'typeof MyService'
   }
}

How can I fix that

3
  • 3
    Not possible, unless you declare the property as static, see this Commented Oct 22, 2019 at 8:59
  • 1
    As @nobalG said. Now, it seems like there is some flaw in this design. The method is static but the property no, so does this property hold any state? If you give us more context maybe we can help you better. Commented Oct 22, 2019 at 9:04
  • 1
    please check stackoverflow.com/questions/41861092/… Commented Oct 22, 2019 at 9:07

2 Answers 2

0

myVar is in scope of the class, therefore it requires an instance of your class to access it.

You can set your variable to be static like so.

export class AddressingService {
   static myVar;
   protected static getUserId() {
      this.myVar // property myVar doesn't exist on type 'typeof MyService'
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to change it to:

export class AddressingService {
   myVar;

   getUserId() {
      this.myVar;
   }
}

// Outside of the class you can access it by:
const serviceInstance = new AddressingService();
serviceInstance.getUserId();

Comments

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.