Issue
An error occurs when attempting to call an overload method for a specific function. The call only works for the first defined overload, causing an error if the call is made to the second overload with mismatched typing compared to the first overload definition.
Scenario
I have created an AxiosWrapper
Class to implement some overloaded methods. The most recent function prototype of Axios is at the end.
export interface MeiliAxiosWrapperInterface {
post(
url: string,
data: IndexRequest,
): Promise<IndexResponse>
post<T = any, R = AxiosResponse<EnqueuedUpdate>>(
url: string,
data?: T,
): Promise<R>
}
// axios-wrapper.ts
import * as Types from './types'
class AxiosWrapper implements Types.MeiliAxiosWrapper {
post(
url: string,
data: Types.IndexRequest,
): Promise<Types.IndexResponse>
post<T = any, R = AxiosResponse<Types.EnqueuedUpdate>>(
url: string,
data?: T,
): Promise<R> {
return this.instance.post(url, data, config) // this.instance is an axios instance
}
}
Success Story
This implementation functions correctly when used in this context (the class where this method exists extends the AxiosWrapper, allowing this.post to leverage the AxiosWrapper):
class Client extends AxiosWrapper {
//...
async createIndex(data: Types.IndexRequest): Promise<Indexes> {
const url = `/indexes`
const index = await this.post(url, data);
return new Indexes(this.config, index.uid)
}
}
Challenges Encountered
In this scenario, there was success using the default axios prototype until an overload was added, resulting in failure:
class Indexes extends AxiosWrapper implements Types.Indexes {
//...
addDocuments(
documents: Types.Document[],
): Promise<Types.EnqueuedUpdate> {
const url = `/indexes/${this.indexUid}/documents`
return this.post(url, documents)
}
}
The following error occurred:
Argument of type 'Document<any>[]' is not assignable to parameter of type 'IndexRequest'.
Property 'uid' is missing in type 'Document<any>[]' but required in type 'IndexRequest'.