taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.12k stars 112 forks source link

Typescript types seem to be incorrect #47

Closed rschristian closed 4 years ago

rschristian commented 4 years ago

Trying to follow the little example in the ReadMe, it seems like your type definitions are off, as it certainly won't compile. The following code...

const root = parse('<ul id="list"><li>Hello World</li></ul>');
console.log(root.firstChild.structure);

...results in the following error...

index.tsx:31:33 - error TS2339: Property 'firstChild' does not exist on type '(TextNode & { valid: boolean; }) | (HTMLElement & { valid: boolean; })'.
  Property 'firstChild' does not exist on type 'TextNode & { valid: boolean; }'.

31             console.log(content.firstChild.structure);

Using Node-HTML-Parser v1.2.17

taoqf commented 4 years ago

I am so sorry that we do not have firstChild(and many attributes and methods) defined in TextNode.I have changed the definition in 1.2.18. let me know if you still have issues.

rschristian commented 4 years ago

Well one step closer, but structure does not exist on Node. It will need to be added here for someone to follow even the second line of the demo.

taoqf commented 4 years ago

It is not enough if only definition defined.see https://github.com/taoqf/node-html-parser/blob/master/src/nodes/html.ts#L228

alogim commented 3 years ago

@rschristian @taoqf If you want to follow the README with TypeScript, write the following:

import { HTMLElement, parse } from 'node-html-parser';
const root: HTMLElement = parse('<ul id="list"><li>Hello World</li></ul>');
console.log((root.firstChild as HTMLElement).structure);

Since HTMLElement is overwritten by this package, you need to import the one which has been overwritten and not the base one.