typescript 可选的通用类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37525094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:08:57  来源:igfitidea点击:

Optional generic type

genericstypescript

提问by Marius

I have the following logging method:

我有以下日志记录方法:

  private logData<T, S>(operation: string, responseData: T, requestData?: S) {
    this.logger.log(operation + ' ' + this.url);
    if (requestData) {
      this.logger.log('SENT');
      this.logger.log(requestData);
    }
    this.logger.log('RECEIVED');
    this.logger.log(responseData);
    return responseData;
  }

The requestDatais optional, I want to be able to call logDatawithout having to specify the Stype when I don't send the requestDatato the method: instead of: this.logData<T, any>('GET', data), I want to call this.logData<T>('GET', data).

requestData是可选的,我希望能够调用logData,而不必指定S类型时,我不送requestData的方法:不是:this.logData<T, any>('GET', data)我想打电话this.logData<T>('GET', data)

Is there a way to achieve this?

有没有办法实现这一目标?

采纳答案by FstTesla

As per TypeScript 2.2 (you can try it in the TS Playground), calling this.logData("GET", data)(with dataof type T) gets inferred succesfully as this.logData<T, {}>("GET", data).

根据 TypeScript 2.2(您可以在 TS Playground 中尝试),调用this.logData("GET", data)data类型为T)成功推断为this.logData<T, {}>("GET", data).

The overload suggested by David Bohunek can be applied if the inference fails with the TS version you use. Anyway, ensure that the second signature is before declared and then defined, otherwise it would not participate in the available overloads.

如果您使用的 TS 版本推理失败,则可以应用 David Bohunek 建议的重载。无论如何,确保第二个签名在声明和定义之前,否则它不会参与可用的重载。

// Declarations
private logData<T>(operation: string, responseData: T);
private logData<T, S>(operation: string, responseData: T, requestData?: S);
// Definition
private logData<T, S>(operation: string, responseData: T, requestData?: S) {
    // Body
}

回答by kimamula

As of TypeScript 2.3, you can use generic parameter defaults.

从 TypeScript 2.3 开始,您可以使用泛型参数 defaults

private logData<T, S = {}>(operation: string, responseData: T, requestData?: S) {
  // your implementation here
}

回答by David Bohunek

You can write the overloading method like this:

您可以像这样编写重载方法:

private logData<T>(operation: string, responseData: T);
private logData<T, S>(operation: string, responseData: T, requestData?: S) {
    this.logger.log(operation + ' ' + this.url);
    if (requestData) {
        this.logger.log('SENT');
        this.logger.log(requestData);
    }
    this.logger.log('RECEIVED');
    this.logger.log(responseData);
    return responseData;
}

But I don't think you really need it, because you don't have to write this.logData<T, any>('GET', data)instead just write this.logData('GET', data). The Ttype will be infered

但我不认为你真的需要它,因为你不必写this.logData<T, any>('GET', data)而只是写this.logData('GET', data)。该T类型将被infered