简体   繁体   中英

Pass generic type as type parameter

I am struggling to come up with a solution to a problem i'm facing, where i am trying to use a generic type as a type parameter. I have the following two classes/types:

UserModel.ts:

export class UserModel{

  private _id : string;
  public get id() : string {
      return this._id;
  }
  public set id(v : string) {
      this._id = v;
  }

  ....
}

HttpResponse.ts:

export class HttpResponse<T>{

  private _success : boolean;
  public get success() : boolean {
      return this._success;
  }
  public set success(v : boolean) {
      this._success = v;
  }

  private _model : T;
  public get model() : T {
      return this._model;
  }
  public set model(v : T) {
      this._model = v;
  }
}

As you can guess, I am using this to have a generic type to handle http calls easily. The intended use is to call my http method with the HttpResponse type and whatever the expect result type is as the type parameter. For example, when making a user related http call the type parameter would be HttpResponse<UserModel>> or for a resource related call it would be HttpResponse<ResourceModel>> . However, i don't seem to be having any luck when trying this.

In my data service I have a method that POSTS to the server called 'create' and

create<T>(data: any){
  //Angular's HttpClient
  return this.http.post<T>(data, ...);
}

I then have a service that extends this class and overloads the create method with some extra bits before it calls super.create. I run into the issue at this point because I want to pass in HttpResponse<Type the service should return> which in the following case would be HttpResponse<UserModel> :

create<HttpResponse<UserModel>>(user: UserModel){
  //stuff happens here
  return super.create<HttpResponse<UserModel>>(user, ...);
}

However, this returns a syntax error at create<HttpResponse<UserModel>> . I had a look online and found another way to achieve this as something along the lines of:

create<HttpResponse, UserModel>(user: any){
  //stuff happens here
  return super.create<HttpResponse<UserModel>>(user, ...);
}

But, again, this returns an error stating "HttpResponse is not generic".

The design idea is that by passing in the types in this manner, the json response from the server can be automatically mapped into the appropriate type, making it simple to use the response throughout the application

Any pointers as to where I am going wrong in my design?

A subclass has to maintain the entire interface of its base class (with some exceptions that aren't relevant here). If your data service base class contains a create method that is generic and works for every T , then your subclass cannot override create with a method that only works for T = HttpResponse<UserModel> . Instead, consider (1) defining a new method with a different name or (2) moving the T parameter from the create method to the base class itself, so that when your subclass extends the base class, it can specify the single T that it works with. (Compare to this recent question .) If this doesn't seem to be what you're looking for, then please provide more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM