Closed
Description
The following code gives two errors:
const options: HTMLReactParserOptions = {
replace: (domNode: DOMNode) => {
if (!(domNode instanceof Element)) return
if (domNode.name === 'a') {
domNode.attribs.target = '_blank'
domNode.attribs.rel = 'noopener noreferrer'
return domNode // error 1
}
if (['html', 'head', 'body'].includes(domNode.name)) {
return <>{domToReact(domNode.children, options)}</> // error 2
}
}
}
Error 1:
Type '(domNode: DOMNode) => Element | JSX.Element | undefined' is not assignable to type '(domNode: DOMNode) => string | boolean | void | Element | null'.
Type 'Element | JSX.Element | undefined' is not assignable to type 'string | boolean | void | Element | null'.
Type 'Element' is not assignable to type 'string | boolean | void | Element | null'.
Type 'Element' is missing the following properties from type 'ReactElement<any, any>': props, keyts(2322)
It seems HTMLReactParserOptions
's replace
expects JSX.Element instead of domhandler's Element
Error 2:
Argument of type 'ChildNode[]' is not assignable to parameter of type 'DOMNode[]'.
Type 'ChildNode' is not assignable to type 'DOMNode'.
Type 'CDATA' is not assignable to type 'DOMNode'.
Type 'CDATA' is missing the following properties from type 'Element': name, attribs, tagName, attributests(2345)
Expected Behavior
There's no type errors
Actual Behavior
Build fails.
Steps to Reproduce
Use the above code
Reproducible Demo
Environment
- Version: v5.0.0
- Platform: React & Vite
- Browser: Chrome
- OS: macOS
Keywords
Type error