DefinitelyTyped/types/jquery
Leonard Thieu c6fe757c0d [jquery] Fix TContext declaration of jQuery.proxy on wrong value's type. (#29930)
* [jquery] Match parameter names to documentation more closely.

* [jquery] Add documentation to parameters.

* [jquery] Fix `TContext` declaration on wrong value's type.

Declaring `this` as `TContext` on the input function ensures that it can handle having its context changed to `TContext`. Declaring `this` on the output function is not important because the function will be already defined. It can also get in the way when passing a callback to a function whose callback parameter has `this` declared.

* [jquery] Drop constraint from `TContext`.

It's not really necessary and it is possible to pass primitives as the context.
2018-10-22 09:28:30 -07:00
..
dist [jquery] Split up declarations to stay under GitHub limits. Improve documentation. (#29831) 2018-10-18 09:23:20 -07:00
test [jquery] Split up declarations to stay under GitHub limits. Improve documentation. (#29831) 2018-10-18 09:23:20 -07:00
v1
v2 [jquery] Change all HTMLElement to be Element (#29282) 2018-10-03 11:53:04 -07:00
index.d.ts [jquery] Split up declarations to stay under GitHub limits. Improve documentation. (#29831) 2018-10-18 09:23:20 -07:00
jquery-tests.ts [jquery] Fix TContext declaration of jQuery.proxy on wrong value's type. (#29930) 2018-10-22 09:28:30 -07:00
JQuery.d.ts [jquery] Split up declarations to stay under GitHub limits. Improve documentation. (#29831) 2018-10-18 09:23:20 -07:00
JQueryStatic.d.ts [jquery] Fix TContext declaration of jQuery.proxy on wrong value's type. (#29930) 2018-10-22 09:28:30 -07:00
legacy.d.ts [jquery] Split up declarations to stay under GitHub limits. Improve documentation. (#29831) 2018-10-18 09:23:20 -07:00
misc.d.ts [jquery] Split up declarations to stay under GitHub limits. Improve documentation. (#29831) 2018-10-18 09:23:20 -07:00
README.md
tsconfig.json [jquery] Split up declarations to stay under GitHub limits. Improve documentation. (#29831) 2018-10-18 09:23:20 -07:00
tslint.json [jquery] Split up declarations to stay under GitHub limits. Improve documentation. (#29831) 2018-10-18 09:23:20 -07:00

Usage

Global

When jQuery is globally available, you can use jQuery and $ directly.

Importing (with a global DOM available)

When you want to import jQuery as a module and have a global DOM available (e.g. browser and browser-like environments):

import jQuery = require('jquery');

Importing (without a global DOM available)

When you want to import jQuery as a module and do not have a global DOM available (e.g. Node.js environment):

import jQueryFactory = require('jquery');
const jQuery = jQueryFactory(window, true);

Note that while the factory function ignores the second parameter, it is required to get correct type declarations.

Project structure

Authoring type definitions for jQuery plugins

$.fn is represented by JQuery.

$ is represented by JQueryStatic.

Declare an interface that has the plugin's overloads as call signatures and static members as properties.

interface MyPlugin {
    settings: MyPluginSettings;
    
    (behavior: 'enable'): JQuery;
    (settings?: MyPluginSettings): JQuery;
}

interface MyPluginSettings {
    title?: string;
}

Then declare a property on JQuery with your plugin's type.

interface JQuery {
    myPlugin: MyPlugin;
}