As someone who is new to TypeScript, I have a question regarding defining properties. In JavaScript, we can define a property dynamically like this:
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
However, in TypeScript, it doesn't work the same way:
class Rectangle {
constructor(height:number, width:number) {
this.height = height; //error
this.width = width; //error
}
}
I understand that adding an access identifier such as public
before the parameters in the constructor like this:
...
constructor(public height:number, public width:number) {...} //which creates declaration automatically
will solve the issue. But my question is, since TypeScript is supposed to be a superset of JavaScript, shouldn't it support all valid JavaScript syntax as well?