I am new to typescript and was playing with compilation. Below file after compilation shows two assignments in js file, why is it so?
input file
class Student {
constructor(public fname: string){
this.fname = fname;
}
}
output content
var Student = /** @class */ (function () {
function Student(fname) {
this.fname = fname;
this.fname = fname;
}
return Student;
}());
TypeScript includes a concise way to create and assign a class instance property from a constructor parameter.
Which means, rather than:
class TestClass {
public name: string;
constructor(name: string) {
this.name = name;
}
}
one can use:
class TestClass {
constructor(public name: string) { }
}
In your code snippet you are mixing both the syntax which is resulting in double assignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With