Can the process described in this answer be achieved using Typescript?
Subclassing a Java Builder class
This is the base class I have implemented so far:
export class ProfileBuilder {
name: string;
withName(value: string): ProfileBuilder {
this.name= value;
return this;
}
build(): Profile{
return new Profile(this);
}
}
export class Profile {
private name: string;
constructor(builder: ProfileBuilder) {
this.name = builder.Name;
}
}
And here is the extended class:
export class CustomerBuilder extends ProfileBuilder {
email: string;
withEmail(value: string): ProfileBuilder {
this.email = value;
return this;
}
build(): Customer {
return new Customer(this);
}
}
export class Customer extends Profile {
private email: string;
constructor(builder: CustomerBuilder) {
super(builder);
this.email= builder.email;
}
}
Similar to what was mentioned in another discussion, building a Customer instance in this order won't work due to the change of context:
let customer: Customer = new CustomerBuilder().withName('John')
.withEmail('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86ece9eee8c6e3ebe7efeaa8e5e9eb">[email protected]</a>')
.build();
I am currently exploring the use of generics to resolve this issue, but encountering difficulties when returning the 'this' pointer in my setter methods (type this is not assignable to type T). Any suggestions?