If I were writing this in JavaScript, it would look like:
function a(b,c) {this.foo = b; this.bar = c; this.yep = b+c}
// undefined
b = new a(1,2)
// a {foo: 1, bar: 2, yep: 3}
However, I've been struggling to achieve the same in TypeScript. None of the following approaches seem to work:
class A {
foo: number;
bar: number;
yep: foo + bar;
}
class A {
foo: number;
bar: number;
yep: this.foo + this.bar;
}
class A {
foo: number;
bar: number;
let yep:number = this.foo + this.bar;
}
class A {
foo: number;
bar: number;
yep: number;
constructor() {
this.yep = this.foo + this.bar;
}
}
class A {
foo: number;
bar: number;
get yep(): number {
return this.foo + this.bar;
}
}
class A {
foo: number;
bar: number;
yep: function () {return this.get("foo") + this.get("bar")};
}
I tried initializing it as follows:
somevar: A = {
foo: 1,
bar: 2
}
Additionally, I experimented with this approach:
somevar: A = {
foo: 1,
bar: 2,
this.yep: this.foo + this.bar
}
Your assistance on resolving this matter is greatly appreciated. The complexity of the calculation requires a more efficient solution that can be reused without embedding it directly into the template.