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));
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;
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;
}
}
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 ...
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 => {...
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. $('....
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 ...