Skip to content

Commit 6e4d695

Browse files
chore(typescript): add dtslint as CI
- Specify return type for `replace`
1 parent c1223e5 commit 6e4d695

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ install:
55
- npm install
66
script:
77
- npm run lint
8+
- npm run dtslint
89
- npm run cover
910
- npm run build
1011
- npm run benchmark

types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export default HTMLReactParser;
99
type ReactElement = React.DetailedReactHTMLElement<{}, HTMLElement>;
1010

1111
export interface HTMLReactParserOptions {
12-
// TODO: Specify return type. React.ReactElement | undefined | false is not enough
13-
replace(domNode: DomNode): any;
12+
// TODO: Replace `object` by type for objects like `{ type: 'h1', props: { children: 'Heading' } }`
13+
replace(domNode: DomNode): React.ReactElement | object | undefined | false;
1414
}
1515

1616
/**

types/test.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import parse from 'html-dom-parser';
1+
import parse, { HTMLReactParserOptions } from 'html-dom-parser';
22
import * as React from 'react';
33

44
// $ExpectType string | DetailedReactHTMLElement<{}, HTMLElement> | DetailedReactHTMLElement<{}, HTMLElement>[]
@@ -16,13 +16,16 @@ parse('<p id="replace">text</p>', {
1616
});
1717

1818
// Return ReactElement
19-
parse('<p><br id="remove"></p>', {
20-
replace: ({ attribs }) =>
21-
attribs && attribs.id === 'remove' && <React.Fragment />
22-
});
19+
const options: HTMLReactParserOptions = {
20+
replace({ attribs }) {
21+
return attribs && attribs.id === 'remove' && <React.Fragment />;
22+
}
23+
}
24+
25+
parse('<p><br id="remove"></p>', options);
2326

2427
// Return domhandler node
25-
parse('<a href="#">Heading</a>', {
28+
parse('<a id="header" href="#">Heading</a>', {
2629
replace: node => {
2730
if (node.attribs && node.attribs.id === 'header') {
2831
return {

0 commit comments

Comments
 (0)