How do I override functions from the Kendo.ui.d.ts?

Topics: Language Specification
May 1, 2014 at 7:58 AM
Edited May 1, 2014 at 8:31 AM
Hi,

We use Telerik's Kendo UI Pro version for our views. We've written our own TypeScript wrappers for all the components we use from Kendo. That way we are able to react fast to changes in the Kendo UI code and we can extend on the functionality easily.

In the latest version Q1 2014 a new feature has become available in the grids called 'frozen column' which is a very nice one. A problem we found though is that the select() now returns 2 parts for each row (because the grid is divided into 2 tables with in each table a part of the row).

We got a work around for this from Telerik and would like to implement that in our TypeScript wrapper. Now this means we have to 'override' the 'select()' from the kendo grid. The problem is that the select is defined in the d.ts like so:
        select(): JQuery;
        select(rows: string): void;
        select(rows: Element): void;
        select(rows: JQuery): void;
Our implementation (override) should look something like this:
        public select(rows: any): JQuery
        {
            if (rows)
            {
                super.select(rows);
            }
            else
            {
                return this.tbody.find(".k-state-selected");
            }
        }
The TypeScript compiler can't extend the class that way because
Error   1   Class 'kendoWrappers.CttGrid' cannot extend class 'kendo.ui.Grid':
    Types of property 'select' of types 'CttGrid' and 'kendo.ui.Grid' are incompatible:
        Call signatures of types '(rows: any) => JQuery' and '{ (): JQuery; (rows: string): void; (rows: Element): void; (rows: JQuery): void; }' are incompatible:
            Call signature expects 0 or fewer parameters.   C:\NotScanned\Ontwikkeling\ControlIT\trunk\Fortrus.Messaging\Scripts\contractors\kendoWrappers\KendoWrappers.ts 320 15  Fortrus.Messaging
The only way I found to solve this is to edit the kendo.ui.d.ts file like so:
        select(rows: any): JQuery;
Now for obvious reasons we don't want to edit the kendo.ui.d.ts file. Should we convince Telerik they should change the way the 'select()' is defined or can is there a way we can implement an override the compiler accepts?

Regards
Paul
May 1, 2014 at 8:31 AM
The addition of the '?' behind the 'rows' seems to do the trick:
        public select(rows?: any): JQuery
        {
            if (rows)
            {
                super.select(rows);
            }
            else
            {
                return this.tbody.find(".k-state-selected");
            }
        }
Marked as answer by PaulSinnema on 5/1/2014 at 1:53 AM
Developer
May 1, 2014 at 7:37 PM
To expand on the reasoning here: The definition for select says "I can be called with no arguments, or I could have an argument of type string, Element, or JQuery." Your override clearly states "I must be called with an argument of any type." Since your class could have its select function called polymorphically through an instance of the base class type which would allow 0 arguments you can't have your implementation take a dependency on > 0 arguments existing. By marking rows as optional in your implementation you have satisfied the requirement for the parameter count, and because your parameter is of type any it satisfies the type requirements the overloads impose.