How to handle type of Element.Properties? #72
-
example: const file = `<img>`;
const logNodes = () => {
return (tree) => {
visit(tree, "element", (node) => {
console.log(`
${node.tagName}: ${JSON.stringify(node.properties)}
`); // should be "img: undefined", but actually logs "img: {}"
if (node.properties) { // unnecessary because node.properties is never undefined, but without check it gives type error.
// do something...
}
});
};
};
unified()
.use(rehypeParse, { fragment: true })
.use(logNodes)
.use(rehypeStringify)
.process(file); The problem is, I cannot utilize Codepen link: https://codepen.io/plastic041/pen/powYBXQ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
If you type
|
Beta Was this translation helpful? Give feedback.
If you type
tree
as ahast.Root
, everything becomes typed.properties
should always beProperties
, notundefined
. However, people sometimes forget to add it in their plugins. That’s why it’s recommended to use an if check to make sure it exists