Assume we have the following class:
class A {
private maxValue = 900;
public minValue = 800;
someMethod() {}
}
And I want to create a plain object with the class type definition:
const objA: A = {
minValue: 200,
maxValue: 2313,
someMethod() {}
};
And I'm getting this error:
Property 'maxValue' is private in type 'A' but not in type '{ minValue: number; maxValue: number; someMethod(): void; }
The question is: Does it make sense to create objects in this way? or Should I always stick with new A()
I don't think it's an ideal way to define an object with this type. If you want to define an object like this, use an interface instead of a class. In case of creating new object regarding class A, new A() is a better solution for creating a new object.
No, it does not make too much sense. If you check objA instanceof A it will return false, although your code states it would be an instance of class A. And as you can see a plain object can not have private properties whereas a class instance can. So better stick to new A() or use interfaces.