Consider the code snippet below:
class MyClass{
x: number;
}
const inst = new MyClass();
inst.x = 8;
inst.y = 9;
inst["z"] = 10;
The Typescript compiler is flagging an issue when adding the y
property to the instance of MyClass
, but not with the z
property.
Could this behavior be due to Typescript attempting to maintain consistency in the shape of all instances of a class, or could it be something else?
Interestingly, I've encountered a similar scenario while working with Angular components and trying to assign values to properties that were not previously defined. For example:
this.prop = 5;