Skip to content

Commit 0da2807

Browse files
Merge pull request #146 from yunyu/ylin/jsx-element-types
fix: use JSX.Element for type definitions
2 parents 8a63228 + bae05c0 commit 0da2807

File tree

4 files changed

+13
-24
lines changed

4 files changed

+13
-24
lines changed

index.d.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// TypeScript Version: 3.3
22

3-
import * as React from 'react';
43
import { DomElement } from 'domhandler';
54
import domToReact from './lib/dom-to-react';
65
import htmlToDOM from 'html-dom-parser';
@@ -9,24 +8,21 @@ export interface HTMLReactParserOptions {
98
// TODO: Replace `object` by type for objects like `{ type: 'h1', props: { children: 'Heading' } }`
109
replace?: (
1110
domNode: DomElement
12-
) => React.ReactElement | object | void | undefined | null | false;
11+
) => JSX.Element | object | void | undefined | null | false;
1312
library?: object;
1413
}
1514

1615
/**
17-
* Converts HTML string to React elements.
16+
* Converts HTML string to JSX element(s).
1817
*
19-
* @param html - The HTML string to parse to React.
18+
* @param html - The HTML string to parse to JSX element(s).
2019
* @param options - The parser options.
21-
* @return - When parsed with HTML string, returns React elements; otherwise, returns string or empty array.
20+
* @return - Single or array of JSX elements.
2221
*/
2322
declare function HTMLReactParser(
2423
html: string,
2524
options?: HTMLReactParserOptions
26-
):
27-
| string
28-
| React.DetailedReactHTMLElement<{}, HTMLElement>
29-
| Array<React.DetailedReactHTMLElement<{}, HTMLElement>>;
25+
): ReturnType<typeof domToReact>;
3026

3127
export { DomElement, domToReact, htmlToDOM };
3228

lib/dom-to-react.d.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
// TypeScript Version: 3.3
22

3-
import * as React from 'react';
43
import { HTMLReactParserOptions } from '..';
54
import { DomElement } from 'domhandler';
65

76
/**
8-
* Converts DOM nodes to React elements.
7+
* Converts DOM nodes to JSX element(s).
98
*
10-
* @param nodes - A list of formatted DomNodes to convert to React.
11-
* @param options - Options to use when converting to react.
12-
* @returns ReactElement or and array of ReactElements.
9+
* @param nodes - An array of DomNodes to convert to JSX element(s).
10+
* @param options - Options to use when converting to JSX.
11+
* @returns Single or array of JSX elements.
1312
*/
1413
export default function domToReact(
1514
nodes: DomElement[],
1615
options?: HTMLReactParserOptions
17-
): React.ReactElement | React.ReactElement[];
16+
): JSX.Element | JSX.Element[];

test/types/index.test.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import parse, { HTMLReactParserOptions, domToReact, htmlToDOM } from 'html-react-parser';
22
import * as React from 'react';
33

4-
// $ExpectType string | DetailedReactHTMLElement<{}, HTMLElement> | DetailedReactHTMLElement<{}, HTMLElement>[]
4+
/* $ExpectType ReactElement | ReactElement[] */
55
parse('<div>text</div>');
66

77
// `options.replace`
@@ -39,8 +39,5 @@ parse('<a id="header" href="#">Heading</a>', {
3939
// $ExpectType DomElement[]
4040
const dom = htmlToDOM('<div>text</div>');
4141

42-
/* $ExpectType ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) |
43-
(new (props: any) => Component<any, any, any>)> |
44-
ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) |
45-
(new (props: any) => Component<any, any, any>)>[] */
42+
/* $ExpectType ReactElement | ReactElement[] */
4643
domToReact(dom);

test/types/lib/dom-to-react.test.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import domToReact from 'html-react-parser/lib/dom-to-react';
33
import * as React from 'react';
44
import htmlToDOM from 'html-dom-parser';
55

6-
/* $ExpectType ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) |
7-
(new (props: any) => Component<any, any, any>)> |
8-
ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) |
9-
(new (props: any) => Component<any, any, any>)>[] */
6+
/* $ExpectType ReactElement | ReactElement[] */
107
domToReact(htmlToDOM('<div>text</div>'));
118

129
// `options.replace`

0 commit comments

Comments
 (0)