Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

problems with multiple levels of inheritance in typescript

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One easy way out might be to just remove the override of find in UserStoreService. But the real problem is that the declaration of find in BaseApi is probably not what you intend. It means that every BaseApi instance (including subclass instances) must support find for every type T. You probably want to make T a type parameter of BaseApi (i.e., BaseApi<T>) and remove the <T> from the find method so that each subclass can specify the single type T that it supports. For example, UserApi would extend BaseApi<User>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...