【问题标题】:Is it possible to set a default value for an argument in typescript when the type of the argument is a generic?当参数的类型是泛型时,是否可以在打字稿中为参数设置默认值?
【发布时间】:2019-10-15 06:59:26
【问题描述】:

我正在编写泛型方法,以便在不同的前端应用程序中使用它们,其想法是能够调用函数.postAsync<CustomModel>('www.mysite.com',..., CustomModel);,并且预期的响应是一个CustomModel对象。

我希望能够为第二个参数设置一个默认值,以便默认情况下该值将是不同的模型,并且可以在需要时被覆盖。

如何为 Constructable<T> 类型的参数设置默认值,其中接口 Constructable 表示

interface Constructable<T> { new(params : any): T ;}

我尝试设置一个带有参数的接口的默认值并将参数设置为不同的类型,但我总是收到错误Type Constructable&lt;CustomModel&gt; is not assignable to type Constructable&lt;T&gt;。我还在 CustomModel 通用方法中设置了默认类型 T ,然后尝试了这个并得到了同样的错误。

interface Constructable<T> { new(params : any): T ;}


export default class WebapiBase {

    static async postAsync<T = CustomModel>(
        uri: string,
        body: object,
        headers: CustomHeaders = new CustomHeaders(),
        // This is the part giving errors
        model: Constructable<T> = <Constructable<CustomModel>>,): Promise<T> {
        return this.requestAsync<T>(model, HTTP_METHOD.POST, uri, headers, body);
    }

    private static async requestAsync<T>(
        model: Constructable<T>,
        method: HTTP_METHOD,
        uri: string,
        headers: CustomHeaders,
        body?: object): Promise<T> {
        const url = new URL(uri, window.location.origin);
        const request = this.buildRequest(url, method, headers, body);
        const bodyResponse = await fetch(request)
            .then(response => this.validate(response, request.headers))
            .then(validResponse => this.extractBody(validResponse))
            // Here is where the response body is being used to initialise one of the custom models that we are passing in. Code below
            .then(extractedBody => this.buildModel<T>(model, extractedBody))
            .catch((error) => { throw this.handleError(error); });
        return bodyResponse;
    }

    private static buildModel<T>(
        Model: Constructable<T>,
        params: ResponseBody,
    ): T {
        return new Model(params);
    }
}

我希望我不必将模型传递给方法postAsync(),并且它总是会返回一个 CustomModel 对象。但实际上我得到了这个错误Type Constructable&lt;CustomModel&gt; is not assignable to type Constructable&lt;T&gt;

Example in Playground, hover over Constructable in args to see error

【问题讨论】:

  • 对于有问题的代码来说,minimal reproducible example 通常是一个好主意,这样有人可以直接进入 IDE 并重现问题,而不会产生无关的依赖或错误......理想情况下甚至会有一个链接到像 the Playground 这样的 Web IDE。很难提出已知可分配给泛型类型的具体类型。不能保证CustomModel 可以分配给Tdefault 只是一个默认值,而不是保证),所以当你需要返回一个CustomModel 时返回一个类型错误T.
  • 那是真的,我真的很抱歉。这是一个非常复杂的例子,明天早上我会将我的问题抽象为一个最小的可重现的例子。谢谢。
  • 在问题中添加了一个链接到我刚刚启动的操场示例,它不是“正在运行”,但它确实得到了相同的错误,或者至少是我在摆弄这个问题时遇到的错误之一.很快就会变得更简洁。谢谢

标签: typescript generics casting


【解决方案1】:

我解决了这个问题,即在未传递模型的情况下返回默认值(在本例中为通用对象)。 一般来说,修复是:

  • 从将模型类型化为Constructable 接口改为使用Constructable&lt;T&gt; | object 的联合类型。
  • 更改将模型传入对象的默认设置。
  • buildModel() 函数检查模型的类型,如果是对象,则返回传入的值,如果是可构造的,则创建它的实例。

查看它是如何在postAsync 参数、ResponseModel 接口和buildModel 方法中实现的requestAsync

interface Constructable<T> { new(params : any): T ;}
type ResponseModel<T> = Constructable<T> | Object;

export default class WebapiBase {
  static async postAsync<T = Object>(
      {
          uri = '',
          body = {},
          headers = new CustomHeaders(),
      }: PostRequestParams = {},
      // Here is where the defualt is implemented
      model: ResponseModel<T> = Object): Promise<T> {
      return this.requestAsync(model, HTTP_METHOD.POST, uri, headers, body);
  }
  private static async requestAsync<T = Object>(
      // This param accepts either a class or object
      model: ResponseModel<T>,
      method: HTTP_METHOD,
      uri: string,
      headers: CustomHeaders,
      body?: object): Promise<T> {
      const url = new URL(uri, window.location.origin);
      const request = this.buildRequest(url, method, headers, body);
      const bodyResponse = await fetch(request)
          .then(response => this.validate(response, request.headers))
          .then(validResponse => this.extractBody(validResponse))
          // Class instance or object returned here
          .then(extractedBody => this.buildModel(model, extractedBody))
          .catch((error) => { throw this.handleError(error); });
      return bodyResponse;
  }

  // Method that conditionally creates an instance of the passed in model
  // Or returns the object passed in if no model specified
  private static buildModel<T = Object>(
        Model: ResponseModel<T>,
        params: any,
    ): T {
        if (typeof Model === 'object') return params;
        return new Model(params);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多