-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Types not regonized on nodes #37
Comments
Type your plugin. See all plugins for how to do it. |
This comment has been minimized.
This comment has been minimized.
Hi! Thanks for reaching out! Because we treat issues as our backlog, we close issues that are questions since they don’t represent a task to be completed. See our support docs for how and where to ask questions. Thanks, |
Adding on, there is a guide on this very topic you can reference @kam-st https://unifiedjs.com/learn/recipe/tree-traversal-typescript/ |
I'm doing something along the same lines but the typings aren't working for me when I'm using Root from hast
The visitor node type is
The node from remarkRehype will be a Root |
The explanation is sometimes the simplest possible explanation: You get a type error because you have a type error. Your code has an error.
No clue what you are doing. Place some |
Thank you for such a speedy reply! I was probably just getting a bit confused with all the different types exported. This works without any TS issues now.
|
you might not need those utilities: - const isAnchor = isElement(node, 'a');
- const hasHref = hasProperty(node, 'href');
-
- if (isAnchor && hasHref) {
+ if (node.tagName === 'a' && node.properties.href) { p.s. thanks for sponsoring me! :) |
Maybe not but it makes me look like I know what I'm doing to the rest of my colleagues. My pleasure, thank you for all of your work 🙏 |
Eg. given the DX of other TypeScript- or JSDoc-supporting libraries, I would expect with a simple call to import {Node} from 'unist';
import { visit } from 'unist-util-visit';
import '@types/unist';
visit({} as Node, 'element', (node) => {
// ^? never
}); Edit: it seems like I was using that because it's the type that is set when using import {Plugin} from 'unified'
const plugin: Plugin = () => {
return (tree) => {
// ^? unist.Node
}
} But maybe
Happy to open new separate issues for either of these 2 points, if they are accepted as possible improvements. |
Also, separately, related to plugin typing: Is there an easy-to-find example of how to get proper types without Looking through the remark List of Plugins, I found many which do not show typing related to the
Doing something like this doesn't change it from the import {Plugin} from 'unified'
import {Node} from 'unist'
import { visit } from 'unist-util-visit';
import '@types/unist'
function plugin(): ReturnType<Plugin> {
return (tree: Node) => {
visit(tree, 'element', (node) => {
// ^? never
})
}
} |
Hmm... after digging for a while, it seems like import { visit } from "unist-util-visit";
import type * as mdast from "mdast";
import type * as unified from "unified";
export const remarkCodeTitle: unified.Plugin<[], mdast.Root> = () => {
return (tree, file) => {
visit(tree, "code", (node, index, parent) => {
// ^? mdast.Code
})
}
} Not sure if this is the right thing to do, but this looks like the closest so far... 🤔 |
For anyone else coming here while trying to build a remark plugin: Seems that the key for a remark plugin is this And then an element type that would work here would be eg. import {Root} from 'mdast';
import { visit } from 'unist-util-visit';
visit({} as Root, 'image', (node) => {
// ^? mdast.Image
}); To follow the recommendation to type the plugin, then it would look like the import { visit } from "unist-util-visit";
import type { Root } from "mdast";
import type { Plugin } from "unified";
export const remarkCodeTitle: Plugin<[], Root> = () => {
return (tree, file) => {
visit(tree, "image", (node, index, parent) => {
// ^? mdast.Image
})
}
} Edit: The behavior below is a bug in the TypeScript Playground With JSDoc, it doesn't appear to be easy to get a typed import { visit } from "unist-util-visit";
// Only for TS Playground demo to download modules
import 'unified';
import 'mdast';
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
export const remarkCodeTitle = () => {
/**
* @param {import('mdast').Root} tree
*/
return (tree, file) => {
visit(tree, "image",
/**
* @param {import('mdast').Image} node
*/
(node, index, parent) => {
// ^? mdast.Image
}
)
}
} Trying to avoid the multiple manual annotations, the type of import { visit } from "unist-util-visit";
// Only for demo
import 'unified';
import 'mdast';
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
export const remarkCodeTitle = () => {
return (tree, file) => {
visit(tree, "image",
(node, index, parent) => {
// ^? any
}
)
}
} |
Type the input. This issue is posted at a utility. So how to type things depends. If you are making plugins as you are doing here, then it means typing the plugins. You do not need /**
* @import {Root} from 'mdast'
*/
import {visit} from 'unist-util-visit'
export function myPlugin() {
/**
* @param {Root} tree
* @returns {undefined}
*/
return function (tree) {
visit(tree, 'image', function (node) {
// `node` is typed.
})
}
} |
I would not recommend looking at one particular userland example and base all your thinking on it. Instead, look at several examples, particularly the ones maintained by us. For remark, look in the remark organization: https://github.com/orgs/remarkjs/repositories?type=source. |
Edit: The behavior below is a bug in the TypeScript Playground That would be a bit better, but it doesn't seem to yield a non- /**
* @import {Root} from 'mdast'
*/
import { visit } from "unist-util-visit";
// Only for demo
import 'unified';
import 'mdast';
export function myPlugin() {
/**
* @param {Root} tree
* @returns {undefined}
*/
return function (tree) {
visit(tree, 'image', function (node) {
// ^? any
})
}
} I can open a separate issue for this if this is considered a bug in |
I do not know why your playground doesn’t work. Perhaps your config isn‘t set up to support JavaScript, or Node, or perhaps packages don’t load. |
For some reason the playground is unable to load the unist types. As a result, the type of |
Indeed, in my reproduction repo I can confirm that the type is resolved to CI is also running through - no TypeScript errors or Interesting, so the TS Playground has a bug somewhere here... 🤔 |
I would guess that it tries to load the |
Not sure that's related - I think the Maybe Remco's comment indicates an unresolved |
👇
|
Reported a TypeScript Playground issue: |
Initial checklist
Affected packages and versions
unist-util-visit
Link to runnable example
No response
Steps to reproduce
In following code cannot find types for tagName, properties and children.
Expected behavior
Types should be recognized.
Actual behavior
node is of type 'never'
Affected runtime and version
node@20 lts
Affected package manager and version
No response
Affected OS and version
No response
Build and bundle tools
No response
The text was updated successfully, but these errors were encountered: