1

I have the following in my Angular component:

class Test implements ....{
  duration:{from:number, to:number}

  constructor(){
    this.duration.from = "ddd";//set after some calculations
     this.duration.to = "ddd";
  }
}

The above returns an error of "cannot set property from of undefined".

Where am I going wrong?

2
  • 1
    You did declare that duration was of type {from:number, to:number}. That does not initialize it. this.duration is undefined at the time the constructor runs. Use this.duration = {from: 'ddd', to: 'ddd'} to initialize it. Commented Feb 18, 2018 at 20:26
  • Hi, try declare duration = {...} Commented Feb 18, 2018 at 20:27

1 Answer 1

5

In your class you define variable duration and specify it's type, however you do not initialize it, so the value of the variable remains undefined. Instead of assigning properties this.duration.from and this.duration.to, you should initialize variable:

class Test implements ....{
    duration:{from:number, to:number}

    constructor(){
        this.duration = {
            from: 1,
            to: 2
        };
    }
}
Sign up to request clarification or add additional context in comments.

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.