I want to add a property someString to the JQueryStatic interface so I can access it with $.someString.
In index.ts, I have this code:
interface JQueryStatic {
someString: string;
}
$.someString = "string";
$ is of type JQueryStatic, however I get the following error:
Property 'someString' does not exist on type 'JQueryStatic'.
You can make use of an ambient type declaration that extends the jQuery type declarations like this:
put it in a
.d.tsfile somewhere in your code and make sure it's included (or not excluded) in yourtsconfig.json.Why does this work?
The
declarebit is what makes this "ambient" - as in "there exists somewhere aJQueryStaticwhich has asomeStringmember". TypeScript is smart and picks up that there is also anotherJQueryStaticambient declaration elsewhere (in your case the jQuery typings and "merges" them together in a singleJQueryStaticinterface declaration.