diff --git a/index.js b/index.js index 0b7d96f..d182655 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,13 @@ * @typedef {import('unist').Node} Node */ +/** + * @template {Node} [Tree=Node] + * @typedef {import('./complex-types.js').MapFunction} MapFunction + * Function called with a node, its index, and its parent to produce a new + * node. + */ + /** * Create a new tree by mapping all nodes with the given function. * @@ -9,7 +16,7 @@ * Type of input tree. * @param {Tree} tree * Tree to map. - * @param {import('./complex-types').MapFunction} mapFunction + * @param {MapFunction} mapFunction * Function called with a node, its index, and its parent to produce a new * node. * @returns {Tree} diff --git a/readme.md b/readme.md index ce5e61a..b38ee20 100644 --- a/readme.md +++ b/readme.md @@ -133,8 +133,7 @@ if the original node has children, those are mapped instead. ## Types This package is fully typed with [TypeScript][]. -It exports a type `MapFunction` from -`unist-util-map/complex-types.d.ts` to properly type `MapFunction`s. +It exports a type `MapFunction` to properly type `MapFunction`s. ## Compatibility diff --git a/test.js b/test.js index 6e059d1..a7e09f6 100644 --- a/test.js +++ b/test.js @@ -46,6 +46,15 @@ test('unist-util-map', function (t) { 'should work when passing an empty object' ) + /** @type {Root} */ + const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]) + + t.deepEqual( + map(tree, asIs), + tree, + 'should support an explicitly typed `MapFunction`' + ) + t.end() /** @@ -69,4 +78,11 @@ test('unist-util-map', function (t) { function addValue() { return {value: 'test'} } + + /** + * @type {import('./index.js').MapFunction} + */ + function asIs(node) { + return node + } })