JavaScript not able to set property of interface type in typescript

I am beginner in typescript programming not able to set property interface type so please guide me about that.

interface addition { 
    num1: number;
    num2: number;
    add(num1:number, num2:number): number;
}

class Calculator implements addition{
    num1: number;
    num2: number;
    adds: addition;   //

    add(num1: number, num2: number): number { 
        return num1 + num2;
    }    

    sub(num1: number, num2: number): number { 
        let sk: addition = new Calculator()
        console.log(sk.add(2, 3)); 
        this.adds.num1=12;   // showing error here Uncaught TypeError: Cannot set property 'num1' of undefined
        console.log(this.adds.num1)
        return num1-num2
    }
}

let cal = new Calculator();
console.log(cal.sub(2, 3));
Answer:1

You did not initialize the adds field in the declaration. So before using it the method sub you need to initialize it:

 this.adds = //initialize with a value before using it in the next line.
 this.adds.num1=12;
Answer:2

It's not clear what the adds in Calculator is meant to be, but nothing in your code creates an addition object and assigns it to adds, so adds is undefined, and you can't access properties on undefined. Just declaring the property for it doesn't create it. You have to either create one or receive one. (But I suspect you don't want adds at all.)


FWIW:

The code in Calculator seems odd. It declares properties it never uses (and doesn't need, from the apparent API it provides). What are the adds, num1, and num2 properties for? You're accepting the numbers as parameters to add and sub, and Calculator itself is the addition object.

Without those properties, it would be rather simpler:

interface Addition { 
    add(num1:number, num2:number): number;
}
class Calculator implements addition {
    add(num1: number, num2: number): number { 
        return num1 + num2;
    }    

    sub(num1: number, num2: number): number { 
        return num1 - num2;
    }
}
Answer:3

I have a HTML import element, which is a DOM element, imported via: <link rel="import" src="element.html"> Now i want to update the custom element without reloading the site. //example method ...

I have a HTML import element, which is a DOM element, imported via: <link rel="import" src="element.html"> Now i want to update the custom element without reloading the site. //example method ...

  1. polymer update element
  2. polymer refresh element
  3. polymer change element property

I'm trying to write a curried function dissoc, that takes in a prop and an object and removes the key and its value from the object. Here is the code that works: const dissoc = prop => obj => {...

I'm trying to write a curried function dissoc, that takes in a prop and an object and removes the key and its value from the object. Here is the code that works: const dissoc = prop => obj => {...

  1. javascript object destructuring rename
  2. javascript object destructuring default value
  3. javascript object destructuring nested
  4. javascript object destructuring assignment
  5. javascript object destructuring alias
  6. javascript object destructuring undefined
  7. javascript object destructuring reassign
  8. javascript object destructuring rest
  9. javascript object destructuring null
  10. javascript object destructuring array
  11. javascript object destructuring into new object
  12. javascript object destructuring argument
  13. javascript object destructuring constructor
  14. javascript object destructuring function arguments
  15. javascript object destructuring remove property
  16. javascript object destructuring parameters
  17. javascript object destructuring function parameters
  18. javascript object destructuring dynamic key
  19. javascript object destructuring deep
  20. javascript object destructuring example

I would like to know how to disable unselected checkboxes using jQuery. The goal is to disable the unselected checkboxes if the number of selected checkboxes is greater than or equal to 3. $('....

I would like to know how to disable unselected checkboxes using jQuery. The goal is to disable the unselected checkboxes if the number of selected checkboxes is greater than or equal to 3. $('....

  1. disable unchecked checkbox jquery
  2. disable unchecked checkbox javascript
  3. unchecked disabled checkbox

I have developed this neat little piece of code which given an element and a properly formatted table, creates a dropdown menu on the element which allows the user to toggle view of columns. I cannot ...

I have developed this neat little piece of code which given an element and a properly formatted table, creates a dropdown menu on the element which allows the user to toggle view of columns. I cannot ...

  1. javascript html table to csv
  2. javascript html table library
  3. javascript html table to json
  4. javascript html table to excel
  5. javascript html table to array
  6. javascript html table sort
  7. javascript html table filter
  8. javascript html table pagination
  9. javascript html table add row
  10. javascript html table search
  11. javascript html table example
  12. javascript html table generator
  13. javascript html table insert row
  14. javascript html table column width
  15. javascript html table to xlsx
  16. javascript html table to pdf
  17. javascript html table to excel xlsx
  18. javascript html table delete all rows
  19. javascript html table
  20. javascript html table export to excel