I'm using a typescript library of classes for api access, which has been automatically generated by a toolset that defines a class ("UserApi") having specific methods extending a "base" class ("BaseApi") that has generic methods. There is also a model interface "User";
These classes are using generics to ensure / keep type safety.
I have now a need to create a third class (one of my own, not part of the library) that itself extends "UserApi" .
The first problem I ran into is that I was wanting to override the `find" method (defined in "BaseApi")
so, in my class "MyClass" I have
import {User} from 'sdk';
export class UserStoreService extends UserApi {
constructor(
) {
super();
}
public find(): Observable<User[]> {
return super.find();
}
}
the definition of the find
method in the "BaseApi" class is
public find<T>(): Observable<T[]> {
return this.request(
I get a syntax error
Property 'find' in type 'UserStoreService' is not assignable to the same property in base type 'UserApi'. Type '() => Observable<User[]>' is not assignable to type '<T>() => Observable<T[]>'.
Type 'Observable<User[]>' is not assignable to type 'Observable<T[]>'.
Type 'User[]' is not assignable to type 'T[]'.
Type 'User' is not assignable to type 'T'.
so I thought I'd change the super.find()
to be super.find<User>()
but got the same error
how can I make these function calls match ?
The second question is about the levels of class extending going on here. The thought has struck me that if I can modify the templates used by the library, I could actually just generate all of the code into a single, model-specific class, not 3.
I see several advantages
- less files to deal with
- less generics to deal with
- much easier code to read and maintain
What is the downside to this ?
- increase in package size (the base class is 29k (!)) - so each model file would be increased by this amount
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…