简体   繁体   English

传递通用类型作为类型参数

[英]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: 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: 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. 如您所料,我使用它来具有一个通用类型来轻松处理http调用。 The intended use is to call my http method with the HttpResponse type and whatever the expect result type is as the type parameter. 预期的用途是使用HttpResponse类型以及期望的结果类型作为type参数调用我的http方法。 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>> . 例如,当进行与用户相关的http调用时,类型参数将为HttpResponse<UserModel>>对于与资源相关的调用,其参数将为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”,然后将其发布到服务器。

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. 然后,我有一个扩展此类的服务,并在调用super.create之前将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> : 我此时遇到了这个问题,因为我想传递HttpResponse<Type the service should return> ,在以下情况下为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>> . 但是,这将在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". 但是,这再次返回一个错误,指出“ HttpResponse不是通用的”。

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 设计思想是,通过以这种方式传递类型,可以将来自服务器的json响应自动映射为适当的类型,从而简化了在整个应用程序中使用响应的过程

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> . 如果您的数据服务基类包含通用的create方法并且适用于每个 T ,则您的子类无法使用仅适用于T = HttpResponse<UserModel>的方法覆盖create 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. 而是考虑(1)定义一个具有不同名称的新方法,或(2)将T参数从create方法移至基类本身,以便当您的子类扩展基类时,它可以指定单个T使用。 (Compare to this recent question .) If this doesn't seem to be what you're looking for, then please provide more information. (与最近的问题进行比较。)如果这不是您要找的内容,请提供更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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