是否可以在不指定签名的情况下重载函数?

时间:2019-02-04 15:30:14

标签: typescript

下面是我拥有的重载函数。

是否可以在不指定每个签名的情况下获得相同(或更优)的结果?

export function request(this: Client, body: RequestBody.CreateProject) : Promise<ServerResponse.CreateProject>
export function request(this: Client, body: RequestBody.GetArcs) : Promise<ServerResponse.GetArcs>
export function request(this: Client, body: RequestBody.OpenProject) : Promise<ServerResponse.OpenProject>
export function request(this: Client, body: RequestBody.SetAllMain) : Promise<ServerResponse.SetAllMain>
export function request(this: Client, body: RequestBody.GetArcId) : Promise<ServerResponse.GetArcId>
export function request(this: Client, body: RequestBody.GetActiveProject) : Promise<ServerResponse.GetActiveProject>
export function request(this: Client, body: RequestBody.ArcIsConnected) : Promise<ServerResponse.ArcIsConnected>
export function request(this: Client, body: RequestBody.ArcSetMain) : Promise<ServerResponse.ArcSetMain>
export function request(this: Client, body: RequestBody.ArcCalibrate) : Promise<ServerResponse.ArcCalibrate>
export function request(this: Client, body: RequestBody.ArcEnable5V) : Promise<ServerResponse.ArcEnable5V>
export function request(this: Client, body: RequestBody.ArcEnableChannel) : Promise<ServerResponse.ArcEnableChannel>
export function request(this: Client, body: RequestBody.ArcEnableExpPort) : Promise<ServerResponse.ArcEnableExpPort>
export function request(this: Client, body: RequestBody.ArcEnableUart) : Promise<ServerResponse.ArcEnableUart>
export function request(this: Client, body: RequestBody.ArcGetAdcResistor) : Promise<ServerResponse.ArcGetAdcResistor>
export function request(this: Client, body: RequestBody.ArcGetExpVoltage) : Promise<ServerResponse.ArcGetExpVoltage>
export function request(this: Client, body: RequestBody.ArcGetGpi) : Promise<ServerResponse.ArcGetGpi>
export function request(this: Client, body: RequestBody.ArcGetMainVoltage) : Promise<ServerResponse.ArcGetMainVoltage>
export function request(this: Client, body: RequestBody.ArcGetMaxCurrent) : Promise<ServerResponse.ArcGetMaxCurrent>
export function request(this: Client, body: RequestBody.ArcGetRange) : Promise<ServerResponse.ArcGetRange>
export function request(this: Client, body: RequestBody.ArcGetRx) : Promise<ServerResponse.ArcGetRx>
export function request(this: Client, body: RequestBody.ArcGetSupplyUsedCapacity) : Promise<ServerResponse.ArcGetSupplyUsedCapacity>
export function request(this: Client, body: RequestBody.ArcGetSupplySocTracking) : Promise<ServerResponse.ArcGetSupplySocTracking>
export function request(this: Client, body: RequestBody.ArcGetSupply) : Promise<ServerResponse.ArcGetSupply>
export function request(this: Client, body: RequestBody.ArcGetSupplies) : Promise<ServerResponse.ArcGetSupplies>
export function request(this: Client, body: RequestBody.ArcGetUartBaudrate) : Promise<ServerResponse.ArcGetUartBaudrate>
export function request(this: Client, body: RequestBody.ArcGetValue) : Promise<ServerResponse.ArcGetValue>
export function request(this: Client, body: RequestBody.ArcGetVersion) : Promise<ServerResponse.ArcGetVersion>
export function request(this: Client, body: RequestBody.ArcSetAdcResistor) : Promise<ServerResponse.ArcSetAdcResistor>
export function request(this: Client, body: RequestBody.ArcSetExpVoltage) : Promise<ServerResponse.ArcSetExpVoltage>
export function request(this: Client, body: RequestBody.ArcSetGpo) : Promise<ServerResponse.ArcSetGpo>
export function request(this: Client, body: RequestBody.ArcSetMainVoltage) : Promise<ServerResponse.ArcSetMainVoltage>
export function request(this: Client, body: RequestBody.ArcSetMaxCurrent) : Promise<ServerResponse.ArcSetMaxCurrent>
export function request(this: Client, body: RequestBody.ArcSetRange) : Promise<ServerResponse.ArcSetRange>
export function request(this: Client, body: RequestBody.ArcSetSupplyUsed) : Promise<ServerResponse.ArcSetSupplyUsed>
export function request(this: Client, body: RequestBody.ArcSetSupplySocTracking) : Promise<ServerResponse.ArcSetSupplySocTracking>
export function request(this: Client, body: RequestBody.ArcSetSupply) : Promise<ServerResponse.ArcSetSupply>
export function request(this: Client, body: RequestBody.ArcSetTx) : Promise<ServerResponse.ArcSetTx>
export function request(this: Client, body: RequestBody.ArcSetUartBaudrate) : Promise<ServerResponse.ArcSetUartBaudrate>
export function request(this: Client, body: RequestBody.ArcWriteTx) : Promise<ServerResponse.ArcWriteTx>
export function request(this: Client, body: RequestBody.ProjectClose) : Promise<ServerResponse.ProjectClose>
export function request(this: Client, body: RequestBody.ProjectCropData) : Promise<ServerResponse.ProjectCropData>
export function request(this: Client, body: RequestBody.ProjectGetLastRecording) : Promise<ServerResponse.ProjectGetLastRecording>
export function request(this: Client, body: RequestBody.ProjectGetRecordings) : Promise<ServerResponse.ProjectGetRecordings>
export function request(this: Client, body: RequestBody.ProjectSave) : Promise<ServerResponse.ProjectSave>
export function request(this: Client, body: RequestBody.ProjectStartRecording) : Promise<ServerResponse.ProjectStartRecording>
export function request(this: Client, body: RequestBody.ProjectStopRecording) : Promise<ServerResponse.ProjectStopRecording>
export function request(this: Client, body: RequestBody.RecordingDelete) : Promise<ServerResponse.RecordingDelete>
export function request(this: Client, body: RequestBody.RecordingIsRunning) : Promise<ServerResponse.RecordingIsRunning>
export function request(this: Client, body: RequestBody.RecordingRename) : Promise<ServerResponse.RecordingRename>
export function request(this: Client, body: RequestBody.RecordingDownsampleChannel) : Promise<ServerResponse.RecordingDownsampleChannel>
export function request(this: Client, body: RequestBody.RecordingGetChannelDataCount) : Promise<ServerResponse.RecordingGetChannelDataCount>
export function request(this: Client, body: RequestBody.RecordingGetChannelDataIndex) : Promise<ServerResponse.RecordingGetChannelDataIndex>
export function request(this: Client, body: RequestBody.RecordingGetChannelData) : Promise<ServerResponse.RecordingGetChannelData>
export function request<T extends RequestBody.IRequestBody>(this: Client, body: T) : Promise<ServerResponse.Generic> {
    ...
}

1 个答案:

答案 0 :(得分:1)

假设RequestBodyServerResponse接口具有相同的键,则可以执行以下操作:

export function request<T extends keyof RequestBody>
    (this: Client, body: RequestBody[T]): Promise<ServerResponse[T]> {
    ...
}