Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript overloading class methods - same return type, different parameters

I've got a typescript class:

class ContactModel {

    public getUsage(type: string): restangular.IElement {
      return this.getBase().one('usages', type);
    }

    public getUsage(customerId: number, type: string): restangular.IElement {
      return this.ModelFactory.createRequestMapper(ContactModel.options)
        .one('customers', customerId).all('contacts/usages', type);
    }

    //...
}

which causes the compiler to throw following error:

>> app/modules/common/model/ContactModel.ts(27,12): error TS2393: Duplicate function implementation.
>> app/modules/common/model/ContactModel.ts(31,12): error TS2393: Duplicate function implementation.

The only difference I see between this example and the TypeScript Handbook is that their examples have different return types and I've got the same return types (both cases have different input parameters).

The question is: what am I doing wrong - or do typescript class methods need to have different method argument types to allow overloading? That seems stupid, since both .Net and Java support overloading with same return types and different input types.

like image 357
ducin Avatar asked Aug 04 '15 13:08

ducin


People also ask

Can we overload same method with different return type?

No, you cannot overload a method based on different return type but same argument type and number in java. same name. different parameters (different type or, different number or both).

What will happen if overloaded functions have same arguments but different return type?

The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types. It will throw a compile-time error. If both methods have the same parameter types, but different return types, then it is not possible.

Is method overloading possible in TypeScript?

TypeScript provides the concept of function overloading. You can have multiple functions with the same name but different parameter types and return type. However, the number of parameters should be the same.

Can we overload a method with different return type in CPP?

You cannot overload function declarations that differ only by return type. The function overloading is basically the compile time polymorphism. It checks the function signature.


1 Answers

JavaScript doesn't do runtime type information, so you have to do overload disambiguation yourself. Note that in the example in the Handbook, there's only one function implementation, whereas you have two.

class ContactModel {
  public getUsage(type: string): restangular.IElement;
  public getUsage(customerId: number, type: string): restangular.IElement;
  public getUsage(typeOrCustomerId: string|number, type?: string): restangular.IElement {
    if (typeof typeOrCustomerId === 'string') {
      // First overload
      return this.getBase().one('usages', type);
    } else {
      // Second overload
      return this.ModelFactory.createRequestMapper(ContactModel.options)
        .one('customers', customerId).all('contacts/usages', type);
    }
  }
}
like image 197
Ryan Cavanaugh Avatar answered Sep 22 '22 23:09

Ryan Cavanaugh