Skip to content

Commit f20f9d5

Browse files
committed
Add example of typing a plugin
1 parent cba99d6 commit f20f9d5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

readme.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,31 @@ The remark organization and the unified collective as a whole is fully typed
375375
with [TypeScript][].
376376
Types for mdast are available in [`@types/mdast`][types-mdast].
377377

378+
For TypeScript to work, it is particularly important to type your plugins
379+
correctly.
380+
We strongly recommend using the `Plugin` type from `unified` with its generics
381+
and to use the node types for the syntax trees provided by `@types/mdast`.
382+
383+
```js
384+
/**
385+
* @typedef {import('mdast').Root} Root
386+
*
387+
* @typedef Options
388+
* Configuration (optional).
389+
* @property {boolean} [someField]
390+
* Some option.
391+
*/
392+
393+
// To type options and that the it works with `mdast`:
394+
/** @type {import('unified').Plugin<[Options?], Root>} */
395+
export function myRemarkPluginAcceptingOptions(options) {
396+
// `options` is `Options?`.
397+
return function (tree, file) {
398+
// `tree` is `Root`.
399+
}
400+
}
401+
```
402+
378403
## Compatibility
379404

380405
Projects maintained by the unified collective are compatible with all maintained

0 commit comments

Comments
 (0)