I really enjoy the strictness of Typescript when defining classes and the types of every object used, but I've lately been confronted with something I would like to be DRYer:
I've got a class definition that use and create specific objects for let's say a ClientsDB:
class ClientsDB {
constructor(name: string) {
this.DB = new Database(name);
}
DB: Database;
replaySubject = new ReplaySubject<Client[]>(1);
up() {
fromPromise(this.DB.getDocs<Client>())
.subscribe((clients) => this.replaySubject.next(clients));
}
subscribe(callback: (value: Client[]) => void) {
return this.replaySubject.subscribe(callback);
}
}
The thing is I want to use the same type of class for a ProductsDB that would exactly be the same definition in plain JavaScript but would use different types as:
class ProductsDB {
constructor(name: string) {
this.DB = new Database(name);
}
DB: Database;
replaySubject = new ReplaySubject<Product[]>(1);
up() {
fromPromise(this.DB.getDocs<Product>())
.subscribe((products) => this.replaySubject.next(products));
}
subscribe(callback: (value: Product[]) => void) {
return this.replaySubject.subscribe(callback);
}
}
How could I get only one class definition but with using the same rigor with these types definition?
.pipe(map(...))is only in the first one.