-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change types to base what
visitor
gets on tree
Previously, to define what `visitor` received could only be done through a TypeScript type parameter: ```js // This used to work but no longer!! visitParents<Heading>(tree, 'heading', (node) => { expectType<Heading>(node) }) ``` This did not look at `tree` at all (even if `tree` was hast, and as `Heading` is mdast, it would pass `Heading` to `visitor`). It also made it impossible to narrow types in JS. Given that more and more of unist and friends is now strongly typed, we can expect `tree` to be some kind of implementation of `Node` rather than the abstract `Node` interface itself. With that, we can also find all possible node types inside `tree`. This commit changes to perform the test (`'heading'`) in the type system and actually narrow down which nodes that are in `tree` match `test`. This gives us: ```js // This now works: const tree: Root = {/* … */} visitParents(tree, 'heading', (node) => { expectType<Heading>(node) }) ``` Closes GH-29.
- Loading branch information
Showing
3 changed files
with
153 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters