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

Update querySelector type definition to return null #157

Closed johnnyoshika closed 3 years ago

johnnyoshika commented 3 years ago

The TypeScript return type for querySelector() is HTMLElement instead of HTMLElement | null. querySelector() can potentially return null if no elements are found. This will allow code that could throw a null reference exception to pass the type checker.

Relevant type definition:

    /**
     * Query CSS Selector to find matching node.
     * @param  {string}         selector Simplified CSS selector
     * @return {HTMLElement}    matching node
     */
    querySelector(selector: string): HTMLElement;

https://github.com/taoqf/node-html-parser/blob/2077d420b9cfde74e4f03639fa569b937735ebc5/src/nodes/html.ts#L383-L386

Can you set the return type to HTMLElement | null?

nonara commented 3 years ago

Good catch! Thanks for the heads up, Johnny.

johnnyoshika commented 3 years ago

@nonara Thank you for the great library! I depend on it extensively in my project.

nonara commented 3 years ago

Happy to help! @taoqf is the library's author, but I'm sure he'll be glad to hear that it's working well for you!

I have this and several other fixes in place in the following PR, but I'll also follow-up in this thread when we have a release ready.

nonara commented 3 years ago

Corrected in v5