Repository: remarkablemark/html-react-parser (ts)

Issue:
Type errors with v5.0.0
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

Pull request:
fix(types): add back `object` to `replace` option return type
## What is the motivation for this pull request?

fix(types): add back `object` to `replace` option return type

Maintains backward compatibility for invalid return type

Fixes #1126

## What is the current behavior?

Removed `object` return type from `replace` option in v5

## What is the new behavior?

Revert removal of `object` return type from `replace` option

## Checklist:

- [x] [Conventional Commits](https://www.conventionalcommits.org/)
- [x] Tests
- [ ] Documentation

Patch:
diff --git a/src/types.ts b/src/types.ts
index c7f0d9f..d032410 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -17,7 +17,9 @@ export interface HTMLReactParserOptions {
     [key: string]: any;
   };
 
-  replace?: (domNode: DOMNode) => JSX.Element | string | null | boolean | void;
+  replace?: (
+    domNode: DOMNode,
+  ) => JSX.Element | string | null | boolean | object | void;
 
   transform?: (
     reactNode: ReactNode,


Test patch:
diff --git a/__tests__/dom-to-react.test.tsx b/__tests__/dom-to-react.test.tsx
index e99509d..92cafb5 100644
--- a/__tests__/dom-to-react.test.tsx
+++ b/__tests__/dom-to-react.test.tsx
@@ -172,6 +172,16 @@ describe('library option', () => {
 });
 
 describe('replace option', () => {
+  it.each([undefined, null, 0, 1, true, false, {}])(
+    'does not replace for invalid return value %p',
+    (value) => {
+      const reactElement = domToReact(htmlToDOM('<br>'), {
+        replace: () => value,
+      }) as JSX.Element;
+      expect(reactElement).toEqual(<br />);
+    },
+  );
+
   it("does not set key if there's a single node", () => {
     const reactElement = domToReact(htmlToDOM(html.single), {
       replace: () => <div />,
@@ -213,17 +223,12 @@ describe('replace option', () => {
     const options: HTMLReactParserOptions = {
       replace(domNode) {
         if (domNode instanceof Element) {
-          return <>{domToReact(domNode.children as DOMNode[], options)}</>;
+          return domToReact(domNode.children as DOMNode[], options);
         }
       },
     };
-
-    const reactElement = domToReact(
-      htmlToDOM(html.single),
-      options,
-    ) as JSX.Element;
-
-    expect(reactElement).toBeInstanceOf(Object);
+    const reactElement = domToReact(htmlToDOM('<div>test</div>'), options);
+    expect(reactElement).toEqual(<div>test</div>);
   });
 });