Problem
From TypeScript's documentation:
Overload Signatures and the Implementation Signature
This is a common source of confusion. Often people will write code like this and not understand why there is an error:
function fn(x: string): void;
function fn() {
// ...
}
// Expected to be able to call with zero arguments
fn();
^^^^
Expected 1 arguments, but got 0.
Again, the signature used to write the function body can’t be “seen” from the outside.
The signature of the implementation is not visible from the outside. When writing an overloaded function, you should always have two or more signatures above the implementation of the function.
The implementation signature must also be compatible with the overload signatures. For example, these functions have errors because the implementation signature doesn’t match the overloads in a correct way:
function fn(x: boolean): void;
// Argument type isn't right
function fn(x: string): void;
^^
This overload signature is not compatible with its implementation signature.
function fn(x: boolean) {}
function fn(x: string): string;
// Return type isn't right
function fn(x: number): boolean;
^^
This overload signature is not compatible with its implementation signature.
function fn(x: string | number) {
return "oops";
}
– TypeScript documentation on overload and implementation signatures
In your case you've defined the following overload signature:
static async myMethod(model: FooModel): Promise<BarResult>
But the implementation signature has no overlap. The first argument in your implementation signature is string while the overload is FooModel while the second argument in the implementation signature is string while the overload is undefined.
static async myMethod(inputText: string, outputText: string): Promise<BarResult>{
Solution
Turn your current implementation signature into an overload and add an implementation signature which is compatible with both your overloads:
class Foo {
static async myMethod(model: FooModel): Promise<BarResult>;
static async myMethod(inputText: string, outputText: string): Promise<BarResult>;
static async myMethod(modelOrInputText: string | FooModel, outputText?: string): Promise<BarResult>{
//implementation;
return new BarResult(); //Different content based on the inputs
}
}
– TypeScript Playground