This project is read-only.

overloading in an interface?

Topics: General, Language Specification
Oct 4, 2012 at 2:59 AM

Hello, I am trying to describe the Knockout observableArray and I am trying to figure out how to describe its remove function.. the Interface wont let me overload even though the two remove(s) have different signitures..

here is as far as I have gotten with this interface.

interface observableArrayAny {
    (newValue: any[]): observableArrayAny;
    (): any[];
    subscribe: (newValue: any[]) => void;
    indexOf: (search: any) => any;
    slice: (start: number, count: number) => any[];
    push: (item: any) => void;
    pop: () => any;
    unshift: (item: any) => void;
    shift: () => any;
    reverse: () => void;
    sort: (predicate? : (left: any, right: any) => number) => void;
    splice: (start: number, count: number) => any[];
    remove: (predicate: (item: any) => bool) => void;
    remove: (item : any) => void;
    removeAll: (items?: any[]) => void;

I get red-squigglies at remove: (item : any) => void;



Oct 4, 2012 at 3:04 AM

Never mind! I am setting them up wrong.. (looked back at the jquery example and it all became clear :-P...  it should look like

interface observableArrayAny {
    (newValue: any[]): observableArrayAny;
    (): any[];
    subscribe(newValue: any[]) : void;
    indexOf(search: any) :any;
    slice(start: number, count: number) : any[];
    push(item: any) : void;
    pop() : any;
    unshift(item: any) : void;
    shift() : any;
    reverse() : void;
    sort(predicate? : (left: any, right: any) => number) : void;
    splice(start: number, count: number) : any[];
    remove(predicate: (item: any) => bool) : void;
    remove(item : any) : void;
    removeAll(items?: any[]) : void;



Oct 4, 2012 at 1:58 PM


Have ever try to implement your interface ? You get the same error.

Oct 4, 2012 at 2:07 PM

My original issue was that I was describing functions as Properties with a method description as there value.. Obviously you can't have two different properties with same name (no one lets you do that)... now Technically in Javascript, technically thats what a function is (a property with a method attached to it)... However, because we are describing overloads, we have to describe them as methods.. I don't get the error with the second interface because its properly describing overloading to the Typescript compiler.