Specialized signatures on subclasses without a different implementation

Topics: Language Specification
Jan 27, 2014 at 7:46 PM
Edited Jan 28, 2014 at 3:36 AM
Hi,

I’m currently trying to define a pattern for data models. In this code, due to platform restrictions (ES3), we use get and set methods to interact with properties. Specialised signatures enable us to do this and get correct typing, but an error “TS1041: Function implementation expected.” occurs when trying to subclass and define additional specialised signatures without a function implementation:
class Parent {
  get(key:string):any {
    return this[key];
  }
}

class Child extends Parent {
  get(key:'foo'):string;
  get(key:'bar'):number;
  get(key:string):any;
}
(This example is simplified, but you get the idea.)

In this case, the function implementation should be able to come from the parent class, but TS will complain. Trying to turn get into a property on the subclass to avoid the method checking results in a different error, “Class 'Child' defines instance member property 'get', but extended class 'Parent' defines it as instance member function.” The only solutions seem to be either this:
class Child extends Parent {
  get(key:'foo'):string;
  get(key:'bar'):number;
  get(key:string):any;
  get(key:string):any {
    return super.get(key);
  }
}
or this:
module Child {
  export interface Getters {
    get(key:'foo'):string;
    get(key:'bar'):number;
    get(key:string):any;
  }
}

class Child extends Parent implements Child.Getters {}
Is there a particular reason why the superclass’s implementation can’t/shouldn’t just be used automatically in a situation like this?