Consider this initial base interface and abstract class setup:
export interface IPersonViewModel {
name: string;
setName(value: string): IPersonViewModel;
}
export abstract class PersonViewModel implements IPersonViewModel {
constructor(name: string) {
this.name = name;
}
public name: string;
public setName(value: string): IPersonViewModel {
const vm = { ...this, name: value };
return vm;
}
}
Now, let's introduce a derived class and interface to the scenario:
export interface IEmployeeViewModel extends IPersonViewModel {
role: string;
}
export class EmployeeViewModel extends PersonViewModel implements IEmployeeViewModel {
constructor(role: string, name: string) {
super(name);
this.role = role;
}
public role: string;
public setRole(value: string): IEmployeeViewModel {
const vm = { ...this, role: value };
return vm;
}
}
The issue arises when using setName on an instance of IEmployeeViewModel as it returns the base type IPersonViewModel. Is there a solution involving generics or other methods to ensure that the return type of IEmployeeViewModel.setName is IEmployeeViewModel?