3

I am using jQuery and it's definitions from Definitely Typed collection. I want to extend it with a Farbtastitc color picker that ads new methods directly to JQueryStatic, for instance:

$.farbtastic(placeholder)

I don't want to modify the original jquery.d.ts file to add this method, instead I would like to create something like jquery-extend.d.ts with the added methods.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77
  • I would say extending framework with arbitrary methods is comfy but unsafe and not good design. In other words JavaScript. – daniel.sedlacek Feb 03 '14 at 23:45
  • Hi Daniel, although I agree in principle with that statement, it is the bread and butter of polyfills - so being able to do it is important, just as choosing "when not to" is. – Fenton Feb 04 '14 at 14:03
  • I'm not an expert but I can imagine the same done with interfaces. But I'm really not an expert. – daniel.sedlacek Feb 04 '14 at 22:21

1 Answers1

18

I found out that there is nothing preventing me from adding new methods to the original classes, so in the jquery-extend.d.ts I simply declare:

interface JQueryStatic {
    farbtastic(element : string, callback? : Function) : Farbtastic;
}

interface JQuery {
    farbtastic(element : string, callback? : Function) : void;
}

interface Farbtastic {
    linkTo(callback : Function) : void;
    setColor(color : string) : void;
}

Hope this will help someone one day.

daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77
  • What do you do with this new `d.ts` file for it to be recognized and used? I have done this but still just get the error that my new properties do not exist. I've seen a few answer to this general question but none address what to actually do with this file to have it be used. Do I have to put it in a specific location? – WillyC Nov 29 '16 at 17:55
  • @WillyC do this at the very top of your .ts file: /// – daniel.sedlacek Dec 04 '16 at 15:02