简体   繁体   中英

Typescript Method overloading for different type of parameters but same response

I need to overload a method using TypeScript.

FooModel has 6 parameters, but 2 string parameters are the only mandatory params. So instead of creating a FooModel every time I want to use the myMethod , I want to overload myMethod and create the FooModel once in there and then the rest of the logic before return.

I've tried this based on what I found so far online, but it I get the following error:

TS2394: This overload signature is not compatible with its implementation signature.

The solutions for this error is not compatable with my method

    static async myMethod(model: FooModel): Promise<BarResult>
    static async myMethod(inputText: string, outputText: string): Promise<BarResult>{
         //implementation;
      return new BarResult(); //Different content based on the inputs
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM