This is a simplified example:
class PersonParms{
name:string;
lastName:string;
age?:number;
get fullName(){return this.name + " " + this.lastName;}
}
class Person{
constructor(prms:PersonParms){
}
}
new Person({name:'John',lastName:'Doe'}) // ts error: Property 'fullName' is missing in type '{ name: string; lastName: string; }'.
The idea is to pass a literal object as the intizalizer of PersonParms but having that getter you can neither declare the getter optional or add the property to the object literal. Is there another way to achieve it?
I found this solution which is ok for me:
class Person {
name?:string;
lastName?:string;
age?: number;
fullName?:string;
constructor(public config: { name: string, lastName: string }) {
Object.defineProperty(this,'fullName',{
get(){return this.name + " " + this.lastName;}
});
}