I have a problem with TypeScript syntax in my Angular 4 project. If I create a normal class, such as:
export class MyClass {
field1: string;
field2: number;
field3: boolean;
myMethod() {
this.field2 = this.field2 + 2;
}
}
I can't declare an array of MyClass elements with this syntax:
myclassarray: MyClass[] = [{
field1: 'hello',
field2: 3,
field3: false
}];
Because in my class there is an instance method and not a static method. The question is: how can I declare an array of elements that also include non-static methods that are useful to me?
I also don't understand the syntax that allows you to initialize these items in a declarative way.
new MyClass()syntax. If you want to initialize properties - use constructor. If you have "too many" properties - use constructor that accepts interface (constructor(dto: MyClassInterface){ this = Object.assign({}, dto);}).