DefinitelyTyped/types/jquery
Leonard Thieu abbce5d159 [jquery] Documentation improvements (#29658)
* [jquery] Fix code for example not rendering in VS Code autocomplete.

VS Code does not render the code block of the last example in the autocomplete tooltip (but does in the hover tooltip). This is fixed by adding an additional line terminated by a zero-width space. There doesn't appear to be an obvious pattern to what causes this and there may be other incidences of this bug in the declarations.

* [jquery] Improve formatting of documentation for unified signatures in tooltips.

Documentation for unified signatures is also unified. This means that documentation for a parameter will contain documentation from all the parameters that it's composed of. VS Code and WebStorm render this as one continuous line. This makes it confusing as it's not obvious which parts of documentation apply to a parameter.

To remedy this, parameters are formatted as a bulletted list. `<br>` tags are necessary to force line breaks in WebStorm. The list also cannot start on the first line. To force the first line break, the `<br>` tag is used for WebStorm. For VS Code, a non-empty body must follow the parameter name on its line. A Braille Pattern Blank could be used here but to provide a more intuitive and consistent experience, VS Code's format for rendering parameter info is mimicked instead. The `@` symbol must be encoded to prevent `@param` from being parsed as a JSDoc tag.

Note: VS Code renders the `<br>` tags literally and there does not appear to be a way to hide them.
2018-10-11 11:45:40 -07:00
..
test [jquery] Drop type guards from isNumeric()/isPlainObject() and improve declarations of jQuery.data()/.data()/jQuery.css() (#29381) 2018-10-02 11:37:07 -07:00
v1
v2 [jquery] Change all HTMLElement to be Element (#29282) 2018-10-03 11:53:04 -07:00
index.d.ts [jquery] Documentation improvements (#29658) 2018-10-11 11:45:40 -07:00
jquery-tests.ts [jquery] Documentation improvements. Add jQuery.cleanData and overloads for jQuery.getScript and Data APIs. (#29573) 2018-10-09 08:34:57 -07:00
README.md
tsconfig.json
tslint.json

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;
}