html-react-parser icon indicating copy to clipboard operation
html-react-parser copied to clipboard

Cannot pass `domNode.children` to `domToReact`, as the docs suggest you can (with Typescript)

Open JoshMcCullough opened this issue 1 year ago • 3 comments

Expected Behavior

Examples in README should work correctly in terms of types / types should be correct..

Actual Behavior

The README suggests that you can directly pass domNode.children to domToReact, but you get a type error if you try to do this.

Steps to Reproduce

Try to pass a DOMNode's children to domToReact.

Reproducible Demo

N/A

Environment

N/A

Keywords

N/A

JoshMcCullough avatar Jun 11 '24 18:06 JoshMcCullough

Does this resolve your issue? https://github.com/remarkablemark/html-react-parser/tree/v5.1.10#v5

remarkablemark avatar Jun 11 '24 18:06 remarkablemark

Technically yes, I'm doing it a bit more "safely" than that:

function getChildNodes(childNodes: ChildNode[]): DOMNode[] {
  // Hacky but we need to only get named nodes (e.g. not comment nodes), and need to coerce them to be DOMNodes.
  return childNodes
    .filter(o => o instanceof Comment || o instanceof Element || o instanceof ProcessingInstruction || o instanceof Text)
    .map(o => o as DOMNode);
}

Called as:

domToReact(getChildNodes(node.children))

Coercing the type to DOMNode[] just hides the underlying type issue.

JoshMcCullough avatar Jun 11 '24 18:06 JoshMcCullough

Gotcha can you provide a reproducible example?

remarkablemark avatar Jun 11 '24 18:06 remarkablemark

Closing issue due to inactivity

remarkablemark avatar Dec 17 '24 00:12 remarkablemark

Same, but the fix https://github.com/remarkablemark/html-react-parser/issues/1444#issuecomment-2161367675 doesn't satisfies TS

tresorama avatar Oct 31 '25 14:10 tresorama