html-react-parser: TS Error: This expression is not callable
Expected Behavior
Expression is callable according to TS, ergo, no errors.
Actual Behavior
A TS error appears when calling:
import parse from 'html-react-parser';
parse('<b>foo</b>');
Error:
This expression is not callable.
Type 'typeof import("c:/Projects/.../node_modules/html-react-parser/index")' has no call signatures.
ts(2349)
Steps to Reproduce
Checkout the repro below, do a npm i
on it, and open it in VScode. This should display the error:
Maybe running tsc
on it will give the same error - I haven’t tried it.
Reproducible Demo
https://github.com/thany/html-react-parser-error
Please note: Yes, "moduleResolution": "node"
will fix it, but that will cause other problems. It has to be set to nodenext
and cannot be anything else, unfortunately.
Environment
- Version: 4.0.0
- Platform: Typescript I guess?
- Browser: N/A
- OS: Windows 10
Keywords
typescript import default error
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 15 (8 by maintainers)
Commits related to this issue
- fix(index): fix error https://github.com/remarkablemark/html-react-parser/issues/975 — committed to remarkablemark/html-react-parser-error-975 by remarkablemark a year ago
Closing this issue since we’ll be migrating to TypeScript in #1000
Yeah I thought so. Even so, feel free to use (parts of) it to modernise your branch.
Meanwhile, I’ve managed to change your repo in a fork, to have modern imports and exports everywhere, making it “just work” (in my scenario).
Imports/exports is definitely among “the bad parts” of javascript, sjeez. Anyway, here it is: https://github.com/thany/html-react-parser
I had to do a few more improvements on the side to get everything working again, because of course the unittests are quite rigorous, which is good, but it was quite a pain to figure them out. This also meant that everything needed to go in a single commit, because your precommit hook requires the unittests to succeed.
Lemme know if you’d like a PR.
Gotcha @thany, I’ll see if I can look into this some more or fork your repo to try things. Have a great weekend!
Oh for me
parse()
worked fine at runtime butparse.default()
just straight up didn’t exist, so I got an error along the lines of “…is not a function” (sorry, it’s the weekend now, I’m not at my work pc and enjoying a beer, so I don’t remember the exact wording 🙂).Even so, I don’t think it’s normal to be having to use that syntax, even if it did work. Unfortunately, packaging and exports and all that, combined with the plethora of configuration options and a sprinkle of typescript, is pure magic to me. Therefore I can’t be too sure what other packages are doing differently where a default export works exactly as per their documentation.
One such package is axios, but there are many more.
Cool. No hurry though, because we do have that silly workaround for the time being 🙂
And if it comes to upgrading typescript, well, that’s due anyway, but somehow I doubt it’s the root cause. I can certainly give it a go though.
Edit: nope, upgrading typescript didn’t help. VScode was set to use its internal version anyway, which is 5.1.3, and upping that to 5.1.6 (the latest stable) didn’t resolve the issue. It was a nice shot though.
Thanks, I’ll try to check it out later tonight when I have some time
In the repro, I added the same typescript version as the one in our proper project, which is 4.9.3.