Let's say I have a very simple class with a computed property:
class Person { firstName: string; lastName: string; get fuillName(): string { return this.firstName + ' ' + this.lastName; } }
Now I want to create an object of type Person :
let p: Person = { firstName: 'John', lastName: 'Smith' };
This gives me an error:
Enter '{firstName: string; lastName: string; } 'is not assigned to the type' Person '. The 'fuillName' property is not in the type '{firstName: string; lastName: string; } ".
A? fullName is a read-only property. So I followed this question and implemented a partial initializer:
constructor(init?: Partial<Person>) { Object.assign(this, init); }
The same mistakes. I know I can do this:
let p = new Person(); p.firstName = 'John'; p.lastName = 'Smith'; console.debug(p.fullName);
But is there a shorthand for initializing a class using JSON syntax?
source share