I am a beginner in TypeScript and I have defined some classes. These classes are being used as types for the properties of other classes, like so:
FileOne.ts
export class A {
propertyOne: string;
propertyTwo: string;
}
Now, in another file (filetwo.ts), I have another class B:
FileTwo.ts
import { A } from './FileOne';
export class B {
myProperty: A;
mySecondProperty: string;
}
I have created an instance of class B in a different file with the following code:
MyApp.ts
import { B } from './FileTwo';
export class C {
let myObj: B = new B();
myObj.myProperty.propertyOne = 'hello';
myObj.myProperty.propertyTwo = 'world'';
console.log(myObj);
}
However, I encountered an error when trying to set the property of A through B:
Cannot set the property "propertyOne" of undefined
I'm confused as I expected this behavior to be similar to Java. Could you please explain why I am facing this issue and suggest the correct approach? I would appreciate not just a solution to the problem, but also an explanation.