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

New TS infers template literals which breaks for older TS #143

Closed nonara closed 3 years ago

nonara commented 3 years ago

Abstract

The current release seems to have somehow inferred template literal types as a return type for class methods in the compiled declaration files. In these cases, a string is the proper return type. Because template literal types are a newer feature, this is breaking compilation with typescript versions below the one which supports template literals.

Example

See: https://unpkg.com/node-html-parser@4.1.2/dist/nodes/comment.d.ts

Line 17 reads:

    toString(): `<!--${string}-->`;

What's odd is, when I run yarn build, this properly compiles to: toString(): string. It's possible that there was a bug with the version of TS that was used to compile the latest.

@taoqf Can you reset the project to the latest commit, wipe and reinstall node_modules, then run yarn build and let me know what line 17 says for ./dist/nodes/comment.d.ts ?

Proposed Solution

I think the solution here is:

  1. Reinstall all modules
  2. Rebuild
  3. Make sure there are no instances of the pattern: : ` (colon space backtick) in any *.d.ts files
    • Double check line 17 in /dist/nodes/commend.d.ts has type string to be sure
  4. Publish patch version

Related Issues

120

142

boukeversteegh commented 3 years ago

I also suspect that the build was bad.

There are two funny aspects here related to the typescript version:

So this is why upgrading to TS4 works, but it would be better if the dist contained :string in the first place.

Also, I did not like being required to update my typescript to a next major version, just for 1 utility dependency. If there are no deep reasons why it requires TS4, I would much prefer this library to be TS3 compatible.

taoqf commented 3 years ago

Sorry for late responding. I have rebuilt and released to 4.1.3. Thanks for all your work. https://github.com/taoqf/node-html-parser/release/tag/v4.1.3 https://unpkg.com/node-html-parser@4.1.3/dist/nodes/comment.d.ts