Próbuję przepisać istniejącą klasę na klasę abstrakcyjną i docelową. Punkt wyjścia jest taki:
export var BooksManager = class {
state: booksStateInterface;
constructor(state: booksStateInterface) {
this.state = state;
}
_remove(id: string) {
this.state.data = tableHelpers.remove([...this.state.data], id);
return this;
}
_filter(payload?: FiltrationObjectType) {
if (payload) this.state.filter = payload;
this.state.data = tableHelpers.filtrate([...this.state.data], this.state.filter);
return this;
}
Remove(payload: string) {
this._remove(payload)._filter()._sort()._changePage()._setNumberOfPages();
}
Filter(payload: FiltrationObjectType) {
this._filter(payload)._sort()._changePage()._setNumberOfPages();
}
}
To wersja skrócona W każdym razie mamy metody "_" cząstkowe i zaczynające się od dużej litery docelowe (publiczne, ale póki co dla uproszczenia tego nie piszę), które stosują chaining na cząstkowych. Chodzi o wyekstrahowanie publicznych do klasy abstrakcyjnej.
Abstrakcyjna:
abstract class Manager {
abstract _remove: (id: string) => typeof this;
abstract _filter: (payload?: FiltrationObjectType) => typeof this;
Remove(payload: string) {
this._remove(payload)._filter()._sort()._changePage()._setNumberOfPages();
}
Filter(payload: FiltrationObjectType) {
this._filter(payload)._sort()._changePage()._setNumberOfPages();
}
}
"Ostateczna"
export class BookMan extends Manager {
state: booksStateInterface;
constructor(state: booksStateInterface) {
super()
this.state = state;
}
_remove(id: string) {
this.state.data = tableHelpers.remove([...this.state.data], id);
return this;
}
_filter(payload?: FiltrationObjectType) {
if (payload) this.state.filter = payload;
this.state.data = tableHelpers.filtrate([...this.state.data], this.state.filter);
return this;
}
}
W 'ostatecznej' przy obu metodach występują błędy:
> Class 'Manager' defines instance member property '_filter', but extended class 'BookMan' defines it as instance member function.
> Class 'Manager' defines instance member property '_sort', but extended class 'BookMan' defines it as instance member function
I nie bardzo rozumiem dlaczego? Miałem watpliwości kiedy w abstrakcyjnej pisałem typ wartości zwracanej dla obu metod
> typeof this;
Ale nic lepszego nie przychodziło mi do głowy. Co tu jest nie tak?