I'm struggling with setting up class properties in TypeScript. Here is the TypeScript code that I am working on:
export class myClass {
a: number;
b: number;
public getCopy(anotherClass: myClass): myClass {
return {
a: anotherClass.a,
b: anotherClass.b
};
}
}
However, I encountered this error message:
Property 'getCopy' is missing in type '{ a: number; b: number; }' but required in type 'myClass'
Why is it interpreting getCopy() as a property? Coming from a C# background, I am used to not having to initialize functions. Is there a way in TypeScript to instantiate a class and set properties without initializing functions like C# does? I would prefer something simpler like the C# code snippet below:
var newInstance = new myClass()
{
a = 1,
b = 2
};
Instead of resorting to this approach:
var newInstance = new myClass();
newInstance.a = 1;
newInstance.b = 2;
Is achieving this elegance possible in TypeScript?