mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-07 13:15:59 +08:00
[jquery] Add README describing usage and guidance for authoring jQuery plugin typedefs.
This commit is contained in:
53
types/jquery/README.md
Normal file
53
types/jquery/README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
### 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):
|
||||
|
||||
```typescript
|
||||
import * as jQuery from '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):
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
### 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.
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
```typescript
|
||||
interface JQuery {
|
||||
myPlugin: MyPlugin;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user