mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-09-01 14:50:25 +00:00
* [jquery] `isNumeric` can not be a user defined type guard. `isNumeric` can return true for strings in addition to numbers, but only for a subset of either. It'll return false for NaN (which TypeScript treats as a number) and for strings which cannot be parsed as a number. * [jquery] `isPlainObject` can not be a user defined type guard. Due to the imprecise way with how `PlainObject` is declared, `isPlainObject` can not be a user defined type guard. * [jquery] Use https for links. * [jquery] Fix link for `JQuery.jquery`. * [jquery] Remove empty line before `@deprecated`. For consistent formatting since examples were added. * [jquery] Document additional `extend` parameters. * [jquery] Improve declarations of `data()` to catch unintentionally passing `undefined` as a value. This handles the more likely case of users passing a value that is possibly undefined. * [jquery] Improve declaration of `css()`. * [jquery] Minor improves to `proxy()` documentation. * [jquery] Fix region folding for WebStorm. WebStorm uses `// #region` to designate a region start while VS Code uses `// region`. They can both (must, in this case) be terminated using `// #endregion`. Using both `// #endregion` and `// endregion` results in odd behavior in both editors.
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
- jquery-tests.ts
- Tests that exercise TypeScript-specific usage and cases not covered by other test files.
- test/example-tests.ts
- Tests generated from examples in jQuery documentation.
- test/longdesc-tests.ts
- Tests generated from non-example snippets in jQuery documentation.
- test/learn-tests.ts
- Tests imported from examples in jQuery Learning Center.
- test/jquery-window-module-tests.ts
test/jquery-slim-window-module-tests.ts- Tests importing jQuery with a DOM available
- test/jquery-no-window-module-tests.ts
test/jquery-slim-no-window-module-tests.ts- Tests importing jQuery without a DOM available
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;
}